Plasma Engine  2.0
Loading...
Searching...
No Matches
Rect.h
1#pragma once
2
3#include <Foundation/Basics.h>
4#include <Foundation/Math/Vec2.h>
5
8template <typename Type>
10{
11public:
12 // Means this object can be copied using memcpy instead of copy construction.
13 PL_DECLARE_POD_TYPE();
14
15 // *** Data ***
16public:
17 Type x;
18 Type y;
19
20 Type width;
21 Type height;
22
23 // *** Constructors ***
24public:
27
29 plRectTemplate(Type x, Type y, Type width, Type height);
30
32 plRectTemplate(Type width, Type height);
33
35 plRectTemplate<Type>(const plVec2Template<Type>& vTopLeftPosition, const plVec2Template<Type>& vSize);
36
41 [[nodiscard]] static plRectTemplate<Type> MakeInvalid();
42
47 [[nodiscard]] static plRectTemplate<Type> MakeIntersection(const plRectTemplate<Type>& r0, const plRectTemplate<Type>& r1);
48
52 [[nodiscard]] static plRectTemplate<Type> MakeUnion(const plRectTemplate<Type>& r0, const plRectTemplate<Type>& r1);
53
55 Type Left() const { return x; }
56
58 Type Right() const { return x + width; }
59
61 Type Top() const { return y; }
62
64 Type Bottom() const { return y + height; }
65
67 Type GetX1() const { return x; }
68
70 Type GetX2() const { return x + width; }
71
73 Type GetY1() const { return y; }
74
76 Type GetY2() const { return y + height; }
77
80
82 plVec2Template<Type> GetMaxCorner() const { return plVec2Template<Type>(x + width, y + height); }
83
86
88 plVec2Template<Type> GetTopRight() const { return plVec2Template<Type>(x + width, y); }
89
91 plVec2Template<Type> GetBottomLeft() const { return plVec2Template<Type>(x, y + height); }
92
94 plVec2Template<Type> GetBottomRight() const { return plVec2Template<Type>(x + width, y + height); }
95
97 plVec2Template<Type> GetCenter() const { return plVec2Template<Type>(x + width / 2, y + height / 2); }
98
100 plVec2Template<Type> GetExtents() const { return plVec2Template<Type>(width, height); }
101
103 plVec2Template<Type> GetHalfExtents() const { return plVec2Template<Type>(width / 2, height / 2); }
104
106 void Grow(Type xy);
107
108 // *** Common Functions ***
109public:
110 [[nodiscard]] bool operator==(const plRectTemplate<Type>& rhs) const;
111 [[nodiscard]] bool operator!=(const plRectTemplate<Type>& rhs) const;
112
114 [[nodiscard]] bool IsValid() const;
115
117 [[nodiscard]] bool HasNonZeroArea() const;
118
120 [[nodiscard]] bool Contains(const plVec2Template<Type>& vPoint) const;
121
122 [[nodiscard]] bool Contains(const plRectTemplate<Type>& r) const;
123
126 [[nodiscard]] bool Overlaps(const plRectTemplate<Type>& other) const;
127
129 void ExpandToInclude(const plRectTemplate<Type>& other);
130
132 void ExpandToInclude(const plVec2Template<Type>& other);
133
135 void Clip(const plRectTemplate<Type>& clipRect);
136
139 [[nodiscard]] const plVec2Template<Type> GetClampedPoint(const plVec2Template<Type>& vPoint) const;
140
144 [[nodiscard]] const plRectTemplate<Type> GetClampedRect(const plRectTemplate<Type>& r) const
145 {
148 return plRectTemplate<Type>(vNewMin, vNewMax - vNewMin);
149 }
150
152 void SetCenter(Type tX, Type tY);
153
155 void Translate(Type tX, Type tY);
156
158 void Scale(Type sX, Type sY);
159};
160
161#include <Foundation/Math/Implementation/Rect_inl.h>
162
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
plVec2Template< Type > GetMaxCorner() const
Returns the maximum corner position. Same as GetBottomRight().
Definition Rect.h:82
bool HasNonZeroArea() const
Returns true if the area of the rectangle is non zero.
Definition Rect_inl.h:102
plVec2Template< Type > GetMinCorner() const
Returns the minimum corner position. Same as GetTopLeft().
Definition Rect.h:79
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
plVec2Template< Type > GetExtents() const
Returns the width and height as a vec2.
Definition Rect.h:100
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
plVec2Template< Type > GetTopLeft() const
Returns the top left corner. Same as GetMinCorner().
Definition Rect.h:85
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
Type Left() const
The smaller value along x.
Definition Rect.h:55
plVec2Template< Type > GetBottomRight() const
Returns the bottom right corner. Same as GetMaxCorner().
Definition Rect.h:94
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
plVec2Template< Type > GetBottomLeft() const
Returns the bottom left corner.
Definition Rect.h:91
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
plVec2Template< Type > GetTopRight() const
Returns the top right corner.
Definition Rect.h:88
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
plVec2Template< Type > GetHalfExtents() const
Returns the half width and half height as a vec2.
Definition Rect.h:103
plVec2Template< Type > GetCenter() const
Returns the center point of the rectangle.
Definition Rect.h:97
Type Top() const
The smaller value along y.
Definition Rect.h:61
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
const plRectTemplate< Type > GetClampedRect(const plRectTemplate< Type > &r) const
Clamps the given rect to the area of this rect and returns it.
Definition Rect.h:144
A 2-component vector class.
Definition Vec2.h:14