Irrlicht 3D Engine
 
Loading...
Searching...
No Matches
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
12namespace irr
13{
14namespace 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)
36
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
48 {
49 rect<T> ret(*this);
50 return ret+=pos;
51 }
52
55 {
56 UpperLeftCorner += pos;
57 LowerRightCorner += pos;
58 return *this;
59 }
60
63 {
64 rect<T> ret(*this);
65 return ret-=pos;
66 }
67
70 {
71 UpperLeftCorner -= pos;
72 LowerRightCorner -= pos;
73 return *this;
74 }
75
77 bool operator==(const rect<T>& other) const
78 {
79 return (UpperLeftCorner == other.UpperLeftCorner &&
80 LowerRightCorner == other.LowerRightCorner);
81 }
82
84 bool operator!=(const rect<T>& other) const
85 {
86 return (UpperLeftCorner != other.UpperLeftCorner ||
87 LowerRightCorner != other.LowerRightCorner);
88 }
89
91 bool operator<(const rect<T>& other) const
92 {
93 return getArea() < other.getArea();
94 }
95
97 T getArea() const
98 {
99 return getWidth() * getHeight();
100 }
101
103
105 bool isPointInside(const position2d<T>& pos) const
106 {
107 return (UpperLeftCorner.X <= pos.X &&
108 UpperLeftCorner.Y <= pos.Y &&
109 LowerRightCorner.X >= pos.X &&
110 LowerRightCorner.Y >= pos.Y);
111 }
112
114
116 bool isRectCollided(const rect<T>& other) const
117 {
118 return (LowerRightCorner.Y > other.UpperLeftCorner.Y &&
119 UpperLeftCorner.Y < other.LowerRightCorner.Y &&
120 LowerRightCorner.X > other.UpperLeftCorner.X &&
121 UpperLeftCorner.X < other.LowerRightCorner.X);
122 }
123
125
127 {
128 if (other.LowerRightCorner.X < LowerRightCorner.X)
129 LowerRightCorner.X = other.LowerRightCorner.X;
130 if (other.LowerRightCorner.Y < LowerRightCorner.Y)
131 LowerRightCorner.Y = other.LowerRightCorner.Y;
132
133 if (other.UpperLeftCorner.X > UpperLeftCorner.X)
134 UpperLeftCorner.X = other.UpperLeftCorner.X;
135 if (other.UpperLeftCorner.Y > UpperLeftCorner.Y)
136 UpperLeftCorner.Y = other.UpperLeftCorner.Y;
137
138 // correct possible invalid rect resulting from clipping
143 }
144
146
148 {
149 if (other.getWidth() < getWidth() || other.getHeight() < getHeight())
150 return false;
151
152 T diff = other.LowerRightCorner.X - LowerRightCorner.X;
153 if (diff < 0)
154 {
157 }
158
159 diff = other.LowerRightCorner.Y - LowerRightCorner.Y;
160 if (diff < 0)
161 {
164 }
165
166 diff = UpperLeftCorner.X - other.UpperLeftCorner.X;
167 if (diff < 0)
168 {
171 }
172
173 diff = UpperLeftCorner.Y - other.UpperLeftCorner.Y;
174 if (diff < 0)
175 {
178 }
179
180 return true;
181 }
182
184 T getWidth() const
185 {
187 }
188
190 T getHeight() const
191 {
193 }
194
196 void repair()
197 {
199 {
200 T t = LowerRightCorner.X;
202 UpperLeftCorner.X = t;
203 }
204
206 {
207 T t = LowerRightCorner.Y;
209 UpperLeftCorner.Y = t;
210 }
211 }
212
214
216 bool isValid() const
217 {
218 return ((LowerRightCorner.X >= UpperLeftCorner.X) &&
220 }
221
224 {
225 return position2d<T>(
228 }
229
232 {
233 return dimension2d<T>(getWidth(), getHeight());
234 }
235
236
238
242 {
243 addInternalPoint(p.X, p.Y);
244 }
245
247
252 {
253 if (x>LowerRightCorner.X)
254 LowerRightCorner.X = x;
255 if (y>LowerRightCorner.Y)
256 LowerRightCorner.Y = y;
257
258 if (x<UpperLeftCorner.X)
259 UpperLeftCorner.X = x;
260 if (y<UpperLeftCorner.Y)
261 UpperLeftCorner.Y = y;
262 }
263
268 };
269
274
275} // end namespace core
276} // end namespace irr
277
278#endif
279
Axis aligned bounding box in 3d dimensional space.
Definition aabbox3d.h:22
T getArea() const
Get the surface area of the box in squared units.
Definition aabbox3d.h:134
Rectangle template.
Definition rect.h:27
rect()
Default constructor creating empty rectangle at (0,0)
Definition rect.h:31
rect< T > operator+(const position2d< T > &pos) const
move right by given numbers
Definition rect.h:47
void addInternalPoint(const position2d< T > &p)
Adds a point to the rectangle.
Definition rect.h:241
T getArea() const
Returns size of rectangle.
Definition rect.h:97
void addInternalPoint(T x, T y)
Adds a point to the bounding rectangle.
Definition rect.h:251
dimension2d< T > getSize() const
Get the dimensions of the rectangle.
Definition rect.h:231
bool operator==(const rect< T > &other) const
equality operator
Definition rect.h:77
bool isPointInside(const position2d< T > &pos) const
Returns if a 2d point is within this rectangle.
Definition rect.h:105
bool constrainTo(const rect< T > &other)
Moves this rectangle to fit inside another one.
Definition rect.h:147
void repair()
If the lower right corner of the rect is smaller then the upper left, the points are swapped.
Definition rect.h:196
rect(const position2d< T > &pos, const dimension2d< U > &size)
Constructor with upper left corner and dimension.
Definition rect.h:43
void clipAgainst(const rect< T > &other)
Clips this rectangle with another one.
Definition rect.h:126
bool operator<(const rect< T > &other) const
compares size of rectangles
Definition rect.h:91
rect(const position2d< T > &upperLeft, const position2d< T > &lowerRight)
Constructor with two corners.
Definition rect.h:38
position2d< T > LowerRightCorner
Lower right corner.
Definition rect.h:267
rect< T > & operator-=(const position2d< T > &pos)
move left by given numbers
Definition rect.h:69
bool isRectCollided(const rect< T > &other) const
Check if the rectangle collides with another rectangle.
Definition rect.h:116
position2d< T > UpperLeftCorner
Upper left corner.
Definition rect.h:265
bool isValid() const
Returns if the rect is valid to draw.
Definition rect.h:216
T getHeight() const
Get height of rectangle.
Definition rect.h:190
T getWidth() const
Get width of rectangle.
Definition rect.h:184
bool operator!=(const rect< T > &other) const
inequality operator
Definition rect.h:84
rect< T > operator-(const position2d< T > &pos) const
move left by given numbers
Definition rect.h:62
rect(T x, T y, T x2, T y2)
Constructor with two corners.
Definition rect.h:34
rect< T > & operator+=(const position2d< T > &pos)
move right by given numbers
Definition rect.h:54
position2d< T > getCenter() const
Get the center of the rectangle.
Definition rect.h:223
rect< s32 > recti
Rectangle with int values.
Definition rect.h:273
rect< f32 > rectf
Rectangle with float values.
Definition rect.h:271
Everything in the Irrlicht Engine can be found in this namespace.
Definition aabbox3d.h:13