Rect.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 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. #pragma once
  24. #include "Vector2.h"
  25. /// Two-dimensional bounding rectangle.
  26. class Rect
  27. {
  28. public:
  29. /// Construct an undefined rect.
  30. Rect() :
  31. min_(Vector2::ZERO),
  32. max_(Vector2::ZERO),
  33. defined_(false)
  34. {
  35. }
  36. /// Copy-construct from another rect.
  37. Rect(const Rect& rect) :
  38. min_(rect.min_),
  39. max_(rect.max_),
  40. defined_(rect.defined_)
  41. {
  42. }
  43. /// Construct from minimum and maximum vectors.
  44. Rect(const Vector2& min, const Vector2& max) :
  45. min_(min),
  46. max_(max),
  47. defined_(true)
  48. {
  49. }
  50. /// Construct from coordinates.
  51. Rect(float left, float top, float right, float bottom) :
  52. min_(left, top),
  53. max_(right, bottom),
  54. defined_(true)
  55. {
  56. }
  57. /// Assign from another rect.
  58. Rect& operator = (const Rect& rhs)
  59. {
  60. min_ = rhs.min_;
  61. max_ = rhs.max_;
  62. defined_ = rhs.defined_;
  63. return *this;
  64. }
  65. /// Test for equality with another rect.
  66. bool operator == (const Rect& rhs) const { return min_ == rhs.min_ && max_ == rhs.max_; }
  67. /// Test for inequality with another rect.
  68. bool operator != (const Rect& rhs) const { return min_ != rhs.min_ || max_ != rhs.max_; }
  69. /// Define from another rect.
  70. void Define(const Rect& rect)
  71. {
  72. min_ = rect.min_;
  73. max_ = rect.max_;
  74. defined_ = true;
  75. }
  76. /// Define from minimum and maximum vectors.
  77. void Define(const Vector2& min, const Vector2& max)
  78. {
  79. min_ = min;
  80. max_ = max;
  81. defined_ = true;
  82. }
  83. /// Define from a point.
  84. void Define(const Vector2& point)
  85. {
  86. min_ = max_ = point;
  87. defined_ = true;
  88. }
  89. /// Merge a point.
  90. void Merge(const Vector2& point)
  91. {
  92. if (!defined_)
  93. {
  94. min_ = max_ = point;
  95. defined_ = true;
  96. }
  97. if (point.x_ < min_.x_)
  98. min_.x_ = point.x_;
  99. if (point.x_ > max_.x_)
  100. max_.x_ = point.x_;
  101. if (point.y_ < min_.y_)
  102. min_.y_ = point.y_;
  103. if (point.y_ > max_.y_)
  104. max_.y_ = point.y_;
  105. }
  106. /// Merge a rect.
  107. void Merge(const Rect& rect)
  108. {
  109. if (!defined_)
  110. {
  111. min_ = rect.min_;
  112. max_ = rect.max_;
  113. defined_ = true;
  114. }
  115. if (rect.min_.x_ < min_.x_)
  116. min_.x_ = rect.min_.x_;
  117. if (rect.min_.y_ < min_.y_)
  118. min_.y_ = rect.min_.y_;
  119. if (rect.max_.x_ > max_.x_)
  120. max_.x_ = rect.max_.x_;
  121. if (rect.max_.y_ > max_.y_)
  122. max_.y_ = rect.max_.y_;
  123. }
  124. /// Clear to undefined state.
  125. void Clear()
  126. {
  127. min_ = Vector2::ZERO;
  128. max_ = Vector2::ZERO;
  129. defined_ = false;
  130. }
  131. /// Clip with another rect.
  132. void Clip(const Rect& rect);
  133. /// Return center.
  134. Vector2 Center() const { return (max_ + min_) * 0.5f; }
  135. /// Return size.
  136. Vector2 Size() const { return max_ - min_; }
  137. /// Return half-size.
  138. Vector2 HalfSize() const { return (max_ - min_) * 0.5f; }
  139. /// Return float data.
  140. const void* Data() const { return &min_.x_; }
  141. /// Return as string.
  142. String ToString() const;
  143. /// Minimum vector.
  144. Vector2 min_;
  145. /// Maximum vector.
  146. Vector2 max_;
  147. /// Defined flag.
  148. bool defined_;
  149. /// Rect in the range (-1, -1) - (1, 1)
  150. static const Rect FULL;
  151. /// Rect in the range (0, 0) - (1, 1)
  152. static const Rect POSITIVE;
  153. /// Zero-sized rect.
  154. static const Rect ZERO;
  155. };
  156. /// Two-dimensional bounding rectangle with integer values.
  157. class IntRect
  158. {
  159. public:
  160. /// Construct an undefined rect.
  161. IntRect()
  162. {
  163. }
  164. /// Construct from coordinates.
  165. IntRect(int left, int top, int right, int bottom) :
  166. left_(left),
  167. top_(top),
  168. right_(right),
  169. bottom_(bottom)
  170. {
  171. }
  172. /// Test for equality with another rect.
  173. bool operator == (const IntRect& rhs) const { return left_ == rhs.left_ && top_ == rhs.top_ && right_ == rhs.right_ && bottom_ == rhs.bottom_; }
  174. /// Test for inequality with another rect.
  175. bool operator != (const IntRect& rhs) const { return left_ != rhs.left_ || top_ != rhs.top_ || right_ != rhs.right_ || bottom_ != rhs.bottom_; }
  176. /// Return integer data.
  177. const int* GetData() const { return &left_; }
  178. /// Return as string.
  179. String ToString() const;
  180. /// Left coordinate.
  181. int left_;
  182. /// Top coordinate.
  183. int top_;
  184. /// Right coordinate.
  185. int right_;
  186. /// Bottom coordinate.
  187. int bottom_;
  188. /// Zero-sized rect.
  189. static const IntRect ZERO;
  190. };