Rect.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. //
  2. // Copyright (c) 2008-2013 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #pragma once
  23. #include "Vector2.h"
  24. namespace Urho3D
  25. {
  26. /// Two-dimensional bounding rectangle.
  27. class Rect
  28. {
  29. public:
  30. /// Construct an undefined rect.
  31. Rect() :
  32. min_(Vector2::ZERO),
  33. max_(Vector2::ZERO),
  34. defined_(false)
  35. {
  36. }
  37. /// Copy-construct from another rect.
  38. Rect(const Rect& rect) :
  39. min_(rect.min_),
  40. max_(rect.max_),
  41. defined_(rect.defined_)
  42. {
  43. }
  44. /// Construct from minimum and maximum vectors.
  45. Rect(const Vector2& min, const Vector2& max) :
  46. min_(min),
  47. max_(max),
  48. defined_(true)
  49. {
  50. }
  51. /// Construct from coordinates.
  52. Rect(float left, float top, float right, float bottom) :
  53. min_(left, top),
  54. max_(right, bottom),
  55. defined_(true)
  56. {
  57. }
  58. /// Assign from another rect.
  59. Rect& operator = (const Rect& rhs)
  60. {
  61. min_ = rhs.min_;
  62. max_ = rhs.max_;
  63. defined_ = rhs.defined_;
  64. return *this;
  65. }
  66. /// Test for equality with another rect.
  67. bool operator == (const Rect& rhs) const { return min_ == rhs.min_ && max_ == rhs.max_; }
  68. /// Test for inequality with another rect.
  69. bool operator != (const Rect& rhs) const { return min_ != rhs.min_ || max_ != rhs.max_; }
  70. /// Define from another rect.
  71. void Define(const Rect& rect)
  72. {
  73. min_ = rect.min_;
  74. max_ = rect.max_;
  75. defined_ = true;
  76. }
  77. /// Define from minimum and maximum vectors.
  78. void Define(const Vector2& min, const Vector2& max)
  79. {
  80. min_ = min;
  81. max_ = max;
  82. defined_ = true;
  83. }
  84. /// Define from a point.
  85. void Define(const Vector2& point)
  86. {
  87. min_ = max_ = point;
  88. defined_ = true;
  89. }
  90. /// Merge a point.
  91. void Merge(const Vector2& point)
  92. {
  93. if (!defined_)
  94. {
  95. min_ = max_ = point;
  96. defined_ = true;
  97. }
  98. if (point.x_ < min_.x_)
  99. min_.x_ = point.x_;
  100. if (point.x_ > max_.x_)
  101. max_.x_ = point.x_;
  102. if (point.y_ < min_.y_)
  103. min_.y_ = point.y_;
  104. if (point.y_ > max_.y_)
  105. max_.y_ = point.y_;
  106. }
  107. /// Merge a rect.
  108. void Merge(const Rect& rect)
  109. {
  110. if (!defined_)
  111. {
  112. min_ = rect.min_;
  113. max_ = rect.max_;
  114. defined_ = true;
  115. }
  116. if (rect.min_.x_ < min_.x_)
  117. min_.x_ = rect.min_.x_;
  118. if (rect.min_.y_ < min_.y_)
  119. min_.y_ = rect.min_.y_;
  120. if (rect.max_.x_ > max_.x_)
  121. max_.x_ = rect.max_.x_;
  122. if (rect.max_.y_ > max_.y_)
  123. max_.y_ = rect.max_.y_;
  124. }
  125. /// Clear to undefined state.
  126. void Clear()
  127. {
  128. min_ = Vector2::ZERO;
  129. max_ = Vector2::ZERO;
  130. defined_ = false;
  131. }
  132. /// Clip with another rect.
  133. void Clip(const Rect& rect);
  134. /// Return center.
  135. Vector2 Center() const { return (max_ + min_) * 0.5f; }
  136. /// Return size.
  137. Vector2 Size() const { return max_ - min_; }
  138. /// Return half-size.
  139. Vector2 HalfSize() const { return (max_ - min_) * 0.5f; }
  140. /// Test for equality with another rect with epsilon.
  141. bool Equals(const Rect& rhs) const { return min_.Equals(rhs.min_) && max_.Equals(rhs.max_); }
  142. /// Return float data.
  143. const void* Data() const { return &min_.x_; }
  144. /// Return as string.
  145. String ToString() const;
  146. /// Minimum vector.
  147. Vector2 min_;
  148. /// Maximum vector.
  149. Vector2 max_;
  150. /// Defined flag.
  151. bool defined_;
  152. /// Rect in the range (-1, -1) - (1, 1)
  153. static const Rect FULL;
  154. /// Rect in the range (0, 0) - (1, 1)
  155. static const Rect POSITIVE;
  156. /// Zero-sized rect.
  157. static const Rect ZERO;
  158. };
  159. /// Two-dimensional bounding rectangle with integer values.
  160. class IntRect
  161. {
  162. public:
  163. /// Construct an undefined rect.
  164. IntRect()
  165. {
  166. }
  167. /// Construct from coordinates.
  168. IntRect(int left, int top, int right, int bottom) :
  169. left_(left),
  170. top_(top),
  171. right_(right),
  172. bottom_(bottom)
  173. {
  174. }
  175. /// Test for equality with another rect.
  176. bool operator == (const IntRect& rhs) const { return left_ == rhs.left_ && top_ == rhs.top_ && right_ == rhs.right_ && bottom_ == rhs.bottom_; }
  177. /// Test for inequality with another rect.
  178. bool operator != (const IntRect& rhs) const { return left_ != rhs.left_ || top_ != rhs.top_ || right_ != rhs.right_ || bottom_ != rhs.bottom_; }
  179. /// Return size.
  180. IntVector2 Size() const { return IntVector2(Width(), Height()); }
  181. /// Return width.
  182. int Width() const { return right_ - left_; }
  183. /// Return height.
  184. int Height() const { return bottom_ - top_; }
  185. /// Return integer data.
  186. const int* Data() const { return &left_; }
  187. /// Return as string.
  188. String ToString() const;
  189. /// Left coordinate.
  190. int left_;
  191. /// Top coordinate.
  192. int top_;
  193. /// Right coordinate.
  194. int right_;
  195. /// Bottom coordinate.
  196. int bottom_;
  197. /// Zero-sized rect.
  198. static const IntRect ZERO;
  199. };
  200. }