Rect.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #pragma once
  2. #include "Common.h"
  3. #include "Point.h"
  4. NS_BF_BEGIN;
  5. template <typename T>
  6. class Rect
  7. {
  8. public:
  9. T x;
  10. T y;
  11. T width;
  12. T height;
  13. public:
  14. Rect()
  15. {
  16. x = 0;
  17. y = 0;
  18. width = 0;
  19. height = 0;
  20. }
  21. Rect(T x, T y, T width, T height)
  22. {
  23. this->x = x;
  24. this->y = y;
  25. this->width = width;
  26. this->height = height;
  27. }
  28. bool operator==(const Rect& r2)
  29. {
  30. return (x == r2.x) && (y == r2.y) && (width == r2.width) && (height == r2.height);
  31. }
  32. bool operator!=(const Rect& r2)
  33. {
  34. return (x != r2.x) || (y != r2.y) || (width != r2.width) || (height != r2.height);
  35. }
  36. bool Contains(T x, T y)
  37. {
  38. return (x >= this->x) && (y >= this->y) && (x < this->x + width) && (y < this->y + height);
  39. }
  40. bool Contains(Point<T> pt)
  41. {
  42. return (pt.x >= this->x) && (y >= this->y) && (x < this->x + width) && (y < this->y + height);
  43. }
  44. T GetRight()
  45. {
  46. return x + width;
  47. }
  48. T GetBottom()
  49. {
  50. return y + height;
  51. }
  52. Rect Intersection(Rect rect)
  53. {
  54. T x1 = Max(x, rect.x);
  55. T x2 = Min(x + width, rect.x + rect.width);
  56. T y1 = Max(y, rect.y);
  57. T y2 = Min(y + height, rect.y + rect.height);
  58. if (((x2 - x1) < 0) || ((y2 - y1) < 0))
  59. return Rect();
  60. else
  61. return Rect(x1, y1, x2 - x1, y2 - y1);
  62. }
  63. bool Intersects(Rect rect)
  64. {
  65. T x1 = BF_MAX(x, rect.x);
  66. T x2 = BF_MIN(x + width, rect.x + rect.width);
  67. T y1 = BF_MAX(y, rect.y);
  68. T y2 = BF_MIN(y + height, rect.y + rect.height);
  69. if (((x2 - x1) <= 0) || ((y2 - y1) <= 0))
  70. return false;
  71. else
  72. return true;
  73. }
  74. Rect Union(Rect rect)
  75. {
  76. T x1 = Min(x, rect.x);
  77. T x2 = Max(x + width, rect.x + rect.width);
  78. T y1 = Min(y, rect.y);
  79. T y2 = Max(y + height, rect.y + rect.height);
  80. return Rect(x1, y1, x2 - x1, y2 - y1);
  81. }
  82. void Include(Point<T> pt)
  83. {
  84. T left = x;
  85. T top = y;
  86. T right = x + width;
  87. T bottom = y + height;
  88. x = BF_MIN(pt.x, left);
  89. y = BF_MIN(pt.y, top);
  90. width = BF_MAX(pt.x, right) - x;
  91. height = BF_MAX(pt.y, bottom) - y;
  92. }
  93. void Inflate(T x, T y)
  94. {
  95. this->x -= x;
  96. this->width += x + x;
  97. this->y -= y;
  98. this->height += y + y;
  99. }
  100. };
  101. typedef Rect<double> RectD;
  102. typedef Rect<float> RectF;
  103. typedef Rect<int32> RectI32;
  104. NS_BF_END;
  105. template <>
  106. struct BeefHash<Beefy::RectI32>
  107. {
  108. size_t operator()(Beefy::RectI32 val)
  109. {
  110. return (size_t)val.x * 4790557 + (size_t)val.y * 6578863 + (size_t)val.width * 6273881 + (size_t)val.height * 9501077;
  111. }
  112. };