Irrlicht 3D Engine
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 "irrTypes.h"
9 #include "dimension2d.h"
10 #include "position2d.h"
11 
12 namespace irr
13 {
14 namespace core
15 {
16 
18 
25  template <class T>
26  class rect
27  {
28  public:
29 
32 
34  rect(T x, T y, T x2, T y2)
35  : UpperLeftCorner(x,y), LowerRightCorner(x2,y2) {}
36 
38  rect(const position2d<T>& upperLeft, const position2d<T>& lowerRight)
39  : UpperLeftCorner(upperLeft), LowerRightCorner(lowerRight) {}
40 
42  template <class U>
43  rect(const position2d<T>& pos, const dimension2d<U>& size)
44  : UpperLeftCorner(pos), LowerRightCorner(pos.X + size.Width, pos.Y + size.Height) {}
45 
47  template <class U>
48  explicit rect(const dimension2d<U>& size)
49  : UpperLeftCorner(0,0), LowerRightCorner(size.Width, size.Height) {}
50 
52  rect<T> operator+(const position2d<T>& pos) const
53  {
54  rect<T> ret(*this);
55  return ret+=pos;
56  }
57 
59  rect<T>& operator+=(const position2d<T>& pos)
60  {
61  UpperLeftCorner += pos;
62  LowerRightCorner += pos;
63  return *this;
64  }
65 
67  rect<T> operator-(const position2d<T>& pos) const
68  {
69  rect<T> ret(*this);
70  return ret-=pos;
71  }
72 
74  rect<T>& operator-=(const position2d<T>& pos)
75  {
76  UpperLeftCorner -= pos;
77  LowerRightCorner -= pos;
78  return *this;
79  }
80 
82  bool operator==(const rect<T>& other) const
83  {
84  return (UpperLeftCorner == other.UpperLeftCorner &&
86  }
87 
89  bool operator!=(const rect<T>& other) const
90  {
91  return (UpperLeftCorner != other.UpperLeftCorner ||
93  }
94 
96  bool operator<(const rect<T>& other) const
97  {
98  return getArea() < other.getArea();
99  }
100 
102  T getArea() const
103  {
104  return getWidth() * getHeight();
105  }
106 
108 
110  bool isPointInside(const position2d<T>& pos) const
111  {
112  return (UpperLeftCorner.X <= pos.X &&
113  UpperLeftCorner.Y <= pos.Y &&
114  LowerRightCorner.X >= pos.X &&
115  LowerRightCorner.Y >= pos.Y);
116  }
117 
119 
121  bool isRectCollided(const rect<T>& other) const
122  {
123  return (LowerRightCorner.Y > other.UpperLeftCorner.Y &&
124  UpperLeftCorner.Y < other.LowerRightCorner.Y &&
125  LowerRightCorner.X > other.UpperLeftCorner.X &&
126  UpperLeftCorner.X < other.LowerRightCorner.X);
127  }
128 
130 
131  void clipAgainst(const rect<T>& other)
132  {
133  if (other.LowerRightCorner.X < LowerRightCorner.X)
135  if (other.LowerRightCorner.Y < LowerRightCorner.Y)
137 
138  if (other.UpperLeftCorner.X > UpperLeftCorner.X)
139  UpperLeftCorner.X = other.UpperLeftCorner.X;
140  if (other.UpperLeftCorner.Y > UpperLeftCorner.Y)
141  UpperLeftCorner.Y = other.UpperLeftCorner.Y;
142 
143  // correct possible invalid rect resulting from clipping
148  }
149 
151 
152  bool constrainTo(const rect<T>& other)
153  {
154  if (other.getWidth() < getWidth() || other.getHeight() < getHeight())
155  return false;
156 
157  T diff = other.LowerRightCorner.X - LowerRightCorner.X;
158  if (diff < 0)
159  {
160  LowerRightCorner.X += diff;
161  UpperLeftCorner.X += diff;
162  }
163 
164  diff = other.LowerRightCorner.Y - LowerRightCorner.Y;
165  if (diff < 0)
166  {
167  LowerRightCorner.Y += diff;
168  UpperLeftCorner.Y += diff;
169  }
170 
171  diff = UpperLeftCorner.X - other.UpperLeftCorner.X;
172  if (diff < 0)
173  {
174  UpperLeftCorner.X -= diff;
175  LowerRightCorner.X -= diff;
176  }
177 
178  diff = UpperLeftCorner.Y - other.UpperLeftCorner.Y;
179  if (diff < 0)
180  {
181  UpperLeftCorner.Y -= diff;
182  LowerRightCorner.Y -= diff;
183  }
184 
185  return true;
186  }
187 
189  T getWidth() const
190  {
191  return LowerRightCorner.X - UpperLeftCorner.X;
192  }
193 
195  T getHeight() const
196  {
197  return LowerRightCorner.Y - UpperLeftCorner.Y;
198  }
199 
201  void repair()
202  {
204  {
205  T t = LowerRightCorner.X;
207  UpperLeftCorner.X = t;
208  }
209 
211  {
212  T t = LowerRightCorner.Y;
214  UpperLeftCorner.Y = t;
215  }
216  }
217 
219 
221  bool isValid() const
222  {
223  return ((LowerRightCorner.X >= UpperLeftCorner.X) &&
225  }
226 
228  position2d<T> getCenter() const
229  {
230  return position2d<T>(
231  (UpperLeftCorner.X + LowerRightCorner.X) / 2,
232  (UpperLeftCorner.Y + LowerRightCorner.Y) / 2);
233  }
234 
237  {
238  return dimension2d<T>(getWidth(), getHeight());
239  }
240 
241 
243 
246  void addInternalPoint(const position2d<T>& p)
247  {
248  addInternalPoint(p.X, p.Y);
249  }
250 
252 
256  void addInternalPoint(T x, T y)
257  {
258  if (x>LowerRightCorner.X)
259  LowerRightCorner.X = x;
260  if (y>LowerRightCorner.Y)
261  LowerRightCorner.Y = y;
262 
263  if (x<UpperLeftCorner.X)
264  UpperLeftCorner.X = x;
265  if (y<UpperLeftCorner.Y)
266  UpperLeftCorner.Y = y;
267  }
268 
270  position2d<T> UpperLeftCorner;
272  position2d<T> LowerRightCorner;
273  };
274 
276  typedef rect<f32> rectf;
278  typedef rect<s32> recti;
279 
280 } // end namespace core
281 } // end namespace irr
282 
283 #endif
284 
rect(T x, T y, T x2, T y2)
Constructor with two corners.
Definition: rect.h:34
Rectangle template.
Definition: rect.h:26
rect(const position2d< T > &pos, const dimension2d< U > &size)
Constructor with upper left corner and dimension.
Definition: rect.h:43
rect()
Default constructor creating empty rectangle at (0,0)
Definition: rect.h:31
rect< s32 > recti
Rectangle with int values.
Definition: rect.h:278
bool operator!=(const rect< T > &other) const
inequality operator
Definition: rect.h:89
bool operator<(const rect< T > &other) const
compares size of rectangles
Definition: rect.h:96
position2d< T > UpperLeftCorner
Upper left corner.
Definition: rect.h:270
bool isValid() const
Returns if the rect is valid to draw.
Definition: rect.h:221
Everything in the Irrlicht Engine can be found in this namespace.
Definition: aabbox3d.h:12
Specifies a 2 dimensional size.
Definition: dimension2d.h:20
bool constrainTo(const rect< T > &other)
Moves this rectangle to fit inside another one.
Definition: rect.h:152
position2d< T > LowerRightCorner
Lower right corner.
Definition: rect.h:272
rect< T > operator+(const position2d< T > &pos) const
move right by given numbers
Definition: rect.h:52
bool isRectCollided(const rect< T > &other) const
Check if the rectangle collides with another rectangle.
Definition: rect.h:121
dimension2d< T > getSize() const
Get the dimensions of the rectangle.
Definition: rect.h:236
T getHeight() const
Get height of rectangle.
Definition: rect.h:195
rect< T > & operator-=(const position2d< T > &pos)
move left by given numbers
Definition: rect.h:74
T getWidth() const
Get width of rectangle.
Definition: rect.h:189
position2d< T > getCenter() const
Get the center of the rectangle.
Definition: rect.h:228
bool isPointInside(const position2d< T > &pos) const
Returns if a 2d point is within this rectangle.
Definition: rect.h:110
rect(const position2d< T > &upperLeft, const position2d< T > &lowerRight)
Constructor with two corners.
Definition: rect.h:38
void clipAgainst(const rect< T > &other)
Clips this rectangle with another one.
Definition: rect.h:131
void addInternalPoint(const position2d< T > &p)
Adds a point to the rectangle.
Definition: rect.h:246
rect< T > & operator+=(const position2d< T > &pos)
move right by given numbers
Definition: rect.h:59
void addInternalPoint(T x, T y)
Adds a point to the bounding rectangle.
Definition: rect.h:256
rect< T > operator-(const position2d< T > &pos) const
move left by given numbers
Definition: rect.h:67
bool operator==(const rect< T > &other) const
equality operator
Definition: rect.h:82
rect< f32 > rectf
Rectangle with float values.
Definition: rect.h:276
rect(const dimension2d< U > &size)
Constructor with upper left at 0,0 and lower right using dimension.
Definition: rect.h:48
void repair()
If the lower right corner of the rect is smaller then the upper left, the points are swapped.
Definition: rect.h:201
T getArea() const
Returns size of rectangle.
Definition: rect.h:102