summaryrefslogtreecommitdiff
path: root/rect.c
diff options
context:
space:
mode:
Diffstat (limited to 'rect.c')
-rw-r--r--rect.c96
1 files changed, 96 insertions, 0 deletions
diff --git a/rect.c b/rect.c
new file mode 100644
index 0000000..bcc95cd
--- /dev/null
+++ b/rect.c
@@ -0,0 +1,96 @@
+#include "maths.h"
+#include "rect.h"
+
+Rectangle make_rect(int x, int y, int w, int h) {
+ Rectangle r;
+ r.x = x;
+ r.y = y;
+ r.w = w;
+ r.h = h;
+ return r;
+}
+
+int rects_overlap(const Rectangle* a, const Rectangle* b) {
+ return
+ a->x + a->w > b->x &&
+ a->y + a->h > b->y &&
+ a->x < b->x + b->w &&
+ a->y < b->y + b->h;
+}
+
+int rects_overlap2(
+ int x0,
+ int y0,
+ int w0,
+ int h0,
+ int x1,
+ int y1,
+ int w1,
+ int h1
+) {
+ return
+ x0 + w0 > x1 &&
+ y0 + h0 > y1 &&
+ x0 < x1 + w1 &&
+ y0 < y1 + h1;
+}
+
+int point_rect_overlap(
+ const Rectangle* r,
+ int px,
+ int py
+) {
+ return
+ px >= r->x &&
+ py >= r->y &&
+ px <= r->x + r->w &&
+ py <= r->y + r->h;
+}
+
+int point_rect_overlap2(
+ int x,
+ int y,
+ int w,
+ int h,
+ int px,
+ int py
+) {
+ return
+ px > x &&
+ py > y &&
+ px < x + w &&
+ py < y + h;
+}
+
+Rectangle rect_intersect(
+ const Rectangle* a,
+ const Rectangle* b
+) {
+ int x1, y1, x2, y2;
+ Rectangle r;
+ x1 = maxi(a->x, b->x);
+ y1 = maxi(a->y, b->y);
+ x2 = mini(a->x + a->w, b->x + b->w);
+ y2 = mini(a->y + a->h, b->y + b->h);
+ r.x = x1;
+ r.y = y1;
+ r.w = maxi(0, x2 - x1);
+ r.h = maxi(0, y2 - y1);
+ return r;
+}
+
+Rectangle* rect_merge(
+ Rectangle* d,
+ const Rectangle* r
+) {
+ int x1, y1, x2, y2;
+ x1 = mini(d->x, r->x);
+ y1 = mini(d->y, r->y);
+ x2 = maxi(d->x + d->w, r->x + r->w);
+ y2 = maxi(d->y + d->h, r->y + r->h);
+ d->x = x1;
+ d->y = y1;
+ d->w = x2 - x1;
+ d->h = y2 - y1;
+ return d;
+}