Rect.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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 "Vector4.h"
  24. namespace Urho3D
  25. {
  26. /// Two-dimensional bounding rectangle.
  27. class URHO3D_API 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. /// Construct from a Vector4.
  59. Rect(const Vector4& vector) :
  60. min_(vector.x_, vector.y_),
  61. max_(vector.z_, vector.w_),
  62. defined_(true)
  63. {
  64. }
  65. /// Assign from another rect.
  66. Rect& operator = (const Rect& rhs)
  67. {
  68. min_ = rhs.min_;
  69. max_ = rhs.max_;
  70. defined_ = rhs.defined_;
  71. return *this;
  72. }
  73. /// Test for equality with another rect.
  74. bool operator == (const Rect& rhs) const { return min_ == rhs.min_ && max_ == rhs.max_; }
  75. /// Test for inequality with another rect.
  76. bool operator != (const Rect& rhs) const { return min_ != rhs.min_ || max_ != rhs.max_; }
  77. /// Define from another rect.
  78. void Define(const Rect& rect)
  79. {
  80. min_ = rect.min_;
  81. max_ = rect.max_;
  82. defined_ = true;
  83. }
  84. /// Define from minimum and maximum vectors.
  85. void Define(const Vector2& min, const Vector2& max)
  86. {
  87. min_ = min;
  88. max_ = max;
  89. defined_ = true;
  90. }
  91. /// Define from a point.
  92. void Define(const Vector2& point)
  93. {
  94. min_ = max_ = point;
  95. defined_ = true;
  96. }
  97. /// Merge a point.
  98. void Merge(const Vector2& point)
  99. {
  100. if (!defined_)
  101. {
  102. min_ = max_ = point;
  103. defined_ = true;
  104. }
  105. if (point.x_ < min_.x_)
  106. min_.x_ = point.x_;
  107. if (point.x_ > max_.x_)
  108. max_.x_ = point.x_;
  109. if (point.y_ < min_.y_)
  110. min_.y_ = point.y_;
  111. if (point.y_ > max_.y_)
  112. max_.y_ = point.y_;
  113. }
  114. /// Merge a rect.
  115. void Merge(const Rect& rect)
  116. {
  117. if (!defined_)
  118. {
  119. min_ = rect.min_;
  120. max_ = rect.max_;
  121. defined_ = true;
  122. }
  123. if (rect.min_.x_ < min_.x_)
  124. min_.x_ = rect.min_.x_;
  125. if (rect.min_.y_ < min_.y_)
  126. min_.y_ = rect.min_.y_;
  127. if (rect.max_.x_ > max_.x_)
  128. max_.x_ = rect.max_.x_;
  129. if (rect.max_.y_ > max_.y_)
  130. max_.y_ = rect.max_.y_;
  131. }
  132. /// Clear to undefined state.
  133. void Clear()
  134. {
  135. min_ = Vector2::ZERO;
  136. max_ = Vector2::ZERO;
  137. defined_ = false;
  138. }
  139. /// Clip with another rect.
  140. void Clip(const Rect& rect);
  141. /// Return center.
  142. Vector2 Center() const { return (max_ + min_) * 0.5f; }
  143. /// Return size.
  144. Vector2 Size() const { return max_ - min_; }
  145. /// Return half-size.
  146. Vector2 HalfSize() const { return (max_ - min_) * 0.5f; }
  147. /// Test for equality with another rect with epsilon.
  148. bool Equals(const Rect& rhs) const { return min_.Equals(rhs.min_) && max_.Equals(rhs.max_); }
  149. /// Return float data.
  150. const void* Data() const { return &min_.x_; }
  151. /// Return as a vector.
  152. Vector4 ToVector4() const { return Vector4(min_.x_, min_.y_, max_.x_, max_.y_); }
  153. /// Return as string.
  154. String ToString() const;
  155. /// Minimum vector.
  156. Vector2 min_;
  157. /// Maximum vector.
  158. Vector2 max_;
  159. /// Defined flag.
  160. bool defined_;
  161. /// Rect in the range (-1, -1) - (1, 1)
  162. static const Rect FULL;
  163. /// Rect in the range (0, 0) - (1, 1)
  164. static const Rect POSITIVE;
  165. /// Zero-sized rect.
  166. static const Rect ZERO;
  167. };
  168. /// Two-dimensional bounding rectangle with integer values.
  169. class URHO3D_API IntRect
  170. {
  171. public:
  172. /// Construct an undefined rect.
  173. IntRect()
  174. {
  175. }
  176. /// Construct from coordinates.
  177. IntRect(int left, int top, int right, int bottom) :
  178. left_(left),
  179. top_(top),
  180. right_(right),
  181. bottom_(bottom)
  182. {
  183. }
  184. /// Construct from an int array.
  185. IntRect(const int* data) :
  186. left_(data[0]),
  187. top_(data[1]),
  188. right_(data[2]),
  189. bottom_(data[3])
  190. {
  191. }
  192. /// Test for equality with another rect.
  193. bool operator == (const IntRect& rhs) const { return left_ == rhs.left_ && top_ == rhs.top_ && right_ == rhs.right_ && bottom_ == rhs.bottom_; }
  194. /// Test for inequality with another rect.
  195. bool operator != (const IntRect& rhs) const { return left_ != rhs.left_ || top_ != rhs.top_ || right_ != rhs.right_ || bottom_ != rhs.bottom_; }
  196. /// Return size.
  197. IntVector2 Size() const { return IntVector2(Width(), Height()); }
  198. /// Return width.
  199. int Width() const { return right_ - left_; }
  200. /// Return height.
  201. int Height() const { return bottom_ - top_; }
  202. /// Return integer data.
  203. const int* Data() const { return &left_; }
  204. /// Return as string.
  205. String ToString() const;
  206. /// Left coordinate.
  207. int left_;
  208. /// Top coordinate.
  209. int top_;
  210. /// Right coordinate.
  211. int right_;
  212. /// Bottom coordinate.
  213. int bottom_;
  214. /// Zero-sized rect.
  215. static const IntRect ZERO;
  216. };
  217. }