From 72c31255c382f55be68a115b9a2fa0086905a901 Mon Sep 17 00:00:00 2001 From: Loukas Agorgianitis Date: Mon, 14 Apr 2025 11:38:47 +0200 Subject: [PATCH] util/box: introduce wlr_fbox_intersection Signed-off-by: Loukas Agorgianitis --- include/wlr/util/box.h | 8 ++++++++ util/box.c | 28 ++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/include/wlr/util/box.h b/include/wlr/util/box.h index 64dcbc5cf..816bece7e 100644 --- a/include/wlr/util/box.h +++ b/include/wlr/util/box.h @@ -61,6 +61,14 @@ void wlr_box_closest_point(const struct wlr_box *box, double x, double y, bool wlr_box_intersection(struct wlr_box *dest, const struct wlr_box *box_a, const struct wlr_box *box_b); +/** + * Gives the intersecting box between two struct wlr_fbox. + * + * Returns an empty box if the provided boxes don't intersect. + */ +bool wlr_fbox_intersection(struct wlr_fbox *dest, const struct wlr_fbox *box_a, + const struct wlr_fbox *box_b); + /** * Verifies if a point is contained within the bounds of a given struct wlr_box. * diff --git a/util/box.c b/util/box.c index aae09888f..087a33d19 100644 --- a/util/box.c +++ b/util/box.c @@ -74,6 +74,34 @@ bool wlr_box_intersection(struct wlr_box *dest, const struct wlr_box *box_a, return true; } +bool wlr_fbox_intersection(struct wlr_fbox *dest, const struct wlr_fbox *box_a, + const struct wlr_fbox *box_b) { + bool a_empty = wlr_fbox_empty(box_a); + bool b_empty = wlr_fbox_empty(box_b); + + if (a_empty || b_empty) { + *dest = (struct wlr_fbox){0}; + return false; + } + + double x1 = fmax(box_a->x, box_b->x); + double y1 = fmax(box_a->y, box_b->y); + double x2 = fmin(box_a->x + box_a->width, box_b->x + box_b->width); + double y2 = fmin(box_a->y + box_a->height, box_b->y + box_b->height); + + dest->x = x1; + dest->y = y1; + dest->width = x2 - x1; + dest->height = y2 - y1; + + if (wlr_fbox_empty(dest)) { + *dest = (struct wlr_fbox){0}; + return false; + } + + return true; +} + bool wlr_box_contains_point(const struct wlr_box *box, double x, double y) { if (wlr_box_empty(box)) { return false;