Rect.pkg 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. $#include "Rect.h"
  2. /// Two-dimensional bounding rectangle.
  3. class Rect
  4. {
  5. public:
  6. /// Construct an undefined rect.
  7. Rect() :
  8. min_(Vector2::ZERO),
  9. max_(Vector2::ZERO),
  10. defined_(false)
  11. {
  12. }
  13. /// Copy-construct from another rect.
  14. Rect(const Rect& rect) :
  15. min_(rect.min_),
  16. max_(rect.max_),
  17. defined_(rect.defined_)
  18. {
  19. }
  20. /// Construct from minimum and maximum vectors.
  21. Rect(const Vector2& min, const Vector2& max) :
  22. min_(min),
  23. max_(max),
  24. defined_(true)
  25. {
  26. }
  27. /// Construct from coordinates.
  28. Rect(float left, float top, float right, float bottom) :
  29. min_(left, top),
  30. max_(right, bottom),
  31. defined_(true)
  32. {
  33. }
  34. /// Construct from a Vector4.
  35. Rect(const Vector4& vector) :
  36. min_(vector.x_, vector.y_),
  37. max_(vector.z_, vector.w_),
  38. defined_(true)
  39. {
  40. }
  41. /// Test for equality with another rect.
  42. bool operator == (const Rect& rhs) const { return min_ == rhs.min_ && max_ == rhs.max_; }
  43. /// Define from another rect.
  44. void Define(const Rect& rect)
  45. {
  46. min_ = rect.min_;
  47. max_ = rect.max_;
  48. defined_ = true;
  49. }
  50. /// Define from minimum and maximum vectors.
  51. void Define(const Vector2& min, const Vector2& max)
  52. {
  53. min_ = min;
  54. max_ = max;
  55. defined_ = true;
  56. }
  57. /// Define from a point.
  58. void Define(const Vector2& point)
  59. {
  60. min_ = max_ = point;
  61. defined_ = true;
  62. }
  63. /// Merge a point.
  64. void Merge(const Vector2& point)
  65. {
  66. if (!defined_)
  67. {
  68. min_ = max_ = point;
  69. defined_ = true;
  70. }
  71. if (point.x_ < min_.x_)
  72. min_.x_ = point.x_;
  73. if (point.x_ > max_.x_)
  74. max_.x_ = point.x_;
  75. if (point.y_ < min_.y_)
  76. min_.y_ = point.y_;
  77. if (point.y_ > max_.y_)
  78. max_.y_ = point.y_;
  79. }
  80. /// Merge a rect.
  81. void Merge(const Rect& rect)
  82. {
  83. if (!defined_)
  84. {
  85. min_ = rect.min_;
  86. max_ = rect.max_;
  87. defined_ = true;
  88. }
  89. if (rect.min_.x_ < min_.x_)
  90. min_.x_ = rect.min_.x_;
  91. if (rect.min_.y_ < min_.y_)
  92. min_.y_ = rect.min_.y_;
  93. if (rect.max_.x_ > max_.x_)
  94. max_.x_ = rect.max_.x_;
  95. if (rect.max_.y_ > max_.y_)
  96. max_.y_ = rect.max_.y_;
  97. }
  98. /// Clear to undefined state.
  99. void Clear()
  100. {
  101. min_ = Vector2::ZERO;
  102. max_ = Vector2::ZERO;
  103. defined_ = false;
  104. }
  105. /// Clip with another rect.
  106. void Clip(const Rect& rect);
  107. /// Return center.
  108. Vector2 Center() const { return (max_ + min_) * 0.5f; }
  109. /// Return size.
  110. Vector2 Size() const { return max_ - min_; }
  111. /// Return half-size.
  112. Vector2 HalfSize() const { return (max_ - min_) * 0.5f; }
  113. /// Test for equality with another rect with epsilon.
  114. bool Equals(const Rect& rhs) const { return min_.Equals(rhs.min_) && max_.Equals(rhs.max_); }
  115. /// Return as a vector.
  116. Vector4 ToVector4() const { return Vector4(min_.x_, min_.y_, max_.x_, max_.y_); }
  117. /// Return as string.
  118. String ToString() const;
  119. /// Minimum vector.
  120. Vector2 min_ @ min;
  121. /// Maximum vector.
  122. Vector2 max_ @ max;
  123. /// Rect in the range (-1, -1) - (1, 1)
  124. static const Rect FULL;
  125. /// Rect in the range (0, 0) - (1, 1)
  126. static const Rect POSITIVE;
  127. /// Zero-sized rect.
  128. static const Rect ZERO;
  129. };
  130. /// Two-dimensional bounding rectangle with integer values.
  131. class IntRect
  132. {
  133. public:
  134. /// Construct an undefined rect.
  135. IntRect();
  136. /// Construct from coordinates.
  137. IntRect(int left, int top, int right, int bottom);
  138. /// Test for equality with another rect.
  139. bool operator == (const IntRect& rhs) const;
  140. /// Return size.
  141. IntVector2 Size() const;
  142. /// Return width.
  143. int Width() const;
  144. /// Return height.
  145. int Height() const;
  146. /// Left coordinate.
  147. int left_ @ left;
  148. /// Top coordinate.
  149. int top_ @ top;
  150. /// Right coordinate.
  151. int right_ @ right;
  152. /// Bottom coordinate.
  153. int bottom_ @ bottom;
  154. /// Zero-sized rect.
  155. static const IntRect ZERO;
  156. };