Rect.h 5.2 KB

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