Rect.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #ifndef MATH_RECT_H
  24. #define MATH_RECT_H
  25. #include "Vector2.h"
  26. //! A two-dimensional bounding rectangle
  27. class Rect
  28. {
  29. public:
  30. //! Construct an undefined rect
  31. Rect() :
  32. mDefined(false)
  33. {
  34. }
  35. //! Copy-construct from another rect
  36. Rect(const Rect& rect) :
  37. mMin(rect.mMin),
  38. mMax(rect.mMax),
  39. mDefined(true)
  40. {
  41. }
  42. //! Construct from minimum and maximum vectors
  43. Rect(const Vector2& min, const Vector2& max) :
  44. mMin(min),
  45. mMax(max),
  46. mDefined(true)
  47. {
  48. }
  49. //! Construct from coordinates
  50. Rect(float left, float top, float right, float bottom) :
  51. mMin(left, top),
  52. mMax(right, bottom),
  53. mDefined(true)
  54. {
  55. }
  56. //! Assign from another rect
  57. Rect& operator = (const Rect& rhs)
  58. {
  59. mMin = rhs.mMin;
  60. mMax = rhs.mMax;
  61. mDefined = true;
  62. return *this;
  63. }
  64. //! Test for equality with another rect
  65. bool operator == (const Rect& rhs) const
  66. {
  67. return (mMin == rhs.mMin) && (mMax == rhs.mMax);
  68. }
  69. //! Test for inequality with another rect
  70. bool operator != (const Rect& rhs) const
  71. {
  72. return (mMin != rhs.mMin) || (mMax != rhs.mMax);
  73. }
  74. //! Define from minimum and maximum vectors
  75. void define(const Vector2& min, const Vector2& max)
  76. {
  77. mMin = min;
  78. mMax = max;
  79. mDefined = true;
  80. }
  81. //! Define from a point
  82. void define(const Vector2& point)
  83. {
  84. mMin = mMax = point;
  85. mDefined = true;
  86. }
  87. //! Merge a point
  88. void merge(const Vector2& point)
  89. {
  90. if (!mDefined)
  91. {
  92. mMin = mMax = point;
  93. mDefined = true;
  94. }
  95. if (point.mX < mMin.mX)
  96. mMin.mX = point.mX;
  97. if (point.mX > mMax.mX)
  98. mMax.mX = point.mX;
  99. if (point.mY < mMin.mY)
  100. mMin.mY = point.mY;
  101. if (point.mY > mMax.mY)
  102. mMax.mY = point.mY;
  103. }
  104. //! Merge a rect
  105. void merge(const Rect& rect)
  106. {
  107. if (!mDefined)
  108. {
  109. mMin = rect.mMin;
  110. mMax = rect.mMax;
  111. mDefined = true;
  112. }
  113. if (rect.mMin.mX < mMin.mX) mMin.mX = rect.mMin.mX;
  114. if (rect.mMin.mY < mMin.mY) mMin.mY = rect.mMin.mY;
  115. if (rect.mMax.mX > mMax.mX) mMax.mX = rect.mMax.mX;
  116. if (rect.mMax.mY > mMax.mY) mMax.mY = rect.mMax.mY;
  117. }
  118. //! Return float data
  119. const void* getData() const { return &mMin.mX; }
  120. //! Minimum vector
  121. Vector2 mMin;
  122. //! Maximum vector
  123. Vector2 mMax;
  124. //! Defined flag
  125. bool mDefined;
  126. //! A rect in the range (-1, -1) - (1, 1)
  127. static const Rect sFullRect;
  128. //! A rect in the range (0, 0) - (1, 1)
  129. static const Rect sPositiveRect;
  130. //! A zero-sized rect
  131. static const Rect sZero;
  132. };
  133. //! A two-dimensional bounding rectangle with integer values
  134. class IntRect
  135. {
  136. public:
  137. //! Construct an undefined rect
  138. IntRect()
  139. {
  140. }
  141. //! Construct from coordinates
  142. IntRect(int left, int top, int right, int bottom) :
  143. mLeft(left),
  144. mTop(top),
  145. mRight(right),
  146. mBottom(bottom)
  147. {
  148. }
  149. //! Test for equality with another rect
  150. bool operator == (const IntRect& rhs) const
  151. {
  152. return (mLeft == rhs.mLeft) && (mTop == rhs.mTop) && (mRight == rhs.mRight) && (mBottom == rhs.mBottom);
  153. }
  154. //! Test for inequality with another rect
  155. bool operator != (const IntRect& rhs) const
  156. {
  157. return (mLeft != rhs.mLeft) || (mTop != rhs.mTop) || (mRight != rhs.mRight) || (mBottom != rhs.mBottom);
  158. }
  159. //! Return integer data
  160. const int* getData() const { return &mLeft; }
  161. //! Left coordinate
  162. int mLeft;
  163. //! Top coordinate
  164. int mTop;
  165. //! Right coordinate
  166. int mRight;
  167. //! Bottom coordinate
  168. int mBottom;
  169. //! Zero-sized rect
  170. static const IntRect sZero;
  171. };
  172. #endif // MATH_RECT_H