PatchworkOS
Loading...
Searching...
No Matches
rect.h
Go to the documentation of this file.
1#ifndef PATCHWORK_RECT_H
2#define PATCHWORK_RECT_H 1
3
4#include <stdint.h>
5#include <sys/math.h>
6
7#if defined(__cplusplus)
8extern "C"
9{
10#endif
11
19
20typedef struct
21{
22 rect_t rects[4];
25
26#define RECT_INIT(left, top, right, bottom) \
27 (rect_t) \
28 { \
29 left, top, right, bottom, \
30 }
31
32#define RECT_INIT_DIM(x, y, width, height) \
33 (rect_t) \
34 { \
35 (x), (y), (x) + (width), (y) + (height), \
36 }
37
38#define RECT_WIDTH(rect) ((rect)->right - (rect)->left)
39#define RECT_HEIGHT(rect) ((rect)->bottom - (rect)->top)
40#define RECT_AREA(rect) (RECT_WIDTH(rect) * RECT_HEIGHT(rect))
41
42#define RECT_HAS_NEGATIVE_DIMS(rect) (RECT_WIDTH(rect) < 0 || RECT_HEIGHT(rect) < 0)
43
44#define RECT_EXPAND_TO_CONTAIN(rect, other) \
45 ({ \
46 (rect)->left = MIN((rect)->left, (other)->left); \
47 (rect)->top = MIN((rect)->top, (other)->top); \
48 (rect)->right = MAX((rect)->right, (other)->right); \
49 (rect)->bottom = MAX((rect)->bottom, (other)->bottom); \
50 })
51
52#define RECT_EQUAL(rect, other) \
53 ((other)->left == (rect)->left && (other)->right == (rect)->right && (other)->top == (rect)->top && \
54 (other)->bottom == (rect)->bottom)
55
56#define RECT_CONTAINS(rect, other) \
57 ((rect)->left <= (rect)->right && (rect)->top <= (rect)->bottom && (other)->left <= (other)->right && \
58 (other)->top <= (other)->bottom && (other)->left >= (rect)->left && (other)->right <= (rect)->right && \
59 (other)->top >= (rect)->top && (other)->bottom <= (rect)->bottom)
60
61#define RECT_CONTAINS_POINT(rect, point) \
62 ((point)->x >= (rect)->left && (point)->x < (rect)->right && (point)->y >= (rect)->top && \
63 (point)->y < (rect)->bottom)
64
65#define RECT_OVERLAP(rect, other) \
66 (!((rect)->right <= (other)->left || (rect)->left >= (other)->right || (rect)->bottom <= (other)->top || \
67 (rect)->top >= (other)->bottom))
68
69#define RECT_OVERLAP_STRICT(rect, other) \
70 (!((rect)->right < (other)->left || (rect)->left > (other)->right || (rect)->bottom < (other)->top || \
71 (rect)->top > (other)->bottom))
72
73#define RECT_FIT(rect, parent) \
74 ({ \
75 (rect)->left = CLAMP((rect)->left, (parent)->left, (parent)->right); \
76 (rect)->top = CLAMP((rect)->top, (parent)->top, (parent)->bottom); \
77 (rect)->right = CLAMP((rect)->right, (parent)->left, (parent)->right); \
78 (rect)->bottom = CLAMP((rect)->bottom, (parent)->top, (parent)->bottom); \
79 })
80
81#define RECT_SHRINK(rect, margin) \
82 ({ \
83 (rect)->left += margin; \
84 (rect)->top += margin; \
85 (rect)->right -= margin; \
86 (rect)->bottom -= margin; \
87 })
88
89#define RECT_EXPAND(rect, margin) \
90 ({ \
91 (rect)->left -= margin; \
92 (rect)->top -= margin; \
93 (rect)->right += margin; \
94 (rect)->bottom += margin; \
95 })
96
97#define RECT_SUBTRACT(result, rect, other) \
98 ({ \
99 rect_subtract_t res = {.count = 0}; \
100 if (!RECT_OVERLAP(rect, other)) \
101 { \
102 res.rects[0] = *(rect); \
103 res.count = 1; \
104 } \
105 else \
106 { \
107 if ((other)->top > (rect)->top) \
108 { \
109 res.rects[res.count++] = (rect_t){(rect)->left, (rect)->top, (rect)->right, (other)->top}; \
110 } \
111 if ((other)->bottom < (rect)->bottom) \
112 { \
113 res.rects[res.count++] = (rect_t){(rect)->left, (other)->bottom, (rect)->right, (rect)->bottom}; \
114 } \
115 if ((other)->left > (rect)->left) \
116 { \
117 res.rects[res.count++] = (rect_t){(rect)->left, MAX((rect)->top, (other)->top), (other)->left, \
118 MIN((rect)->bottom, (other)->bottom)}; \
119 } \
120 if ((other)->right < (rect)->right) \
121 { \
122 res.rects[res.count++] = (rect_t){(other)->right, MAX((rect)->top, (other)->top), (rect)->right, \
123 MIN((rect)->bottom, (other)->bottom)}; \
124 } \
125 } \
126 *(result) = res; \
127 })
128
129#define RECT_INTERSECT(dest, src1, src2) \
130 ({ \
131 (dest)->left = MAX((src1)->left, (src2)->left); \
132 (dest)->top = MAX((src1)->top, (src2)->top); \
133 (dest)->right = MIN((src1)->right, (src2)->right); \
134 (dest)->bottom = MIN((src1)->bottom, (src2)->bottom); \
135 })
136
137#if defined(__cplusplus)
138}
139#endif
140
141#endif
__INT32_TYPE__ int32_t
Definition stdint.h:14
__UINT8_TYPE__ uint8_t
Definition stdint.h:11
uint8_t count
Definition rect.h:23
Definition rect.h:13
int32_t bottom
Definition rect.h:17
int32_t top
Definition rect.h:15
int32_t right
Definition rect.h:16
int32_t left
Definition rect.h:14