Rect.h 7.7 KB

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