microui  14.5.5
microui
Macros | Functions
ui_rect_util.h File Reference

Utility functions to handle rectangles. More...

#include <stddef.h>
#include "ui_rect.h"
#include "ui_util.h"

Go to the source code of this file.

Macros

#define UI_RECT_EMPTY   { 1u, 1u, 0u, 0u }
 The initializer of an empty rectangle: its end (x2,y2) is before its start (x1,y1).
 

Functions

ui_rect_t UI_RECT_get_minimum_bounding_rect (const ui_rect_t rects[], const size_t count)
 Computes the minimum bounding rectangle of an array of rectangles. More...
 
uint32_t UI_RECT_union (ui_rect_t output[3], const ui_rect_t *first, const ui_rect_t *second)
 Computes the union of two rectangles. More...
 
uint32_t UI_RECT_subtract (ui_rect_t output[4], const ui_rect_t *first, const ui_rect_t *second)
 Computes the difference between two rectangles. More...
 

Detailed Description

Utility functions to handle rectangles.

See also
ui_rect_t
Author
MicroEJ Developer Team
Version
14.5.5

Definition in file ui_rect_util.h.

Function Documentation

◆ UI_RECT_get_minimum_bounding_rect()

ui_rect_t UI_RECT_get_minimum_bounding_rect ( const ui_rect_t  rects[],
const size_t  count 
)

Computes the minimum bounding rectangle of an array of rectangles.

The behavior is undefined if the array is empty.

Parameters
[in]rectsthe array of rectangles
[in]countthe length of the array
Returns
the minimum bounding rectangle

Definition at line 32 of file ui_rect_util.c.

32  {
33  assert(count > 0u);
34  ui_rect_t result = rects[0];
35  for (size_t i = 1; i < count; i++) {
36  result = UI_RECT_get_minimum_bounding_rect_two_rects(&result, &rects[i]);
37  }
38  return result;
39 }

◆ UI_RECT_subtract()

uint32_t UI_RECT_subtract ( ui_rect_t  output[4],
const ui_rect_t *  first,
const ui_rect_t *  second 
)

Computes the difference between two rectangles.

Parameters
[out]outputthe rectangles defining the difference between the parameters
[in]firstthe original rectangle
[in]secondthe rectangle that will be removed from the original one
Returns
the number of resulting rectangles. Some of them may be empty.

Definition at line 89 of file ui_rect_util.c.

89  {
90  uint32_t ret;
91  if (UI_RECT_contains_rect(first, second)) {
92  output[0] = UI_RECT_new_xyxy(first->x1, first->y1, first->x2, second->y1 - 1); // Top
93  output[1] = UI_RECT_new_xyxy(first->x1, second->y1, second->x1 - 1, second->y2); // Left
94  output[2] = UI_RECT_new_xyxy(second->x2 + 1, second->y1, first->x2, second->y2); // Right
95  output[3] = UI_RECT_new_xyxy(first->x1, second->y2 + 1, first->x2, first->y2); // Bottom
96  ret = 4u;
97  } else if (UI_RECT_contains_rect(second, first)) {
98  UI_RECT_mark_empty(&output[0]);
99  UI_RECT_mark_empty(&output[1]);
100  UI_RECT_mark_empty(&output[2]);
101  UI_RECT_mark_empty(&output[3]);
102  ret = 0u;
103  } else if (!UI_RECT_intersects_rect(first, second)) {
104  output[0] = *first;
105  UI_RECT_mark_empty(&output[1]);
106  UI_RECT_mark_empty(&output[2]);
107  UI_RECT_mark_empty(&output[3]);
108  ret = 1u;
109  } else {
110  output[0] = UI_RECT_new_xyxy(first->x1, first->y1, first->x2, second->y1 - 1); // Top
111  output[1] = UI_RECT_new_xyxy(first->x1, MAX(first->y1, second->y1), second->x1 - 1, MIN(first->y2, second->y2)); // Left
112  output[2] = UI_RECT_new_xyxy(second->x2 + 1, MAX(first->y1, second->y1), first->x2, MIN(first->y2, second->y2)); // Right
113  output[3] = UI_RECT_new_xyxy(first->x1, second->y2 + 1, first->x2, first->y2); // Bottom
114  ret = 4u;
115  }
116  return ret;
117 }
#define MIN(a, b)
Gets the lowest value.
Definition: ui_util.h:31
#define MAX(a, b)
Gets the highest value.
Definition: ui_util.h:38

◆ UI_RECT_union()

uint32_t UI_RECT_union ( ui_rect_t  output[3],
const ui_rect_t *  first,
const ui_rect_t *  second 
)

Computes the union of two rectangles.

Parameters
[out]outputrectangles defining the union of the parameters
[in]firstthe first rectangle
[in]secondthe second rectangle
Returns
the number of resulting rectangles. Some of them may be empty.

Definition at line 42 of file ui_rect_util.c.

42  {
43  uint32_t ret;
44  if (UI_RECT_contains_rect(first, second)) {
45  output[0] = *first;
46  UI_RECT_mark_empty(&output[1]);
47  UI_RECT_mark_empty(&output[2]);
48  ret = 1u;
49  } else if (UI_RECT_contains_rect(second, first)) {
50  output[0] = *second;
51  UI_RECT_mark_empty(&output[1]);
52  UI_RECT_mark_empty(&output[2]);
53  ret = 1u;
54  } else if (!UI_RECT_intersects_rect(first, second)) {
55  output[0] = *first;
56  output[1] = *second;
57  UI_RECT_mark_empty(&output[2]);
58  ret = 2u;
59  } else {
60  const ui_rect_t *top_above = (first->y1 < second->y1) ? first : second;
61  const ui_rect_t *top_below = (first->y1 < second->y1) ? second : first;
62  const ui_rect_t *bottom_below = (first->y2 > second->y2) ? first : second;
63  const ui_rect_t *bottom_above = (first->y2 > second->y2) ? second : first;
64 
65  uint32_t count = 0u;
66 
67  output[count] = UI_RECT_new_xyxy(top_above->x1, top_above->y1, top_above->x2, top_below->y1 - 1);
68  if (!UI_RECT_is_empty(output)) {
69  count++;
70  }
71 
72  output[count] = UI_RECT_new_xyxy(MIN(first->x1, second->x1), top_below->y1, MAX(first->x2, second->x2),
73  bottom_above->y2);
74  if (!UI_RECT_is_empty(output)) {
75  count++;
76  }
77 
78  output[count] = UI_RECT_new_xyxy(bottom_below->x1, bottom_above->y2 + 1, bottom_below->x2, bottom_below->y2);
79  if (!UI_RECT_is_empty(output)) {
80  count++;
81  }
82 
83  ret = count;
84  }
85  return ret;
86 }