arsa  2.7
Public Member Functions | Public Attributes | List of all members
irr::core::rect< T > Class Template Reference

Rectangle template. More...

#include <rect.h>

Public Member Functions

 rect ()
 Default constructor creating empty rectangle at (0,0) More...
 
 rect (T x, T y, T x2, T y2)
 Constructor with two corners. More...
 
 rect (const position2d< T > &upperLeft, const position2d< T > &lowerRight)
 Constructor with two corners. More...
 
template<class U >
 rect (const position2d< T > &pos, const dimension2d< U > &size)
 Constructor with upper left corner and dimension. More...
 
template<class U >
 rect (const dimension2d< U > &size)
 Constructor with upper left at 0,0 and lower right using dimension. More...
 
rect< T > operator+ (const position2d< T > &pos) const
 move right by given numbers More...
 
rect< T > & operator+= (const position2d< T > &pos)
 move right by given numbers More...
 
rect< T > operator- (const position2d< T > &pos) const
 move left by given numbers More...
 
rect< T > & operator-= (const position2d< T > &pos)
 move left by given numbers More...
 
bool operator== (const rect< T > &other) const
 equality operator More...
 
bool operator!= (const rect< T > &other) const
 inequality operator More...
 
bool operator< (const rect< T > &other) const
 compares size of rectangles More...
 
getArea () const
 Returns size of rectangle. More...
 
bool isPointInside (const position2d< T > &pos) const
 Returns if a 2d point is within this rectangle. More...
 
bool isRectCollided (const rect< T > &other) const
 Check if the rectangle collides with another rectangle. More...
 
void clipAgainst (const rect< T > &other)
 Clips this rectangle with another one. More...
 
bool constrainTo (const rect< T > &other)
 Moves this rectangle to fit inside another one. More...
 
getWidth () const
 Get width of rectangle. More...
 
getHeight () const
 Get height of rectangle. More...
 
void repair ()
 If the lower right corner of the rect is smaller then the upper left, the points are swapped. More...
 
bool isValid () const
 Returns if the rect is valid to draw. More...
 
position2d< T > getCenter () const
 Get the center of the rectangle. More...
 
dimension2d< T > getSize () const
 Get the dimensions of the rectangle. More...
 
void addInternalPoint (const position2d< T > &p)
 Adds a point to the rectangle. More...
 
void addInternalPoint (T x, T y)
 Adds a point to the bounding rectangle. More...
 

Public Attributes

position2d< T > UpperLeftCorner
 Upper left corner. More...
 
position2d< T > LowerRightCorner
 Lower right corner. More...
 

Detailed Description

template<class T>
class irr::core::rect< T >

Rectangle template.

Mostly used by 2D GUI elements and for 2D drawing methods. It has 2 positions instead of position and dimension and a fast method for collision detection with other rectangles and points.

Coordinates are (0,0) for top-left corner, and increasing to the right and to the bottom.

Definition at line 26 of file rect.h.

Constructor & Destructor Documentation

◆ rect() [1/5]

template<class T>
irr::core::rect< T >::rect ( )
inline

Default constructor creating empty rectangle at (0,0)

Definition at line 31 of file rect.h.

31 : UpperLeftCorner(0,0), LowerRightCorner(0,0) {}
position2d< T > UpperLeftCorner
Upper left corner.
Definition: rect.h:270
position2d< T > LowerRightCorner
Lower right corner.
Definition: rect.h:272

◆ rect() [2/5]

template<class T>
irr::core::rect< T >::rect ( x,
y,
x2,
y2 
)
inline

Constructor with two corners.

Definition at line 34 of file rect.h.

GLfixed GLfixed GLfixed y2
position2d< T > UpperLeftCorner
Upper left corner.
Definition: rect.h:270
GLfixed GLfixed x2
position2d< T > LowerRightCorner
Lower right corner.
Definition: rect.h:272
GLint GLint GLint GLint GLint GLint y
Definition: SDL_opengl.h:1574
GLint GLint GLint GLint GLint x
Definition: SDL_opengl.h:1574

