Plasma Engine  2.0
Loading...
Searching...
No Matches
Rect_inl.h
1#pragma once
2
3template <typename Type>
4PL_ALWAYS_INLINE plRectTemplate<Type>::plRectTemplate() = default;
5
6template <typename Type>
7PL_ALWAYS_INLINE plRectTemplate<Type>::plRectTemplate(Type x, Type y, Type width, Type height)
8 : x(x)
9 , y(y)
10 , width(width)
11 , height(height)
12{
13}
14
15template <typename Type>
16PL_ALWAYS_INLINE plRectTemplate<Type>::plRectTemplate(Type width, Type height)
17 : x(0)
18 , y(0)
19 , width(width)
20 , height(height)
21{
22}
23
24template <typename Type>
25PL_ALWAYS_INLINE plRectTemplate<Type>::plRectTemplate(const plVec2Template<Type>& vTopLeftPosition, const plVec2Template<Type>& vSize)
26{
27 x = vTopLeftPosition.x;
28 y = vTopLeftPosition.y;
29 width = vSize.x;
30 height = vSize.y;
31}
33template <typename Type>
35{
37
39
40 const Type fLargeValue = plMath::MaxValue<Type>() / 2;
41 res.x = fLargeValue;
42 res.y = fLargeValue;
43 res.width = -fLargeValue;
44 res.height = -fLargeValue;
45
46 return res;
48
49template <typename Type>
51{
53
55
56 Type x1 = plMath::Max(r0.GetX1(), r1.GetX1());
57 Type y1 = plMath::Max(r0.GetY1(), r1.GetY1());
58 Type x2 = plMath::Min(r0.GetX2(), r1.GetX2());
59 Type y2 = plMath::Min(r0.GetY2(), r1.GetY2());
60
61 res.x = x1;
62 res.y = y1;
63 res.width = x2 - x1;
64 res.height = y2 - y1;
65
66 return res;
67}
68
69template <typename Type>
71{
73
75
76 Type x1 = plMath::Min(r0.GetX1(), r1.GetX1());
77 Type y1 = plMath::Min(r0.GetY1(), r1.GetY1());
78 Type x2 = plMath::Max(r0.GetX2(), r1.GetX2());
79 Type y2 = plMath::Max(r0.GetY2(), r1.GetY2());
80
81 res.x = x1;
82 res.y = y1;
83 res.width = x2 - x1;
84 res.height = y2 - y1;
85
86 return res;
87}
88
89template <typename Type>
90PL_ALWAYS_INLINE bool plRectTemplate<Type>::operator==(const plRectTemplate<Type>& rhs) const
91{
92 return x == rhs.x && y == rhs.y && width == rhs.width && height == rhs.height;
93}
94
95template <typename Type>
96PL_ALWAYS_INLINE bool plRectTemplate<Type>::operator!=(const plRectTemplate<Type>& rhs) const
97{
98 return !(*this == rhs);
99}
100
101template <typename Type>
102PL_ALWAYS_INLINE bool plRectTemplate<Type>::HasNonZeroArea() const
103{
104 return (width > 0) && (height > 0);
105}
107template <typename Type>
108PL_ALWAYS_INLINE bool plRectTemplate<Type>::Contains(const plVec2Template<Type>& vPoint) const
109{
110 if (vPoint.x >= x && vPoint.x <= Right())
111 {
112 if (vPoint.y >= y && vPoint.y <= Bottom())
113 return true;
115
116 return false;
118
119template <typename Type>
120PL_ALWAYS_INLINE bool plRectTemplate<Type>::Contains(const plRectTemplate<Type>& r) const
121{
122 return r.x >= x && r.y >= y && r.Right() <= Right() && r.Bottom() <= Bottom();
123}
124
125template <typename Type>
126PL_ALWAYS_INLINE bool plRectTemplate<Type>::Overlaps(const plRectTemplate<Type>& other) const
127{
128 if (x < other.Right() && Right() > other.x && y < other.Bottom() && Bottom() > other.y)
129 return true;
130
131 return false;
133
134template <typename Type>
136{
137 Type thisRight = Right();
138 Type thisBottom = Bottom();
140 if (other.x < x)
141 x = other.x;
142
143 if (other.y < y)
144 y = other.y;
145
146 if (other.Right() > thisRight)
147 width = other.Right() - x;
148 else
149 width = thisRight - x;
150
151 if (other.Bottom() > thisBottom)
152 height = other.Bottom() - y;
153 else
154 height = thisBottom - y;
156
157template <typename Type>
159{
160 Type thisRight = Right();
161 Type thisBottom = Bottom();
162
163 if (other.x < x)
164 x = other.x;
165
166 if (other.y < y)
167 y = other.y;
168
169 if (other.x > thisRight)
170 width = other.x - x;
171 else
172 width = thisRight - x;
173
174 if (other.y > thisBottom)
175 height = other.y - y;
176 else
177 height = thisBottom - y;
178}
179
180template <typename Type>
182{
183 x -= xy;
184 y -= xy;
185 width += xy * 2;
186 height += xy * 2;
187}
188
189template <typename Type>
190PL_ALWAYS_INLINE void plRectTemplate<Type>::Clip(const plRectTemplate<Type>& clipRect)
191{
192 Type newLeft = plMath::Max<Type>(x, clipRect.x);
193 Type newTop = plMath::Max<Type>(y, clipRect.y);
194
195 Type newRight = plMath::Min<Type>(Right(), clipRect.Right());
196 Type newBottom = plMath::Min<Type>(Bottom(), clipRect.Bottom());
197
198 x = newLeft;
199 y = newTop;
200 width = newRight - newLeft;
201 height = newBottom - newTop;
202}
203
204template <typename Type>
205PL_ALWAYS_INLINE bool plRectTemplate<Type>::IsValid() const
206{
208
209 return width >= 0 && height >= 0;
210}
211
212template <typename Type>
214{
216
217 return plVec2Template<Type>(plMath::Clamp(vPoint.x, Left(), Right()), plMath::Clamp(vPoint.y, Top(), Bottom()));
218}
219
220template <typename Type>
222{
224
225 x = tX - width / 2;
226 y = tY - height / 2;
227}
228
229template <typename Type>
231{
233
234 x += tX;
235 y += tY;
236}
237
238template <typename Type>
239void plRectTemplate<Type>::Scale(Type sX, Type sY)
240{
242
243 x *= sX;
244 y *= sY;
245 width *= sX;
246 height *= sY;
247}
A simple rectangle class templated on the type for x, y and width, height.
Definition Rect.h:10
void Clip(const plRectTemplate< Type > &clipRect)
Clips this rect so that it is fully inside the provided rectangle.
Definition Rect_inl.h:190
bool HasNonZeroArea() const
Returns true if the area of the rectangle is non zero.
Definition Rect_inl.h:102
void Grow(Type xy)
Increases the size of the rect in all directions.
Definition Rect_inl.h:181
Type GetY1() const
The smaller value along y. Same as Top().
Definition Rect.h:73
Type Right() const
The larger value along x.
Definition Rect.h:58
Type GetY2() const
The larger value along y. Same as Bottom().
Definition Rect.h:76
bool IsValid() const
Checks whether the position and size contain valid values.
Definition Rect_inl.h:205
void Scale(Type sX, Type sY)
Scales width and height, and moves the position as well.
Definition Rect_inl.h:239
void SetCenter(Type tX, Type tY)
Sets the center of the rectangle.
Definition Rect_inl.h:221
Type GetX2() const
The larger value along x. Same as Right().
Definition Rect.h:70
const plVec2Template< Type > GetClampedPoint(const plVec2Template< Type > &vPoint) const
The given point is clamped to the area of the rect, i.e. it will be either inside the rect or on its ...
Definition Rect_inl.h:213
bool Contains(const plVec2Template< Type > &vPoint) const
Returns true if the rectangle contains the provided point.
Definition Rect_inl.h:108
Type GetX1() const
The smaller value along x. Same as Left().
Definition Rect.h:67
plRectTemplate()
Default constructor does not initialize the data.
static plRectTemplate< Type > MakeUnion(const plRectTemplate< Type > &r0, const plRectTemplate< Type > &r1)
Creates a rect that is the union of the two provided rects.
Definition Rect_inl.h:70
bool Overlaps(const plRectTemplate< Type > &other) const
Returns true if the rectangle overlaps the provided rectangle. Also returns true if the rectangles ar...
Definition Rect_inl.h:126
void Translate(Type tX, Type tY)
Moves the rectangle.
Definition Rect_inl.h:230
static plRectTemplate< Type > MakeInvalid()
Creates an 'invalid' rect.
Definition Rect_inl.h:34
static plRectTemplate< Type > MakeIntersection(const plRectTemplate< Type > &r0, const plRectTemplate< Type > &r1)
Creates a rect that is the intersection of the two provided rects.
Definition Rect_inl.h:50
Type Bottom() const
The larger value along y.
Definition Rect.h:64
void ExpandToInclude(const plRectTemplate< Type > &other)
Extends this rectangle so that the provided rectangle is completely contained within it.
Definition Rect_inl.h:135
A 2-component vector class.
Definition Vec2.h:14
constexpr PL_ALWAYS_INLINE T Min(T f1, T f2)
Returns the smaller value, f1 or f2.
Definition Math_inl.h:27
constexpr PL_ALWAYS_INLINE T Clamp(T value, T min_val, T max_val)
Clamps "value" to the range [min; max]. Returns "value", if it is inside the range already.
Definition Math_inl.h:51
constexpr TYPE MaxValue()
Returns the largest possible positive value (that is not infinity).
constexpr PL_ALWAYS_INLINE T Max(T f1, T f2)
Returns the greater value, f1 or f2.
Definition Math_inl.h:39