util/box: introduce wlr_fbox_intersection

Signed-off-by: Loukas Agorgianitis <loukas@agorgianitis.com>
This commit is contained in:
Loukas Agorgianitis 2025-04-14 11:38:47 +02:00
parent 62234c5133
commit 72c31255c3
No known key found for this signature in database
GPG key ID: DDC6FA7D5BB332E6
2 changed files with 36 additions and 0 deletions

View file

@ -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.
*

View file

@ -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;