Loading...
Searching...
No Matches
rect.h
Go to the documentation of this file.
// Copyright (C) 2002-2012 Nikolaus Gebhardt
// This file is part of the "Irrlicht Engine".
// For conditions of distribution and use, see copyright notice in irrlicht.h
#ifndef __IRR_RECT_H_INCLUDED__
#define __IRR_RECT_H_INCLUDED__
#include "irrTypes.h"
#include "dimension2d.h"
#include "position2d.h"
namespace irr
{
namespace core
{
template <class T>
class rect
{
public:
rect() : UpperLeftCorner(0,0), LowerRightCorner(0,0) {}
rect(T x, T y, T x2, T y2)
: UpperLeftCorner(x,y), LowerRightCorner(x2,y2) {}
rect(const position2d<T>& upperLeft, const position2d<T>& lowerRight)
: UpperLeftCorner(upperLeft), LowerRightCorner(lowerRight) {}
template <class U>
rect(const position2d<T>& pos, const dimension2d<U>& size)
: UpperLeftCorner(pos), LowerRightCorner(pos.X + size.Width, pos.Y + size.Height) {}
rect<T> operator+(const position2d<T>& pos) const
{
rect<T> ret(*this);
return ret+=pos;
}
rect<T>& operator+=(const position2d<T>& pos)
{
UpperLeftCorner += pos;
LowerRightCorner += pos;
return *this;
}
rect<T> operator-(const position2d<T>& pos) const
{
rect<T> ret(*this);
return ret-=pos;
}
rect<T>& operator-=(const position2d<T>& pos)
{
UpperLeftCorner -= pos;
LowerRightCorner -= pos;
return *this;
}
bool operator==(const rect<T>& other) const
{
return (UpperLeftCorner == other.UpperLeftCorner &&
LowerRightCorner == other.LowerRightCorner);
}
bool operator!=(const rect<T>& other) const
{
return (UpperLeftCorner != other.UpperLeftCorner ||
LowerRightCorner != other.LowerRightCorner);
}
bool operator<(const rect<T>& other) const
{
return getArea() < other.getArea();
}
T getArea() const
{
return getWidth() * getHeight();
}
bool isPointInside(const position2d<T>& pos) const
{
return (UpperLeftCorner.X <= pos.X &&
UpperLeftCorner.Y <= pos.Y &&
LowerRightCorner.X >= pos.X &&
LowerRightCorner.Y >= pos.Y);
}
bool isRectCollided(const rect<T>& other) const
{
return (LowerRightCorner.Y > other.UpperLeftCorner.Y &&
UpperLeftCorner.Y < other.LowerRightCorner.Y &&
LowerRightCorner.X > other.UpperLeftCorner.X &&
UpperLeftCorner.X < other.LowerRightCorner.X);
}
void clipAgainst(const rect<T>& other)
{
if (other.LowerRightCorner.X < LowerRightCorner.X)
LowerRightCorner.X = other.LowerRightCorner.X;
if (other.LowerRightCorner.Y < LowerRightCorner.Y)
LowerRightCorner.Y = other.LowerRightCorner.Y;
if (other.UpperLeftCorner.X > UpperLeftCorner.X)
UpperLeftCorner.X = other.UpperLeftCorner.X;
if (other.UpperLeftCorner.Y > UpperLeftCorner.Y)
UpperLeftCorner.Y = other.UpperLeftCorner.Y;
// correct possible invalid rect resulting from clipping
if (UpperLeftCorner.Y > LowerRightCorner.Y)
UpperLeftCorner.Y = LowerRightCorner.Y;
if (UpperLeftCorner.X > LowerRightCorner.X)
UpperLeftCorner.X = LowerRightCorner.X;
}
bool constrainTo(const rect<T>& other)
{
if (other.getWidth() < getWidth() || other.getHeight() < getHeight())
return false;
T diff = other.LowerRightCorner.X - LowerRightCorner.X;
if (diff < 0)
{
LowerRightCorner.X += diff;
UpperLeftCorner.X += diff;
}
diff = other.LowerRightCorner.Y - LowerRightCorner.Y;
if (diff < 0)
{
LowerRightCorner.Y += diff;
UpperLeftCorner.Y += diff;
}
diff = UpperLeftCorner.X - other.UpperLeftCorner.X;
if (diff < 0)
{
UpperLeftCorner.X -= diff;
LowerRightCorner.X -= diff;
}
diff = UpperLeftCorner.Y - other.UpperLeftCorner.Y;
if (diff < 0)
{
UpperLeftCorner.Y -= diff;
LowerRightCorner.Y -= diff;
}
return true;
}
T getWidth() const
{
return LowerRightCorner.X - UpperLeftCorner.X;
}
T getHeight() const
{
return LowerRightCorner.Y - UpperLeftCorner.Y;
}
void repair()
{
if (LowerRightCorner.X < UpperLeftCorner.X)
{
T t = LowerRightCorner.X;
LowerRightCorner.X = UpperLeftCorner.X;
UpperLeftCorner.X = t;
}
if (LowerRightCorner.Y < UpperLeftCorner.Y)
{
T t = LowerRightCorner.Y;
LowerRightCorner.Y = UpperLeftCorner.Y;
UpperLeftCorner.Y = t;
}
}
bool isValid() const
{
return ((LowerRightCorner.X >= UpperLeftCorner.X) &&
(LowerRightCorner.Y >= UpperLeftCorner.Y));
}
position2d<T> getCenter() const
{
return position2d<T>(
(UpperLeftCorner.X + LowerRightCorner.X) / 2,
(UpperLeftCorner.Y + LowerRightCorner.Y) / 2);
}
dimension2d<T> getSize() const
{
return dimension2d<T>(getWidth(), getHeight());
}
void addInternalPoint(const position2d<T>& p)
{
addInternalPoint(p.X, p.Y);
}
void addInternalPoint(T x, T y)
{
if (x>LowerRightCorner.X)
LowerRightCorner.X = x;
if (y>LowerRightCorner.Y)
LowerRightCorner.Y = y;
if (x<UpperLeftCorner.X)
UpperLeftCorner.X = x;
if (y<UpperLeftCorner.Y)
UpperLeftCorner.Y = y;
}
position2d<T> UpperLeftCorner;
position2d<T> LowerRightCorner;
};
typedef rect<f32> rectf;
typedef rect<s32> recti;
} // end namespace core
} // end namespace irr
#endif
irr::core::dimension2d
Specifies a 2 dimensional size.
Definition dimension2d.h:21
irr::core::rect
Rectangle template.
Definition rect.h:27
irr::core::rect::rect
rect()
Default constructor creating empty rectangle at (0,0).
Definition rect.h:31
irr::core::rect::operator+
rect< T > operator+(const position2d< T > &pos) const
move right by given numbers
Definition rect.h:47
irr::core::rect::addInternalPoint
void addInternalPoint(const position2d< T > &p)
Adds a point to the rectangle.
Definition rect.h:241
irr::core::rect::getArea
T getArea() const
Returns size of rectangle.
Definition rect.h:97
irr::core::rect::addInternalPoint
void addInternalPoint(T x, T y)
Adds a point to the bounding rectangle.
Definition rect.h:251
irr::core::rect::getSize
dimension2d< T > getSize() const
Get the dimensions of the rectangle.
Definition rect.h:231
irr::core::rect::operator==
bool operator==(const rect< T > &other) const
equality operator
Definition rect.h:77
irr::core::rect::isPointInside
bool isPointInside(const position2d< T > &pos) const
Returns if a 2d point is within this rectangle.
Definition rect.h:105
irr::core::rect::constrainTo
bool constrainTo(const rect< T > &other)
Moves this rectangle to fit inside another one.
Definition rect.h:147
irr::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:196
irr::core::rect::rect
rect(const position2d< T > &pos, const dimension2d< U > &size)
Constructor with upper left corner and dimension.
Definition rect.h:43
irr::core::rect::clipAgainst
void clipAgainst(const rect< T > &other)
Clips this rectangle with another one.
Definition rect.h:126
irr::core::rect::operator<
bool operator<(const rect< T > &other) const
compares size of rectangles
Definition rect.h:91
irr::core::rect::rect
rect(const position2d< T > &upperLeft, const position2d< T > &lowerRight)
Constructor with two corners.
Definition rect.h:38
irr::core::rect< f32 >::LowerRightCorner
position2d< f32 > LowerRightCorner
Definition rect.h:267
irr::core::rect::operator-=
rect< T > & operator-=(const position2d< T > &pos)
move left by given numbers
Definition rect.h:69
irr::core::rect::isRectCollided
bool isRectCollided(const rect< T > &other) const
Check if the rectangle collides with another rectangle.
Definition rect.h:116
irr::core::rect< f32 >::UpperLeftCorner
position2d< f32 > UpperLeftCorner
Definition rect.h:265
irr::core::rect::isValid
bool isValid() const
Returns if the rect is valid to draw.
Definition rect.h:216
irr::core::rect::getHeight
T getHeight() const
Get height of rectangle.
Definition rect.h:190
irr::core::rect::getWidth
T getWidth() const
Get width of rectangle.
Definition rect.h:184
irr::core::rect::operator!=
bool operator!=(const rect< T > &other) const
inequality operator
Definition rect.h:84
irr::core::rect::operator-
rect< T > operator-(const position2d< T > &pos) const
move left by given numbers
Definition rect.h:62
irr::core::rect::rect
rect(T x, T y, T x2, T y2)
Constructor with two corners.
Definition rect.h:34
irr::core::rect::operator+=
rect< T > & operator+=(const position2d< T > &pos)
move right by given numbers
Definition rect.h:54
irr::core::rect::getCenter
position2d< T > getCenter() const
Get the center of the rectangle.
Definition rect.h:223
dimension2d.h
irrTypes.h
irr::core
Basic classes such as vectors, planes, arrays, lists, and so on can be found in this namespace.
Definition aabbox3d.h:15
irr::core::recti
rect< s32 > recti
Rectangle with int values.
Definition rect.h:273
irr::core::rectf
rect< f32 > rectf
Rectangle with float values.
Definition rect.h:271
irr
Everything in the Irrlicht Engine can be found in this namespace.
Definition aabbox3d.h:13
position2d.h
position2d
#define position2d
Definition position2d.h:29
- Irrlicht Engine Documentation © 2003-2012 by Nikolaus Gebhardt. Generated on for Irrlicht 3D Engine by Doxygen 1.15.0