Saga3D API Documentation  1.0-RC4
rect.h
Go to the documentation of this file.
1 // Copyright (C) 2002-2012 Nikolaus Gebhardt
2 // This file is part of the "Irrlicht Engine".
3 // For conditions of distribution and use, see copyright notice in irrlicht.h
4 
5 #ifndef __IRR_RECT_H_INCLUDED__
6 #define __IRR_RECT_H_INCLUDED__
7 
8 #include <glm/vec2.hpp>
9 
10 namespace saga
11 {
12 namespace core
13 {
14 
16 
23  template <class T>
24  class rect
25  {
26  public:
27 
30 
32  rect(T x, T y, T x2, T y2)
33  : UpperLeftCorner(x,y), LowerRightCorner(x2,y2) {}
34 
36  rect(const glm::vec2& upperLeft, const glm::vec2& lowerRight)
37  : UpperLeftCorner(upperLeft), LowerRightCorner(lowerRight) {}
38 
40  template <class U>
41  rect(const glm::vec2& pos, const glm::vec2& size)
42  : UpperLeftCorner(pos), LowerRightCorner(pos.x + size.x, pos.y + size.y) {}
43 
45  template <class U>
46  explicit rect(const glm::vec2& size)
47  : UpperLeftCorner(0,0), LowerRightCorner(size.x, size.y) {}
48 
50  rect<T> operator+(const glm::vec2& pos) const
51  {
52  rect<T> ret(*this);
53  return ret+=pos;
54  }
55 
57  rect<T>& operator+=(const glm::vec2& pos)
58  {
59  UpperLeftCorner += pos;
60  LowerRightCorner += pos;
61  return *this;
62  }
63 
65  rect<T> operator-(const glm::vec2& pos) const
66  {
67  rect<T> ret(*this);
68  return ret-=pos;
69  }
70 
72  rect<T>& operator-=(const glm::vec2& pos)
73  {
74  UpperLeftCorner -= pos;
75  LowerRightCorner -= pos;
76  return *this;
77  }
78 
80  bool operator==(const rect<T>& other) const
81  {
82  return (UpperLeftCorner == other.UpperLeftCorner &&
84  }
85 
87  bool operator!=(const rect<T>& other) const
88  {
89  return (UpperLeftCorner != other.UpperLeftCorner ||
91  }
92 
94  bool operator<(const rect<T>& other) const
95  {
96  return getArea() < other.getArea();
97  }
98 
100  T getArea() const
101  {
102  return getWidth() * getHeight();
103  }
104 
106 
108  bool isPointInside(const glm::vec2& pos) const
109  {
110  return (UpperLeftCorner.x <= pos.x &&
111  UpperLeftCorner.y <= pos.y &&
112  LowerRightCorner.x >= pos.x &&
113  LowerRightCorner.y >= pos.y);
114  }
115 
117 
119  bool isRectCollided(const rect<T>& other) const
120  {
121  return (LowerRightCorner.y > other.UpperLeftCorner.y &&
122  UpperLeftCorner.y < other.LowerRightCorner.y &&
123  LowerRightCorner.x > other.UpperLeftCorner.x &&
124  UpperLeftCorner.x < other.LowerRightCorner.x);
125  }
126 
128 
129  void clipAgainst(const rect<T>& other)
130  {
131  if (other.LowerRightCorner.x < LowerRightCorner.x)
133  if (other.LowerRightCorner.y < LowerRightCorner.y)
135 
136  if (other.UpperLeftCorner.x > UpperLeftCorner.x)
137  UpperLeftCorner.x = other.UpperLeftCorner.x;
138  if (other.UpperLeftCorner.y > UpperLeftCorner.y)
139  UpperLeftCorner.y = other.UpperLeftCorner.y;
140 
141  // correct possible invalid rect resulting from clipping
146  }
147 
149 
150  bool constrainTo(const rect<T>& other)
151  {
152  if (other.getWidth() < getWidth() || other.getHeight() < getHeight())
153  return false;
154 
155  T diff = other.LowerRightCorner.x - LowerRightCorner.x;
156  if (diff < 0)
157  {
158  LowerRightCorner.x += diff;
159  UpperLeftCorner.x += diff;
160  }
161 
162  diff = other.LowerRightCorner.y - LowerRightCorner.y;
163  if (diff < 0)
164  {
165  LowerRightCorner.y += diff;
166  UpperLeftCorner.y += diff;
167  }
168 
169  diff = UpperLeftCorner.x - other.UpperLeftCorner.x;
170  if (diff < 0)
171  {
172  UpperLeftCorner.x -= diff;
173  LowerRightCorner.x -= diff;
174  }
175 
176  diff = UpperLeftCorner.y - other.UpperLeftCorner.y;
177  if (diff < 0)
178  {
179  UpperLeftCorner.y -= diff;
180  LowerRightCorner.y -= diff;
181  }
182 
183  return true;
184  }
185 
187  T getWidth() const
188  {
189  return LowerRightCorner.x - UpperLeftCorner.x;
190  }
191 
193  T getHeight() const
194  {
195  return LowerRightCorner.y - UpperLeftCorner.y;
196  }
197 
199  void repair()
200  {
202  {
203  T t = LowerRightCorner.x;
205  UpperLeftCorner.x = t;
206  }
207 
209  {
210  T t = LowerRightCorner.y;
212  UpperLeftCorner.y = t;
213  }
214  }
215 
217 
219  bool isValid() const
220  {
221  return ((LowerRightCorner.x >= UpperLeftCorner.x) &&
223  }
224 
226  glm::vec2 getCenter() const
227  {
228  return glm::vec2(
229  (UpperLeftCorner.x + LowerRightCorner.x) / 2,
230  (UpperLeftCorner.y + LowerRightCorner.y) / 2);
231  }
232 
234  glm::vec2 getSize() const
235  {
236  return glm::vec2(getWidth(), getHeight());
237  }
238 
239 
241 
244  void addInternalPoint(const glm::vec2& p)
245  {
246  addInternalPoint(p.x, p.y);
247  }
248 
250 
254  void addInternalPoint(T x, T y)
255  {
256  if (x>LowerRightCorner.x)
257  LowerRightCorner.x = x;
258  if (y>LowerRightCorner.y)
259  LowerRightCorner.y = y;
260 
261  if (x<UpperLeftCorner.x)
262  UpperLeftCorner.x = x;
263  if (y<UpperLeftCorner.y)
264  UpperLeftCorner.y = y;
265  }
266 
268  glm::vec2 UpperLeftCorner;
270  glm::vec2 LowerRightCorner;
271  };
272 
277 
278 } // namespace core
279 } // namespace saga
280 
281 #endif
282 
saga::core::rect::addInternalPoint
void addInternalPoint(T x, T y)
Adds a point to the bounding rectangle.
Definition: rect.h:254
saga::core::rectf
rect< float > rectf
Rectangle with float values.
Definition: rect.h:274
saga::core::rect::getArea
T getArea() const
Returns size of rectangle.
Definition: rect.h:100
saga::core::rect::getCenter
glm::vec2 getCenter() const
Get the center of the rectangle.
Definition: rect.h:226
saga::core::rect::operator!=
bool operator!=(const rect< T > &other) const
inequality operator
Definition: rect.h:87
saga::core::rect::rect
rect()
Default constructor creating empty rectangle at (0,0)
Definition: rect.h:29
saga::core::rect::rect
rect(const glm::vec2 &pos, const glm::vec2 &size)
Constructor with upper left corner and dimension.
Definition: rect.h:41
saga::core::rect::operator<
bool operator<(const rect< T > &other) const
compares size of rectangles
Definition: rect.h:94
saga::core::rect::addInternalPoint
void addInternalPoint(const glm::vec2 &p)
Adds a point to the rectangle.
Definition: rect.h:244
saga::core::rect::operator+
rect< T > operator+(const glm::vec2 &pos) const
move right by given numbers
Definition: rect.h:50
saga::core::rect::operator-
rect< T > operator-(const glm::vec2 &pos) const
move left by given numbers
Definition: rect.h:65
saga::core::rect::clipAgainst
void clipAgainst(const rect< T > &other)
Clips this rectangle with another one.
Definition: rect.h:129
saga::core::rect::repair
void repair()
If the lower right corner of the rect is smaller then the upper left, the points are swapped.
Definition: rect.h:199
saga::core::rect::isPointInside
bool isPointInside(const glm::vec2 &pos) const
Returns if a 2d point is within this rectangle.
Definition: rect.h:108
saga::core::rect::getWidth
T getWidth() const
Get width of rectangle.
Definition: rect.h:187
saga::core::rect::operator+=
rect< T > & operator+=(const glm::vec2 &pos)
move right by given numbers
Definition: rect.h:57
saga::core::rect::getSize
glm::vec2 getSize() const
Get the dimensions of the rectangle.
Definition: rect.h:234
saga::core::recti
rect< std::int32_t > recti
Rectangle with int values.
Definition: rect.h:276
saga::core::rect::operator-=
rect< T > & operator-=(const glm::vec2 &pos)
move left by given numbers
Definition: rect.h:72
saga::core::rect::rect
rect(const glm::vec2 &size)
Constructor with upper left at 0,0 and lower right using dimension.
Definition: rect.h:46
saga::core::rect::getHeight
T getHeight() const
Get height of rectangle.
Definition: rect.h:193
saga::core::rect::rect
rect(T x, T y, T x2, T y2)
Constructor with two corners.
Definition: rect.h:32
saga::core::rect::isValid
bool isValid() const
Returns if the rect is valid to draw.
Definition: rect.h:219
saga::core::rect::constrainTo
bool constrainTo(const rect< T > &other)
Moves this rectangle to fit inside another one.
Definition: rect.h:150
saga::core::rect::rect
rect(const glm::vec2 &upperLeft, const glm::vec2 &lowerRight)
Constructor with two corners.
Definition: rect.h:36
saga::core::rect::UpperLeftCorner
glm::vec2 UpperLeftCorner
Upper left corner.
Definition: rect.h:268
saga::core::rect
Rectangle template.
Definition: rect.h:24
saga::core::rect::LowerRightCorner
glm::vec2 LowerRightCorner
Lower right corner.
Definition: rect.h:270
saga::core::rect::operator==
bool operator==(const rect< T > &other) const
equality operator
Definition: rect.h:80
saga
Definition: aabbox3d.h:11
saga::core::rect::isRectCollided
bool isRectCollided(const rect< T > &other) const
Check if the rectangle collides with another rectangle.
Definition: rect.h:119