Rectangle.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 >= this->x && x <= (this->x + width) && y >= this->y && y <= (this->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. float t;
  80. if ((t = x - this->x) > this->width || -t > width)
  81. return false;
  82. if ((t = y - this->y) > this->height || -t > height)
  83. return false;
  84. return true;
  85. }
  86. bool Rectangle::intersects(const Rectangle& r) const
  87. {
  88. return intersects(r.x, r.y, r.width, r.height);
  89. }
  90. bool Rectangle::intersect(const Rectangle& r1, const Rectangle& r2, Rectangle* dst)
  91. {
  92. GP_ASSERT(dst);
  93. float xmin = max(r1.x, r2.x);
  94. float xmax = min(r1.right(), r2.right());
  95. if (xmax > xmin)
  96. {
  97. float ymin = max(r1.y, r2.y);
  98. float ymax = min(r1.bottom(), r2.bottom());
  99. if (ymax > ymin)
  100. {
  101. dst->set(xmin, ymin, xmax - xmin, ymax - ymin);
  102. return true;
  103. }
  104. }
  105. dst->set(0, 0, 0, 0);
  106. return false;
  107. }
  108. void Rectangle::combine(const Rectangle& r1, const Rectangle& r2, Rectangle* dst)
  109. {
  110. GP_ASSERT(dst);
  111. dst->x = min(r1.x, r2.x);
  112. dst->y = min(r1.y, r2.y);
  113. dst->width = max(r1.x + r1.width, r2.x + r2.width) - dst->x;
  114. dst->height = max(r1.y + r1.height, r2.y + r2.height) - dst->y;
  115. }
  116. void Rectangle::inflate(float horizontalAmount, float verticalAmount)
  117. {
  118. x -= horizontalAmount;
  119. y -= verticalAmount;
  120. width += horizontalAmount * 2;
  121. height += verticalAmount * 2;
  122. }
  123. Rectangle& Rectangle::operator = (const Rectangle& r)
  124. {
  125. x = r.x;
  126. y = r.y;
  127. width = r.width;
  128. height = r.height;
  129. return *this;
  130. }
  131. bool Rectangle::operator == (const Rectangle& r) const
  132. {
  133. return (x == r.x && width == r.width && y == r.y && height == r.height);
  134. }
  135. bool Rectangle::operator != (const Rectangle& r) const
  136. {
  137. return (x != r.x || width != r.width || y != r.y || height != r.height);
  138. }
  139. }