Nirtcpp 2.1.0
Nirtcpp is a high-performance c++ graphics engine.
Loading...
Searching...
No Matches
rect.hpp
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 nirtcpp/nirtcpp.hpp
4
5#ifndef NIRT_RECT_HPP_INCLUDED
6#define NIRT_RECT_HPP_INCLUDED
7
8#include <nirtcpp/core/engine/nirt_types.hpp>
9#include <nirtcpp/core/engine/dimension2d.hpp>
10#include <nirtcpp/core/engine/position2d.hpp>
11
12namespace nirt
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
47 template <class U>
48 explicit rect(const dimension2d<U>& size)
49 : UpperLeftCorner(0,0), LowerRightCorner(size.Width, size.Height) {}
50
53 {
54 rect<T> ret(*this);
55 return ret+=pos;
56 }
57
60 {
61 UpperLeftCorner += pos;
62 LowerRightCorner += pos;
63 return *this;
64 }
65
68 {
69 rect<T> ret(*this);
70 return ret-=pos;
71 }
72
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 &&
85 LowerRightCorner == other.LowerRightCorner);
86 }
87
89 bool operator!=(const rect<T>& other) const
90 {
91 return (UpperLeftCorner != other.UpperLeftCorner ||
92 LowerRightCorner != other.LowerRightCorner);
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
132 {
133 if (other.LowerRightCorner.X < LowerRightCorner.X)
134 LowerRightCorner.X = other.LowerRightCorner.X;
135 if (other.LowerRightCorner.Y < LowerRightCorner.Y)
136 LowerRightCorner.Y = other.LowerRightCorner.Y;
137
138 if (other.UpperLeftCorner.X > LowerRightCorner.X)
139 LowerRightCorner.X = other.UpperLeftCorner.X;
140 if (other.UpperLeftCorner.Y > LowerRightCorner.Y)
141 LowerRightCorner.Y = other.UpperLeftCorner.Y;
142
143 if (other.LowerRightCorner.X < UpperLeftCorner.X)
144 UpperLeftCorner.X = other.LowerRightCorner.X;
145 if (other.LowerRightCorner.Y < UpperLeftCorner.Y)
146 UpperLeftCorner.Y = other.LowerRightCorner.Y;
147
148 if (other.UpperLeftCorner.X > UpperLeftCorner.X)
149 UpperLeftCorner.X = other.UpperLeftCorner.X;
150 if (other.UpperLeftCorner.Y > UpperLeftCorner.Y)
151 UpperLeftCorner.Y = other.UpperLeftCorner.Y;
152 }
153
155
157 {
158 if (other.getWidth() < getWidth() || other.getHeight() < getHeight())
159 return false;
160
161 T diff = other.LowerRightCorner.X - LowerRightCorner.X;
162 if (diff < 0)
163 {
166 }
167
168 diff = other.LowerRightCorner.Y - LowerRightCorner.Y;
169 if (diff < 0)
170 {
173 }
174
175 diff = UpperLeftCorner.X - other.UpperLeftCorner.X;
176 if (diff < 0)
177 {
180 }
181
182 diff = UpperLeftCorner.Y - other.UpperLeftCorner.Y;
183 if (diff < 0)
184 {
187 }
188
189 return true;
190 }
191
193 T getWidth() const
194 {
196 }
197
199 T getHeight() const
200 {
202 }
203
205 void repair()
206 {
208 {
209 T t = LowerRightCorner.X;
211 UpperLeftCorner.X = t;
212 }
213
215 {
216 T t = LowerRightCorner.Y;
218 UpperLeftCorner.Y = t;
219 }
220 }
221
223
225 bool isValid() const
226 {
227 return ((LowerRightCorner.X >= UpperLeftCorner.X) &&
229 }
230
233 {
234 return position2d<T>(
237 }
238
241 {
242 return dimension2d<T>(getWidth(), getHeight());
243 }
244
245
247
251 {
252 addInternalPoint(p.X, p.Y);
253 }
254
256
261 {
262 if (x>LowerRightCorner.X)
263 LowerRightCorner.X = x;
264 if (y>LowerRightCorner.Y)
265 LowerRightCorner.Y = y;
266
267 if (x<UpperLeftCorner.X)
268 UpperLeftCorner.X = x;
269 if (y<UpperLeftCorner.Y)
270 UpperLeftCorner.Y = y;
271 }
272
277 };
278
283
284} // end namespace core
285} // end namespace nirt
286
287#endif
Axis aligned bounding box in 3d dimensional space.
Definition aabbox3d.hpp:22
T getArea() const
Get the surface area of the box in squared units.
Definition aabbox3d.hpp:142
Rectangle template.
Definition rect.hpp:27
T getArea() const
Returns size of rectangle.
Definition rect.hpp:102
rect(const position2d< T > &upperLeft, const position2d< T > &lowerRight)
Constructor with two corners.
Definition rect.hpp:38
rect< T > & operator+=(const position2d< T > &pos)
move right by given numbers
Definition rect.hpp:59
rect(const position2d< T > &pos, const dimension2d< U > &size)
Constructor with upper left corner and dimension.
Definition rect.hpp:43
T getWidth() const
Get width of rectangle.
Definition rect.hpp:193
bool constrainTo(const rect< T > &other)
Moves this rectangle to fit inside another one.
Definition rect.hpp:156
dimension2d< T > getSize() const
Get the dimensions of the rectangle.
Definition rect.hpp:240
rect< T > & operator-=(const position2d< T > &pos)
move left by given numbers
Definition rect.hpp:74
rect(const dimension2d< U > &size)
Constructor with upper left at 0,0 and lower right using dimension.
Definition rect.hpp:48
rect< T > operator+(const position2d< T > &pos) const
move right by given numbers
Definition rect.hpp:52
rect()
Default constructor creating empty rectangle at (0,0)
Definition rect.hpp:31
bool isRectCollided(const rect< T > &other) const
Check if the rectangle collides with another rectangle.
Definition rect.hpp:121
T getHeight() const
Get height of rectangle.
Definition rect.hpp:199
rect< T > operator-(const position2d< T > &pos) const
move left by given numbers
Definition rect.hpp:67
void addInternalPoint(T x, T y)
Adds a point to the bounding rectangle.
Definition rect.hpp:260
void clipAgainst(const rect< T > &other)
Clips this rectangle with another one.
Definition rect.hpp:131
position2d< T > LowerRightCorner
Lower right corner.
Definition rect.hpp:276
void repair()
If the lower right corner of the rect is smaller then the upper left, the points are swapped.
Definition rect.hpp:205
bool isValid() const
Returns if the rect is valid to draw.
Definition rect.hpp:225
bool operator==(const rect< T > &other) const
equality operator
Definition rect.hpp:82
void addInternalPoint(const position2d< T > &p)
Adds a point to the rectangle.
Definition rect.hpp:250
position2d< T > UpperLeftCorner
Upper left corner.
Definition rect.hpp:274
rect(T x, T y, T x2, T y2)
Constructor with two corners.
Definition rect.hpp:34
bool operator<(const rect< T > &other) const
compares size of rectangles
Definition rect.hpp:96
bool isPointInside(const position2d< T > &pos) const
Returns if a 2d point is within this rectangle.
Definition rect.hpp:110
position2d< T > getCenter() const
Get the center of the rectangle.
Definition rect.hpp:232
bool operator!=(const rect< T > &other) const
inequality operator
Definition rect.hpp:89
As of Nirtcpp 1.6, position2d is a synonym for vector2d.
Definition vector3d.hpp:11

Nirtcpp    @cppfx.xyz

Utxcpp    utx::print