From f40c0c13adacf39bc9a21afd2d9ebbf29135e0f0 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Thu, 18 Mar 2021 09:04:53 -0500 Subject: [PATCH] displayio: area: add displayo_area_copy_coords, displayio_area_empty .. and simplify the implmentation of displayio_area_union This _slightly_ changes the behavior of displayio_area_union: Formerly, if one of the areas was empty, its coordinates were still used in the min/max calculations. Now, if one of the areas is empty, the result gets the other area's coords In particular, taking the union of the empty area with coords (0,0,0,0) with the non-empty area (x1,y1,x2,y2) would give the area (0,0,x2,y2) before, and (x1,y1,x2,y2) after the change. --- shared-module/displayio/__init__.c | 35 ++++++++++++++++++------------ shared-module/displayio/area.h | 2 ++ 2 files changed, 23 insertions(+), 14 deletions(-) diff --git a/shared-module/displayio/__init__.c b/shared-module/displayio/__init__.c index 3c9c2cc3ac..07fd603861 100644 --- a/shared-module/displayio/__init__.c +++ b/shared-module/displayio/__init__.c @@ -309,26 +309,33 @@ bool displayio_area_compute_overlap(const displayio_area_t *a, return true; } +void displayio_copy_coords(const displayio_area_t *src, displayio_area_t *dest) { + dest->x1 = src->x1; + dest->y1 = src->y1; + dest->x2 = src->x2; + dest->y2 = src->y2; +} + +bool displayio_area_empty(const displayio_area_t *a) { + return (a->x1 == a->x2) || (a->y1 == a->y2); +} + void displayio_area_union(const displayio_area_t *a, const displayio_area_t *b, displayio_area_t *u) { - u->x1 = a->x1; - if (b->x1 < u->x1) { - u->x1 = b->x1; - } - u->x2 = a->x2; - if (b->x2 > u->x2) { - u->x2 = b->x2; - } - u->y1 = a->y1; - if (b->y1 < u->y1) { - u->y1 = b->y1; + if (displayio_area_empty(a)) { + displayio_copy_coords(b, u); + return; } - u->y2 = a->y2; - if (b->y2 > u->y2) { - u->y2 = b->y2; + if (displayio_area_empty(b)) { + displayio_copy_coords(a, u); + return; } + u->x1 = MIN(a->x1, b->x1); + u->y1 = MIN(a->y1, b->y1); + u->x2 = MAX(a->x2, b->x2); + u->y2 = MAX(a->y2, b->y2); } uint16_t displayio_area_width(const displayio_area_t *area) { diff --git a/shared-module/displayio/area.h b/shared-module/displayio/area.h index 21bf2f8b25..1b30ce96fe 100644 --- a/shared-module/displayio/area.h +++ b/shared-module/displayio/area.h @@ -53,6 +53,8 @@ typedef struct { extern displayio_buffer_transform_t null_transform; +bool displayio_area_empty(const displayio_area_t *a); +void displayio_area_copy_coords(const displayio_area_t *src, displayio_area_t *dest); void displayio_area_union(const displayio_area_t *a, const displayio_area_t *b, displayio_area_t *u);