45 lines
1.1 KiB
C
45 lines
1.1 KiB
C
#ifndef GAME_RECT_H
|
|
#define GAME_RECT_H
|
|
|
|
struct Rect {
|
|
int x1, y1, x2, y2;
|
|
|
|
void clamp(int minX, int minY, int maxX, int maxY) {
|
|
if (x1 < minX) x1 = minX;
|
|
if (x2 > maxX) x2 = maxX;
|
|
if (y1 < minY) y1 = minY;
|
|
if (y2 > maxY) y2 = maxY;
|
|
}
|
|
|
|
void sort() {
|
|
if (x1 > x2) {
|
|
auto x = x1;
|
|
x1 = x2;
|
|
x2 = x;
|
|
}
|
|
if (y1 > y2) {
|
|
auto y = y1;
|
|
y1 = y2;
|
|
y2 = y;
|
|
}
|
|
}
|
|
|
|
Rect &operator+=(const Rect &other) {
|
|
if (other.x1 < x1) x1 = other.x1;
|
|
if (other.x2 > x2) x2 = other.x2;
|
|
if (other.y1 < y1) y1 = other.y1;
|
|
if (other.y2 > y2) y2 = other.y2;
|
|
|
|
return *this;
|
|
}
|
|
|
|
[[nodiscard]] bool contains(int x, int y) const {
|
|
return x >= x1 && y >= y1 && x < x2 && y < y2;
|
|
}
|
|
|
|
[[nodiscard]] bool overlaps(const Rect &other) const {
|
|
return !(other.x1 > x2 || other.x2 < x1 || other.y1 > y2 || other.y2 < y1);
|
|
}
|
|
};
|
|
|
|
#endif //GAME_RECT_H
|