◆ rect() [3/5]

template<class T>
irr::core::rect< T >::rect ( const position2d< T > &  upperLeft,
const position2d< T > &  lowerRight 
)
inline

Constructor with two corners.

Definition at line 38 of file rect.h.

39  : UpperLeftCorner(upperLeft), LowerRightCorner(lowerRight) {}
position2d< T > UpperLeftCorner
Upper left corner.
Definition: rect.h:270
position2d< T > LowerRightCorner
Lower right corner.
Definition: rect.h:272

◆ rect() [4/5]

template<class T>
template<class U >
irr::core::rect< T >::rect ( const position2d< T > &  pos,
const dimension2d< U > &  size 
)
inline

Constructor with upper left corner and dimension.

Definition at line 43 of file rect.h.

44  : UpperLeftCorner(pos), LowerRightCorner(pos.X + size.Width, pos.Y + size.Height) {}
position2d< T > UpperLeftCorner
Upper left corner.
Definition: rect.h:270
GLsizeiptr size
position2d< T > LowerRightCorner
Lower right corner.
Definition: rect.h:272

◆ rect() [5/5]

template<class T>
template<class U >
irr::core::rect< T >::rect ( const dimension2d< U > &  size)
inlineexplicit

Constructor with upper left at 0,0 and lower right using dimension.

Definition at line 48 of file rect.h.

49  : UpperLeftCorner(0,0), LowerRightCorner(size.Width, size.Height) {}
position2d< T > UpperLeftCorner
Upper left corner.
Definition: rect.h:270
GLsizeiptr size
position2d< T > LowerRightCorner
Lower right corner.
Definition: rect.h:272

Member Function Documentation

◆ addInternalPoint() [1/2]

template<class T>
void irr::core::rect< T >::addInternalPoint ( const position2d< T > &  p)
inline

Adds a point to the rectangle.

Causes the rectangle to grow bigger if point is outside of the box

Parameters
pPoint to add to the box.

Definition at line 246 of file rect.h.

247  {
248  addInternalPoint(p.X, p.Y);
249  }
GLfloat GLfloat p
void addInternalPoint(const position2d< T > &p)
Adds a point to the rectangle.
Definition: rect.h:246

◆ addInternalPoint() [2/2]

template<class T>
void irr::core::rect< T >::addInternalPoint ( x,
y 
)
inline

Adds a point to the bounding rectangle.

Causes the rectangle to grow bigger if point is outside of the box

Parameters
xX-Coordinate of the point to add to this box.
yY-Coordinate of the point to add to this box.

Definition at line 256 of file rect.h.

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  }
position2d< T > UpperLeftCorner
Upper left corner.
Definition: rect.h:270
position2d< T > LowerRightCorner
Lower right corner.
Definition: rect.h:272
GLint GLint GLint GLint GLint GLint y
Definition: SDL_opengl.h:1574
GLint GLint GLint GLint GLint x
Definition: SDL_opengl.h:1574

◆ clipAgainst()

template<class T>
void irr::core::rect< T >::clipAgainst ( const rect< T > &  other)
inline

Clips this rectangle with another one.

Parameters
otherRectangle to clip with

Definition at line 131 of file rect.h.

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 > 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  }
position2d< T > UpperLeftCorner
Upper left corner.
Definition: rect.h:270
position2d< T > LowerRightCorner
Lower right corner.
Definition: rect.h:272

◆ constrainTo()

template<class T>
bool irr::core::rect< T >::constrainTo ( const rect< T > &  other)
inline

Moves this rectangle to fit inside another one.

Returns
True on success, false if not possible

Definition at line 152 of file rect.h.

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  }
position2d< T > UpperLeftCorner
Upper left corner.
Definition: rect.h:270
position2d< T > LowerRightCorner
Lower right corner.
Definition: rect.h:272
T getHeight() const
Get height of rectangle.
Definition: rect.h:195
T getWidth() const
Get width of rectangle.
Definition: rect.h:189

