Infinity Engine v0.6.20
C++ API Documentation
Loading...
Searching...
No Matches
Rect.hpp
1#pragma once
2
3#include <Infinity/api.h>
4#include <Infinity/Types/Core/Value.hpp>
5#include <Infinity/Types/Containers/Array.hpp>
6#include <Infinity/Types/Math/Vector2.hpp>
7
9{
69 struct INFINITY_API_PUBLIC Rect
70 {
71 float l;
72 float r;
73 float t;
74 float b;
75
81 constexpr Rect() :
82 l(0.0f), r(0.0f), t(0.0f), b(0.0f)
83 { }
84
90 constexpr Rect(const Rect& r) :
91 l(r.l), r(r.r), t(r.t), b(r.b)
92 { }
93
110 constexpr Rect(float l, float r, float t, float b) :
111 l(l), r(r), t(t), b(b)
112 { }
113
117 constexpr Rect& operator=(const Rect&) = default;
118
138 static Rect fromSize(float x, float y, float width, float height);
139
145 static constexpr Rect zero()
146 {
147 return Rect(0.0f, 0.0f, 0.0f, 0.0f);
148 }
149
158 void set(float in_l, float in_r, float in_t, float in_b);
159
166
173
180
187
195 float width() const;
196
204 float height() const;
205
213 float area() const;
214
234 bool contains(const Vector2& p) const;
235
250 Rect translated(const Vector2& offset) const;
251
267 Rect scaled(float factor) const;
268
280
286 Vector2 size() const;
287
296 Rect& operator+=(const Rect& rhs);
297
306 Rect& operator+=(const Vector2& rhs);
307
316 Rect& operator-=(const Rect& rhs);
317
326 Rect& operator-=(const Vector2& rhs);
327
335 explicit operator bool() const noexcept;
336 };
337
345 constexpr bool operator==(const Rect& lhs, const Rect& rhs)
346 {
347 return lhs.l == rhs.l && lhs.r == rhs.r && lhs.t == rhs.t && lhs.b == rhs.b;
348 }
349
357 constexpr bool operator!=(const Rect& lhs, const Rect& rhs)
358 {
359 return lhs.l != rhs.l || lhs.r != rhs.r || lhs.t != rhs.t || lhs.b != rhs.b;
360 }
361
371 constexpr Rect operator-(const Rect& lhs, const Rect& rhs)
372 {
373 return Rect(lhs.l - rhs.l, lhs.r - rhs.r, lhs.t - rhs.t, lhs.b - rhs.b);
374 }
375
384 constexpr Rect operator-(const Rect& v)
385 {
386 return Rect(-v.l, -v.r, -v.t, -v.b);
387 }
388
398 constexpr Rect operator+(const Rect& lhs, const Rect& rhs)
399 {
400 return Rect(lhs.l + rhs.l, lhs.r + rhs.r, lhs.t + rhs.t, lhs.b + rhs.b);
401 }
402
412 inline std::ostream& operator<<(std::ostream& out, const Rect& r)
413 {
414 out << r.l << " " << r.r << " " << r.t << " " << r.b;
415 return out;
416 }
417
427 inline std::istream& operator>>(std::istream& in, Rect& r)
428 {
429 in >> r.l >> r.r >> r.t >> r.b;
430 return in;
431 }
432}
433
434// Register Rect in the type system
Dynamic contiguous container for homogeneous elements in the Infinity type system.
Definition Array.hpp:77
Template wrapper for primitive types to integrate with the Infinity type system.
Definition Value.hpp:89
Definition Math.hpp:9
constexpr Rect operator-(const Rect &lhs, const Rect &rhs)
Rectangle subtraction operator.
Definition Rect.hpp:371
constexpr Rect operator+(const Rect &lhs, const Rect &rhs)
Rectangle addition operator.
Definition Rect.hpp:398
Axis-aligned rectangle defined by left, right, top, and bottom edges.
Definition Rect.hpp:70
constexpr Rect()
Default constructor.
Definition Rect.hpp:81
float area() const
Computes the area of the rectangle.
Vector2 center() const
Computes the center point of the rectangle.
Rect & operator+=(const Vector2 &rhs)
Addition assignment with a vector.
Rect & operator+=(const Rect &rhs)
Addition assignment with another rectangle.
float b
Bottom edge position.
Definition Rect.hpp:74
static Rect fromSize(float x, float y, float width, float height)
Creates a rectangle from position and size.
constexpr Rect(const Rect &r)
Copy constructor.
Definition Rect.hpp:90
float height() const
Computes the height of the rectangle.
float t
Top edge position.
Definition Rect.hpp:73
Vector2 bottomLeft() const
Gets the bottom-left corner position.
Rect scaled(float factor) const
Creates a scaled copy of this rectangle.
constexpr Rect & operator=(const Rect &)=default
Copy assignment operator.
float width() const
Computes the width of the rectangle.
constexpr Rect(float l, float r, float t, float b)
Constructs a rectangle from edge positions.
Definition Rect.hpp:110
Vector2 bottomRight() const
Gets the bottom-right corner position.
Rect translated(const Vector2 &offset) const
Creates a translated copy of this rectangle.
float r
Right edge position.
Definition Rect.hpp:72
bool contains(const Vector2 &p) const
Tests if a point is contained within the rectangle.
static constexpr Rect zero()
Creates a zero-sized rectangle at the origin.
Definition Rect.hpp:145
float l
Left edge position.
Definition Rect.hpp:71
Vector2 size() const
Gets the size of the rectangle as a vector.
Vector2 topRight() const
Gets the top-right corner position.
void set(float in_l, float in_r, float in_t, float in_b)
Sets all four edge positions.
Rect & operator-=(const Rect &rhs)
Subtraction assignment with another rectangle.
Vector2 topLeft() const
Gets the top-left corner position.
Rect & operator-=(const Vector2 &rhs)
Subtraction assignment with a vector.
Template structure representing a 2-component vector.
Definition Vector2.hpp:75