Rect.h 6.4 KB

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