Rectangle.cpp 2.9 KB

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