Rectangle.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. float Rectangle::left() const
  45. {
  46. return x;
  47. }
  48. float Rectangle::top() const
  49. {
  50. return y;
  51. }
  52. float Rectangle::right() const
  53. {
  54. return x + width;
  55. }
  56. float Rectangle::bottom() const
  57. {
  58. return y + height;
  59. }
  60. bool Rectangle::contains(float x, float y) const
  61. {
  62. return (x >= x && x <= (x + width) && y >= y && y <= (y + height));
  63. }
  64. bool Rectangle::contains(float x, float y, float width, float height) const
  65. {
  66. return (contains(x, y) && contains(x + width, y + height));
  67. }
  68. bool Rectangle::contains(const Rectangle& r) const
  69. {
  70. return contains(r.x, r.y, r.width, r.height);
  71. }
  72. bool Rectangle::intersects(float x, float y, float width, float height) const
  73. {
  74. const float left = fmaxf(this->x, x);
  75. const float top = fmaxf(this->y, y);
  76. const float right = fminf(x + width, x + width);
  77. const float bottom = fminf(y + height, y + height);
  78. return (right > left && bottom > top);
  79. }
  80. bool Rectangle::intersects(const Rectangle& r) const
  81. {
  82. return intersects(r.x, r.y, r.width, r.height);
  83. }
  84. void Rectangle::combine(const Rectangle& r1, const Rectangle& r2, Rectangle* dst)
  85. {
  86. dst->x = fminf(r1.x, r2.x);
  87. dst->y = fminf(r1.y, r2.y);
  88. dst->width = fmaxf(r1.x + r1.width, r2.x + r2.width) - dst->x;
  89. dst->height = fmaxf(r1.y + r1.height, r2.y + r2.height) - dst->y;
  90. }
  91. void Rectangle::inflate(float horizontalAmount, float verticalAmount)
  92. {
  93. x -= horizontalAmount;
  94. y -= verticalAmount;
  95. width += horizontalAmount * 2;
  96. height += verticalAmount * 2;
  97. }
  98. const Rectangle& Rectangle::operator = (const Rectangle& r)
  99. {
  100. x = r.x;
  101. y = r.y;
  102. width = r.width;
  103. height = r.height;
  104. return *this;
  105. }
  106. bool Rectangle::operator == (const Rectangle& r) const
  107. {
  108. return (x == r.x && width == r.width && y == r.y && height == r.height);
  109. }
  110. bool Rectangle::operator != (const Rectangle& r) const
  111. {
  112. return (x != r.x || width != r.width || y != r.y || height != r.height);
  113. }
  114. }