29Rect<T>::Rect(Vector2<T>
const& position, Vector2<T>
const& size) :
38Rect<T>::Rect(Rect<U>
const& rectangle) :
39 _x(static_cast<T>(rectangle._x)),
40 _y(static_cast<T>(rectangle._y)),
41 _width(static_cast<T>(rectangle._width)),
42 _height(static_cast<T>(rectangle._height))
46bool Rect<T>::operator==(Rect<T>
const& right)
const
48 return (this->_x == right._x) && (this->_width == right._width) && (this->_y == right._y) &&
49 (this->_height == right._height);
53bool Rect<T>::operator!=(Rect<T>
const& right)
const
55 return !this->operator==(right);
59bool Rect<T>::contains(Vector2<T>
const& point)
const
62 T
const farX =
static_cast<T
>(this->_x + this->_width);
63 T
const farY =
static_cast<T
>(this->_y + this->_height);
66 T
const minX = (this->_x < farX) ? this->_x : farX;
67 T
const maxX = (this->_x > farX) ? this->_x : farX;
68 T
const minY = (this->_y < farY) ? this->_y : farY;
69 T
const maxY = (this->_y > farY) ? this->_y : farY;
71 return (point.x >= minX) && (point.x < maxX) && (point.y >= minY) && (point.y < maxY);
74bool Rect<T>::contains(Rect<T>
const& rectangle)
const
77 T
const r1FarX =
static_cast<T
>(this->_x + this->_width);
78 T
const r1FarY =
static_cast<T
>(this->_y + this->_height);
80 T
const r2FarX =
static_cast<T
>(rectangle._x + rectangle._width);
81 T
const r2FarY =
static_cast<T
>(rectangle._y + rectangle._height);
84 T
const r1MinX = (this->_x < r1FarX) ? this->_x : r1FarX;
85 T
const r1MaxX = (this->_x > r1FarX) ? this->_x : r1FarX;
86 T
const r1MinY = (this->_y < r1FarY) ? this->_y : r1FarY;
87 T
const r1MaxY = (this->_y > r1FarY) ? this->_y : r1FarY;
90 T
const r2MinX = (rectangle._x < r2FarX) ? rectangle._x : r2FarX;
91 T
const r2MaxX = (rectangle._x > r2FarX) ? rectangle._x : r2FarX;
92 T
const r2MinY = (rectangle._y < r2FarY) ? rectangle._y : r2FarY;
93 T
const r2MaxY = (rectangle._y > r2FarY) ? rectangle._y : r2FarY;
95 return (r1MinX <= r2MinX) && (r1MaxX >= r2MaxX) && (r1MinY <= r2MinY) && (r1MaxY >= r2MaxY);
99std::optional<Rect<T>> Rect<T>::findIntersection(Rect<T>
const& rectangle)
const
102 T
const r1FarX =
static_cast<T
>(this->_x + this->_width);
103 T
const r1FarY =
static_cast<T
>(this->_y + this->_height);
105 T
const r2FarX =
static_cast<T
>(rectangle._x + rectangle._width);
106 T
const r2FarY =
static_cast<T
>(rectangle._y + rectangle._height);
109 T
const r1MinX = (this->_x < r1FarX) ? this->_x : r1FarX;
110 T
const r1MaxX = (this->_x > r1FarX) ? this->_x : r1FarX;
111 T
const r1MinY = (this->_y < r1FarY) ? this->_y : r1FarY;
112 T
const r1MaxY = (this->_y > r1FarY) ? this->_y : r1FarY;
115 T
const r2MinX = (rectangle._x < r2FarX) ? rectangle._x : r2FarX;
116 T
const r2MaxX = (rectangle._x > r2FarX) ? rectangle._x : r2FarX;
117 T
const r2MinY = (rectangle._y < r2FarY) ? rectangle._y : r2FarY;
118 T
const r2MaxY = (rectangle._y > r2FarY) ? rectangle._y : r2FarY;
121 T
const interLeft = (r1MinX > r2MinX) ? r1MinX : r2MinX;
122 T
const interTop = (r1MinY > r2MinY) ? r1MinY : r2MinY;
123 T
const interRight = (r1MaxX < r2MaxX) ? r1MaxX : r2MaxX;
124 T
const interBottom = (r1MaxY < r2MaxY) ? r1MaxY : r2MaxY;
127 if ((interLeft < interRight) && (interTop < interBottom))
129 return Rect<T>({interLeft, interTop}, {interRight - interLeft, interBottom - interTop});
135Vector2<T> Rect<T>::getPosition()
const
137 return Vector2<T>(this->_x, this->_y);
141Vector2<T> Rect<T>::getSize()
const
143 return Vector2<T>(this->_width, this->_height);
146inline fge::RectFloat operator*(glm::mat4
const& left, fge::RectFloat
const& right)
149 fge::Vector2f
const points[] = {left * glm::vec4(right._x, right._y, 0.0f, 1.0f),
150 left * glm::vec4(right._x, right._y + right._height, 0.0f, 1.0f),
151 left * glm::vec4(right._x + right._width, right._y, 0.0f, 1.0f),
152 left * glm::vec4(right._x + right._width, right._y + right._height, 0.0f, 1.0f)};
155 float posLeft = points[0].x;
156 float posTop = points[0].y;
157 float posRight = points[0].x;
158 float posBottom = points[0].y;
159 for (
int i = 1; i < 4; ++i)
161 if (points[i].x < posLeft)
163 posLeft = points[i].x;
165 else if (points[i].x > posRight)
167 posRight = points[i].x;
169 if (points[i].y < posTop)
171 posTop = points[i].y;
173 else if (points[i].y > posBottom)
175 posBottom = points[i].y;
179 return fge::RectFloat({posLeft, posTop}, {posRight - posLeft, posBottom - posTop});