Rectangle.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. #include "Base.h"
  2. #include "Rectangle.h"
  3. namespace gameplay
  4. {
  5. Rectangle::Rectangle()
  6. : x(0), y(0), width(0), height(0)
  7. {
  8. }
  9. Rectangle::Rectangle(float width, float height) :
  10. x(0), y(0), width(width), height(height)
  11. {
  12. }
  13. Rectangle::Rectangle(float x, float y, float width, float height) :
  14. x(x), y(y), width(width), height(height)
  15. {
  16. }
  17. Rectangle::Rectangle(const Rectangle& copy)
  18. {
  19. set(copy);
  20. }
  21. Rectangle::~Rectangle()
  22. {
  23. }
  24. const Rectangle& Rectangle::empty()
  25. {
  26. static Rectangle empty;
  27. return empty;
  28. }
  29. bool Rectangle::isEmpty() const
  30. {
  31. return (x == 0 && y == 0 && width == 0 && height == 0);
  32. }
  33. void Rectangle::set(const Rectangle& r)
  34. {
  35. set(r.x, r.y, r.width, r.height);
  36. }
  37. void Rectangle::set(float x, float y, float width, float height)
  38. {
  39. this->x = x;
  40. this->y = y;
  41. this->width = width;
  42. this->height = height;
  43. }
  44. void Rectangle::setPosition(float x, float y)
  45. {
  46. this->x = x;
  47. this->y = y;
  48. }
  49. float Rectangle::left() const
  50. {
  51. return x;
  52. }
  53. float Rectangle::top() const
  54. {
  55. return y;
  56. }
  57. float Rectangle::right() const
  58. {
  59. return x + width;
  60. }
  61. float Rectangle::bottom() const
  62. {
  63. return y + height;
  64. }
  65. bool Rectangle::contains(float x, float y) const
  66. {
  67. return (x >= x && x <= (x + width) && y >= y && y <= (y + height));
  68. }
  69. bool Rectangle::contains(float x, float y, float width, float height) const
  70. {
  71. return (contains(x, y) && contains(x + width, y + height));
  72. }
  73. bool Rectangle::contains(const Rectangle& r) const
  74. {
  75. return contains(r.x, r.y, r.width, r.height);
  76. }
  77. bool Rectangle::intersects(float x, float y, float width, float height) const
  78. {
  79. const float left = max(this->x, x);
  80. const float top = max(this->y, y);
  81. const float right = min(x + width, x + width);
  82. const float bottom = min(y + height, y + height);
  83. return (right > left && bottom > top);
  84. }
  85. bool Rectangle::intersects(const Rectangle& r) const
  86. {
  87. return intersects(r.x, r.y, r.width, r.height);
  88. }
  89. void Rectangle::combine(const Rectangle& r1, const Rectangle& r2, Rectangle* dst)
  90. {
  91. GP_ASSERT(dst);
  92. dst->x = min(r1.x, r2.x);
  93. dst->y = min(r1.y, r2.y);
  94. dst->width = max(r1.x + r1.width, r2.x + r2.width) - dst->x;
  95. dst->height = max(r1.y + r1.height, r2.y + r2.height) - dst->y;
  96. }
  97. void Rectangle::inflate(float horizontalAmount, float verticalAmount)
  98. {
  99. x -= horizontalAmount;
  100. y -= verticalAmount;
  101. width += horizontalAmount * 2;
  102. height += verticalAmount * 2;
  103. }
  104. const Rectangle& Rectangle::operator = (const Rectangle& r)
  105. {
  106. x = r.x;
  107. y = r.y;
  108. width = r.width;
  109. height = r.height;
  110. return *this;
  111. }
  112. bool Rectangle::operator == (const Rectangle& r) const
  113. {
  114. return (x == r.x && width == r.width && y == r.y && height == r.height);
  115. }
  116. bool Rectangle::operator != (const Rectangle& r) const
  117. {
  118. return (x != r.x || width != r.width || y != r.y || height != r.height);
  119. }
  120. }