Skip to content

Commit

Permalink
stl: add Rect::with_size and Rect::bounding
Browse files Browse the repository at this point in the history
  • Loading branch information
matcool committed Jan 31, 2025
1 parent 84f3d49 commit 0df0dd0
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions stl/math.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ struct Rect {
Point pos;
Point size;

Rect() {}

Rect(Type x, Type y, Type width, Type height) : pos(x, y), size(width, height) {}

Rect(const Point& pos, const Point& size) : pos(pos), size(size) {}
Expand All @@ -108,6 +110,12 @@ struct Rect {
return Rect(top_left, bot_right - top_left + 1);
}

static Rect bounding(const Rect& a, const Rect& b) {
const auto top_left = Point(min(a.left(), b.left()), min(a.top(), b.top()));
const auto bot_right = Point(max(a.right(), b.right()), max(a.bottom(), b.bottom()));
return from_corners(top_left, bot_right);
}

bool contains(const Point& point) const {
return point.x >= pos.x && point.x < pos.x + size.width && point.y >= pos.y &&
point.y < pos.y + size.height;
Expand Down Expand Up @@ -155,7 +163,11 @@ struct Rect {

Rect with_pos(const Point& point) const { return Rect(point, size); }

Rect with_size(const Point& new_size) const { return Rect(pos, new_size); }

Rect reset_pos() const { return with_pos(Point()); }

bool operator<=>(const Rect&) const = default;
};

}

0 comments on commit 0df0dd0

Please sign in to comment.