[nostalgia/geo] Add ability to init Bounds from two Points

This commit is contained in:
Gary Talent 2022-03-10 02:38:25 -06:00
parent 0b6a36bedc
commit 3486734b50

View File

@ -25,12 +25,17 @@ class Bounds {
constexpr Bounds(int x, int y, int w, int h) noexcept;
constexpr Bounds(const Point &pt1, const Point &pt2) noexcept;
[[nodiscard]]
constexpr bool intersects(const Bounds &other) const noexcept;
[[nodiscard]]
constexpr bool contains(int x, int y) const noexcept;
[[nodiscard]]
constexpr bool contains(const Point &pt) const noexcept;
[[nodiscard]]
constexpr int x2() const noexcept;
@ -52,6 +57,28 @@ constexpr Bounds::Bounds(int x, int y, int w, int h) noexcept {
this->height = h;
}
constexpr Bounds::Bounds(const Point &pt1, const Point &pt2) noexcept {
int x1 = 0, x2 = 0, y1 = 0, y2 = 0;
if (pt1.x <= pt2.x) {
x1 = pt1.x;
x2 = pt2.x;
} else {
x1 = pt2.x;
x2 = pt1.x;
}
if (pt1.y <= pt2.y) {
y1 = pt1.y;
y2 = pt2.y;
} else {
y1 = pt2.y;
y2 = pt1.y;
}
this->x = x1;
this->y = y1;
this->width = x2 - x1;
this->height = y2 - y1;
}
constexpr bool Bounds::intersects(const Bounds &o) const noexcept {
return o.x2() >= x && x2() >= o.x && o.y2() >= y && y2() >= o.y;
}
@ -60,6 +87,10 @@ constexpr bool Bounds::contains(int x, int y) const noexcept {
return x >= this->x && y >= this->y && x <= x2() && y <= y2();
}
constexpr bool Bounds::contains(const Point &pt) const noexcept {
return contains(pt.x, pt.y);
}
constexpr int Bounds::x2() const noexcept {
return x + width;
}