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);
75std::optional<Rect<T>> Rect<T>::findIntersection(Rect<T>
const& rectangle)
const
78 T
const r1FarX =
static_cast<T
>(this->_x + this->_width);
79 T
const r1FarY =
static_cast<T
>(this->_y + this->_height);
81 T
const r2FarX =
static_cast<T
>(rectangle._x + rectangle._width);
82 T
const r2FarY =
static_cast<T
>(rectangle._y + rectangle._height);
85 T
const r1MinX = (this->_x < r1FarX) ? this->_x : r1FarX;
86 T
const r1MaxX = (this->_x > r1FarX) ? this->_x : r1FarX;
87 T
const r1MinY = (this->_y < r1FarY) ? this->_y : r1FarY;
88 T
const r1MaxY = (this->_y > r1FarY) ? this->_y : r1FarY;
91 T
const r2MinX = (rectangle._x < r2FarX) ? rectangle._x : r2FarX;
92 T
const r2MaxX = (rectangle._x > r2FarX) ? rectangle._x : r2FarX;
93 T
const r2MinY = (rectangle._y < r2FarY) ? rectangle._y : r2FarY;
94 T
const r2MaxY = (rectangle._y > r2FarY) ? rectangle._y : r2FarY;
97 T
const interLeft = (r1MinX > r2MinX) ? r1MinX : r2MinX;
98 T
const interTop = (r1MinY > r2MinY) ? r1MinY : r2MinY;
99 T
const interRight = (r1MaxX < r2MaxX) ? r1MaxX : r2MaxX;
100 T
const interBottom = (r1MaxY < r2MaxY) ? r1MaxY : r2MaxY;
103 if ((interLeft < interRight) && (interTop < interBottom))
105 return Rect<T>({interLeft, interTop}, {interRight - interLeft, interBottom - interTop});
111Vector2<T> Rect<T>::getPosition()
const
113 return Vector2<T>(this->_x, this->_y);
117Vector2<T> Rect<T>::getSize()
const
119 return Vector2<T>(this->_width, this->_height);
125 fge::Vector2f
const points[] = {left * glm::vec4(right._x, right._y, 0.0f, 1.0f),
126 left * glm::vec4(right._x, right._y + right._height, 0.0f, 1.0f),
127 left * glm::vec4(right._x + right._width, right._y, 0.0f, 1.0f),
128 left * glm::vec4(right._x + right._width, right._y + right._height, 0.0f, 1.0f)};
131 float posLeft = points[0].x;
132 float posTop = points[0].y;
133 float posRight = points[0].x;
134 float posBottom = points[0].y;
135 for (
int i = 1; i < 4; ++i)
137 if (points[i].x < posLeft)
139 posLeft = points[i].x;
141 else if (points[i].x > posRight)
143 posRight = points[i].x;
145 if (points[i].y < posTop)
147 posTop = points[i].y;
149 else if (points[i].y > posBottom)
151 posBottom = points[i].y;
155 return fge::RectFloat({posLeft, posTop}, {posRight - posLeft, posBottom - posTop});