◆ getArea()

template<class T>
T irr::core::rect< T >::getArea ( ) const
inline

Returns size of rectangle.

Definition at line 102 of file rect.h.

103  {
104  return getWidth() * getHeight();
105  }
T getHeight() const
Get height of rectangle.
Definition: rect.h:195
T getWidth() const
Get width of rectangle.
Definition: rect.h:189

◆ getCenter()

template<class T>
position2d<T> irr::core::rect< T >::getCenter ( ) const
inline

Get the center of the rectangle.

Definition at line 228 of file rect.h.

229  {
230  return position2d<T>(
231  (UpperLeftCorner.X + LowerRightCorner.X) / 2,
232  (UpperLeftCorner.Y + LowerRightCorner.Y) / 2);
233  }
position2d< T > UpperLeftCorner
Upper left corner.
Definition: rect.h:270
position2d< T > LowerRightCorner
Lower right corner.
Definition: rect.h:272

◆ getHeight()

template<class T>
T irr::core::rect< T >::getHeight ( ) const
inline

Get height of rectangle.

Definition at line 195 of file rect.h.

196  {
197  return LowerRightCorner.Y - UpperLeftCorner.Y;
198  }
position2d< T > UpperLeftCorner
Upper left corner.
Definition: rect.h:270
position2d< T > LowerRightCorner
Lower right corner.
Definition: rect.h:272

◆ getSize()

template<class T>
dimension2d<T> irr::core::rect< T >::getSize ( ) const
inline

Get the dimensions of the rectangle.

Definition at line 236 of file rect.h.

237  {
238  return dimension2d<T>(getWidth(), getHeight());
239  }
T getHeight() const
Get height of rectangle.
Definition: rect.h:195
T getWidth() const
Get width of rectangle.
Definition: rect.h:189

◆ getWidth()

template<class T>
T irr::core::rect< T >::getWidth ( ) const
inline

Get width of rectangle.

Definition at line 189 of file rect.h.

190  {
191  return LowerRightCorner.X - UpperLeftCorner.X;
192  }
position2d< T > UpperLeftCorner
Upper left corner.
Definition: rect.h:270
position2d< T > LowerRightCorner
Lower right corner.
Definition: rect.h:272

◆ isPointInside()

template<class T>
bool irr::core::rect< T >::isPointInside ( const position2d< T > &  pos) const
inline

Returns if a 2d point is within this rectangle.

Parameters
posPosition to test if it lies within this rectangle.
Returns
True if the position is within the rectangle, false if not.

Definition at line 110 of file rect.h.

111  {
112  return (UpperLeftCorner.X <= pos.X &&
113  UpperLeftCorner.Y <= pos.Y &&
114  LowerRightCorner.X >= pos.X &&
115  LowerRightCorner.Y >= pos.Y);
116  }
position2d< T > UpperLeftCorner
Upper left corner.
Definition: rect.h:270
position2d< T > LowerRightCorner
Lower right corner.
Definition: rect.h:272

◆ isRectCollided()

template<class T>
bool irr::core::rect< T >::isRectCollided ( const rect< T > &  other) const
inline

Check if the rectangle collides with another rectangle.

Parameters
otherRectangle to test collision with
Returns
True if the rectangles collide.

Definition at line 121 of file rect.h.

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  }
position2d< T > UpperLeftCorner
Upper left corner.
Definition: rect.h:270
position2d< T > LowerRightCorner
Lower right corner.
Definition: rect.h:272

◆ isValid()

template<class T>
bool irr::core::rect< T >::isValid ( ) const
inline

Returns if the rect is valid to draw.

It would be invalid if the UpperLeftCorner is lower or more right than the LowerRightCorner.

Definition at line 221 of file rect.h.

