7#include "numeric_array.hpp"
8#include "alignment.hpp"
45 narrow_cast<T>(x) + narrow_cast<T>(width),
46 narrow_cast<T>(y) + narrow_cast<T>(height)})
67 tt_axiom(position.is_point());
68 tt_axiom(position.z() == 0.0);
69 tt_axiom(
extent.is_vector());
70 tt_axiom(
extent.z() == 0.0);
79 tt_axiom(
extent.is_vector());
80 tt_axiom(
extent.z() == 0.0);
99 tt_axiom(p0.is_point());
100 tt_axiom(p3.is_point());
116 [[nodiscard]]
bool valid() const noexcept
118 return le(v, v.zwzw()) == 0b1111;
123 [[nodiscard]]
bool empty() const noexcept
125 return eq(v, v.zwxy()) == 0b1111;
130 [[nodiscard]]
operator bool() const noexcept
142 return *
this = *
this | rhs;
152 return *
this = *
this | rhs;
161 return *
this = *
this + rhs;
170 return *
this = *
this - rhs;
179 return *
this = *
this * rhs;
190 static_assert(I <= 3);
191 if constexpr (I == 0) {
193 }
else if constexpr (I == 1) {
195 }
else if constexpr (I == 2) {
207 [[nodiscard]] numeric_array<T,4> p3() const noexcept
227 return (v.zwzw() - v).xy00();
230 [[nodiscard]] T x() const noexcept
235 [[nodiscard]] T y() const noexcept
240 [[nodiscard]] T width() const noexcept
242 return (v.zwzw() - v).x();
245 [[nodiscard]] T height() const noexcept
247 return (v.zwzw() - v).y();
250 [[nodiscard]] T bottom() const noexcept
255 [[nodiscard]] T top() const noexcept
260 [[nodiscard]] T left() const noexcept
265 [[nodiscard]] T right() const noexcept
274 return (bottom() + top()) * 0.5f;
281 return (left() + right()) * 0.5f;
290 axis_aligned_rectangle &set_height(T newHeight)
noexcept
292 v = v.xyzy() + numeric_array<T,4>{0.0f, 0.0f, 0.0f, newHeight};
303 return ge(rhs.xyxy(), v) == 0b0011;
315 if (alignment == horizontal_alignment::left) {
316 x = haystack.p0().x();
318 }
else if (alignment == horizontal_alignment::right) {
319 x = haystack.p3().x() - needle.width();
321 }
else if (alignment == horizontal_alignment::center) {
322 x = (haystack.p0().x() + (haystack.width() * 0.5f)) - (needle.width() * 0.5f);
329 if (alignment == vertical_alignment::bottom) {
330 y = haystack.p0().y();
332 }
else if (alignment == vertical_alignment::top) {
333 y = haystack.p3().y() - needle.height();
335 }
else if (alignment == vertical_alignment::middle) {
336 y = (haystack.p0().y() + (haystack.height() * 0.5f)) - (needle.height() * 0.5f);
349 return align(outside, inside, alignment);
354 return lhs.v == rhs.v;
357 [[nodiscard]]
friend bool operator!=(axis_aligned_rectangle
const &lhs, axis_aligned_rectangle
const &rhs)
noexcept
359 return !(lhs == rhs);
362 [[nodiscard]]
friend bool overlaps(axis_aligned_rectangle
const &lhs, axis_aligned_rectangle
const &rhs)
noexcept
364 if (lhs.empty() || rhs.
empty()) {
368 ttlet rhs_swap = rhs.v.zwxy();
371 if ((gt(lhs.v, rhs_swap) & 0b0011) != 0) {
376 if ((lt(lhs.v, rhs_swap) & 0b1100) != 0) {
383 [[nodiscard]]
friend axis_aligned_rectangle operator|(axis_aligned_rectangle
const &lhs, axis_aligned_rectangle
const &rhs)
noexcept
394 [[nodiscard]]
friend axis_aligned_rectangle operator|(axis_aligned_rectangle
const &lhs, numeric_array<T,4>
const &rhs)
noexcept
396 tt_axiom(rhs.is_point());
404 [[nodiscard]]
friend axis_aligned_rectangle operator+(axis_aligned_rectangle
const &lhs, numeric_array<T,4>
const &rhs)
noexcept
409 [[nodiscard]]
friend axis_aligned_rectangle operator-(axis_aligned_rectangle
const &lhs, numeric_array<T,4>
const &rhs)
noexcept
414 [[nodiscard]]
friend axis_aligned_rectangle operator*(axis_aligned_rectangle
const &lhs, T rhs)
noexcept
423 return (rhs.p0() + rhs.p3()) * 0.5f;
433 ttlet
extent = lhs.extent();
434 ttlet scaled_extent =
extent * rhs;
435 ttlet diff_extent = scaled_extent -
extent;
436 ttlet half_diff_extent = diff_extent * 0.5f;
438 ttlet p1 = lhs.p0() - half_diff_extent;
439 ttlet p2 = lhs.p3() + half_diff_extent;
496 auto p0 =
floor(rhs.p0());
497 auto p3 =
ceil(rhs.p3());
505 auto p0 =
ceil(rhs.p0());
506 auto p3 =
floor(rhs.p3());
515 ttlet p0 = max(lhs.p0(), rhs.p0());
516 ttlet p3 = max(p0, min(lhs.p3(), rhs.p3()));
532 std::min(rectangle.width(), bounds.width()),
533 std::min(rectangle.height(), bounds.height())};
535 ttlet translate_from_p0 = max(
numeric_array<T,4>{}, bounds.p0() - resized_rectangle.p0());
536 ttlet translate_from_p3 = min(
numeric_array<T,4>{}, bounds.p3() - resized_rectangle.p3());
537 return resized_rectangle + (translate_from_p0 + translate_from_p3);
541using aarect = axis_aligned_rectangle<float>;
542using iaarect = axis_aligned_rectangle<int>;
Class which represents an axis-aligned rectangle.
Definition aarect.hpp:17
axis_aligned_rectangle & operator-=(numeric_array< T, 4 > const &rhs) noexcept
Translate the box to a new position.
Definition aarect.hpp:168
friend axis_aligned_rectangle scale(axis_aligned_rectangle const &lhs, T rhs) noexcept
Expand the rectangle for the same amount in all directions.
Definition aarect.hpp:431
numeric_array< T, 4 > corner() const noexcept
Get coordinate of a corner.
Definition aarect.hpp:188
friend axis_aligned_rectangle expand(axis_aligned_rectangle const &lhs, numeric_array< T, 4 > rhs) noexcept
Expand the rectangle for the same amount in all directions.
Definition aarect.hpp:460
friend axis_aligned_rectangle ceil(axis_aligned_rectangle const &rhs) noexcept
Round rectangle by expanding to pixel edge.
Definition aarect.hpp:494
axis_aligned_rectangle(numeric_array< T, 4 > const &position, numeric_array< T, 4 > const &extent) noexcept
Create a rectangle from the position and size.
Definition aarect.hpp:65
T center() const noexcept
The center on the x-axis between left and right.
Definition aarect.hpp:279
friend axis_aligned_rectangle shrink(axis_aligned_rectangle const &lhs, numeric_array< T, 4 > rhs) noexcept
Shrink the rectangle for the same amount in all directions.
Definition aarect.hpp:482
static axis_aligned_rectangle p0p3(numeric_array< T, 4 > const &p0, numeric_array< T, 4 > const &p3) noexcept
Create axis_aligned_rectangle from two oposite points.
Definition aarect.hpp:97
friend axis_aligned_rectangle fit(axis_aligned_rectangle const &bounds, axis_aligned_rectangle const &rectangle) noexcept
Make a rectangle fit inside bounds.
Definition aarect.hpp:527
numeric_array< T, 4 > offset() const noexcept
Get vector from origin to the bottom-left corner.
Definition aarect.hpp:216
axis_aligned_rectangle(tt::arithmetic auto x, tt::arithmetic auto y, tt::arithmetic auto width, tt::arithmetic auto height) noexcept
Create a box from the position and size.
Definition aarect.hpp:42
friend axis_aligned_rectangle floor(axis_aligned_rectangle const &rhs) noexcept
Round rectangle by shrinking to pixel edge.
Definition aarect.hpp:503
axis_aligned_rectangle & operator|=(axis_aligned_rectangle const &rhs) noexcept
Expand the current rectangle to include the new rectangle.
Definition aarect.hpp:140
static axis_aligned_rectangle p0p3(numeric_array< T, 4 > const &v) noexcept
Create axis_aligned_rectangle from packed p0p3 coordinates.
Definition aarect.hpp:86
axis_aligned_rectangle & operator*=(T rhs) noexcept
Scale the box by moving the positions (scaling the vectors).
Definition aarect.hpp:177
friend axis_aligned_rectangle align(axis_aligned_rectangle haystack, axis_aligned_rectangle needle, alignment alignment) noexcept
Align a rectangle within another rectangle.
Definition aarect.hpp:312
friend numeric_array< T, 4 > center(axis_aligned_rectangle const &rhs) noexcept
Get the center of the rectangle.
Definition aarect.hpp:421
axis_aligned_rectangle(numeric_array< T, 4 > const &extent) noexcept
Create a rectangle from the size.
Definition aarect.hpp:77
T middle() const noexcept
The middle on the y-axis between bottom and top.
Definition aarect.hpp:272
numeric_array< T, 4 > extent() const noexcept
Get size of the rectangle.
Definition aarect.hpp:225
static axis_aligned_rectangle _align(axis_aligned_rectangle outside, axis_aligned_rectangle inside, alignment alignment) noexcept
Need to call the hiden friend function from within another class.
Definition aarect.hpp:347
bool valid() const noexcept
Make sure p0 is left/bottom from p3.
Definition aarect.hpp:116
bool contains(numeric_array< T, 4 > const &rhs) const noexcept
Check if a 2D coordinate is inside the rectangle.
Definition aarect.hpp:300
friend axis_aligned_rectangle shrink(axis_aligned_rectangle const &lhs, T rhs) noexcept
Shrink the rectangle for the same amount in all directions.
Definition aarect.hpp:471
axis_aligned_rectangle(tt::arithmetic auto width, tt::arithmetic auto height) noexcept
Create a box from the position and size.
Definition aarect.hpp:55
friend axis_aligned_rectangle expand(axis_aligned_rectangle const &lhs, T rhs) noexcept
Expand the rectangle for the same amount in all directions.
Definition aarect.hpp:449
friend axis_aligned_rectangle intersect(axis_aligned_rectangle const &lhs, axis_aligned_rectangle const &rhs) noexcept
Return the overlapping part of two rectangles.
Definition aarect.hpp:513
axis_aligned_rectangle & operator+=(numeric_array< T, 4 > const &rhs) noexcept
Translate the box to a new position.
Definition aarect.hpp:159
axis_aligned_rectangle & operator|=(numeric_array< T, 4 > const &rhs) noexcept
Expand the current rectangle to include the new point.
Definition aarect.hpp:150
bool empty() const noexcept
Check if the rectangle has no area.
Definition aarect.hpp:123
Definition sfloat_rgba32.hpp:14
Definition numeric_array.hpp:27
static constexpr numeric_array point() noexcept
Get a point at the origin.
Definition numeric_array.hpp:90
Definition concepts.hpp:15