trinRectangle.js 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. function TrinRectangle(x, y, width, height) {
  2. this.x = x;
  3. this.y = y;
  4. this.width = width;
  5. this.height = height;
  6. }
  7. TrinRectangle.prototype = {
  8. top: function() {
  9. return this.y;
  10. },
  11. left: function() {
  12. return this.x;
  13. },
  14. bottom: function() {
  15. return this.y + this.height;
  16. },
  17. right: function() {
  18. return this.x + this.width;
  19. },
  20. set: function(x, y, width, height) {
  21. this.x = x;
  22. this.y = y;
  23. this.width = width;
  24. this.height = height;
  25. },
  26. intersects: function(x, y, width, height) {
  27. if (width === undefined && height === undefined) {
  28. return (x > this.left() && x < this.right() && y > this.top() && y < this.bottom());
  29. }
  30. var t = y;
  31. var r = x + width;
  32. var b = y + height;
  33. var l = x;
  34. return (r > this.left() && l < this.right()) && (b > this.top() && t < this.bottom());
  35. },
  36. intersectsRect: function(rect) {
  37. return this.intersects(rect.x, rect.y, rect.width, rect.height);
  38. }
  39. };