222  {
223  return ((LowerRightCorner.X >= UpperLeftCorner.X) &&
225  }
position2d< T > UpperLeftCorner
Upper left corner.
Definition: rect.h:270
position2d< T > LowerRightCorner
Lower right corner.
Definition: rect.h:272

◆ operator!=()

template<class T>
bool irr::core::rect< T >::operator!= ( const rect< T > &  other) const
inline

inequality operator

Definition at line 89 of file rect.h.

90  {
91  return (UpperLeftCorner != other.UpperLeftCorner ||
92  LowerRightCorner != other.LowerRightCorner);
93  }
position2d< T > UpperLeftCorner
Upper left corner.
Definition: rect.h:270
position2d< T > LowerRightCorner
Lower right corner.
Definition: rect.h:272

◆ operator+()

template<class T>
rect<T> irr::core::rect< T >::operator+ ( const position2d< T > &  pos) const
inline

move right by given numbers

Definition at line 52 of file rect.h.

53  {
54  rect<T> ret(*this);
55  return ret+=pos;
56  }

◆ operator+=()

template<class T>
rect<T>& irr::core::rect< T >::operator+= ( const position2d< T > &  pos)
inline

move right by given numbers

Definition at line 59 of file rect.h.

60  {
61  UpperLeftCorner += pos;
62  LowerRightCorner += pos;
63  return *this;
64  }
position2d< T > UpperLeftCorner
Upper left corner.
Definition: rect.h:270
position2d< T > LowerRightCorner
Lower right corner.
Definition: rect.h:272

◆ operator-()

template<class T>
rect<T> irr::core::rect< T >::operator- ( const position2d< T > &  pos) const
inline

move left by given numbers

Definition at line 67 of file rect.h.

68  {
69  rect<T> ret(*this);
70  return ret-=pos;
71  }

◆ operator-=()

template<class T>
rect<T>& irr::core::rect< T >::operator-= ( const position2d< T > &  pos)
inline

move left by given numbers

Definition at line 74 of file rect.h.

75  {
76  UpperLeftCorner -= pos;
77  LowerRightCorner -= pos;
78  return *this;
79  }
position2d< T > UpperLeftCorner
Upper left corner.
Definition: rect.h:270
position2d< T > LowerRightCorner
Lower right corner.
Definition: rect.h:272

◆ operator<()

template<class T>
bool irr::core::rect< T >::operator< ( const rect< T > &  other) const
inline

compares size of rectangles

Definition at line 96 of file rect.h.

97  {
98  return getArea() < other.getArea();
99  }
T getArea() const
Returns size of rectangle.
Definition: rect.h:102

◆ operator==()

template<class T>
bool irr::core::rect< T >::operator== ( const rect< T > &  other) const
inline

equality operator

Definition at line 82 of file rect.h.

83  {
84  return (UpperLeftCorner == other.UpperLeftCorner &&
85  LowerRightCorner == other.LowerRightCorner);
86  }
position2d< T > UpperLeftCorner
Upper left corner.
Definition: rect.h:270
position2d< T > LowerRightCorner
Lower right corner.
Definition: rect.h:272

◆ repair()

template<class T>
void irr::core::rect< T >::repair ( )
inline

If the lower right corner of the rect is smaller then the upper left, the points are swapped.

Definition at line 201 of file rect.h.

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  }
GLdouble GLdouble t
Definition: SDL_opengl.h:2071
position2d< T > UpperLeftCorner
Upper left corner.
Definition: rect.h:270
position2d< T > LowerRightCorner
Lower right corner.
Definition: rect.h:272

Member Data Documentation

◆ LowerRightCorner

template<class T>
position2d<T> irr::core::rect< T >::LowerRightCorner

Lower right corner.

Definition at line 272 of file rect.h.

◆ UpperLeftCorner

template<class T>
position2d<T> irr::core::rect< T >::UpperLeftCorner

Upper left corner.

Definition at line 270 of file rect.h.


The documentation for this class was generated from the following file: