Skip to content

Commit f40c0c1

Browse files
committed
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.
1 parent 47ca792 commit f40c0c1

File tree

2 files changed

+23
-14
lines changed

2 files changed

+23
-14
lines changed

shared-module/displayio/__init__.c

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -309,26 +309,33 @@ bool displayio_area_compute_overlap(const displayio_area_t *a,
309309
return true;
310310
}
311311

312+
void displayio_copy_coords(const displayio_area_t *src, displayio_area_t *dest) {
313+
dest->x1 = src->x1;
314+
dest->y1 = src->y1;
315+
dest->x2 = src->x2;
316+
dest->y2 = src->y2;
317+
}
318+
319+
bool displayio_area_empty(const displayio_area_t *a) {
320+
return (a->x1 == a->x2) || (a->y1 == a->y2);
321+
}
322+
312323
void displayio_area_union(const displayio_area_t *a,
313324
const displayio_area_t *b,
314325
displayio_area_t *u) {
315-
u->x1 = a->x1;
316-
if (b->x1 < u->x1) {
317-
u->x1 = b->x1;
318-
}
319-
u->x2 = a->x2;
320-
if (b->x2 > u->x2) {
321-
u->x2 = b->x2;
322-
}
323326

324-
u->y1 = a->y1;
325-
if (b->y1 < u->y1) {
326-
u->y1 = b->y1;
327+
if (displayio_area_empty(a)) {
328+
displayio_copy_coords(b, u);
329+
return;
327330
}
328-
u->y2 = a->y2;
329-
if (b->y2 > u->y2) {
330-
u->y2 = b->y2;
331+
if (displayio_area_empty(b)) {
332+
displayio_copy_coords(a, u);
333+
return;
331334
}
335+
u->x1 = MIN(a->x1, b->x1);
336+
u->y1 = MIN(a->y1, b->y1);
337+
u->x2 = MAX(a->x2, b->x2);
338+
u->y2 = MAX(a->y2, b->y2);
332339
}
333340

334341
uint16_t displayio_area_width(const displayio_area_t *area) {

shared-module/displayio/area.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ typedef struct {
5353

5454
extern displayio_buffer_transform_t null_transform;
5555

56+
bool displayio_area_empty(const displayio_area_t *a);
57+
void displayio_area_copy_coords(const displayio_area_t *src, displayio_area_t *dest);
5658
void displayio_area_union(const displayio_area_t *a,
5759
const displayio_area_t *b,
5860
displayio_area_t *u);

0 commit comments

Comments
 (0)