PatchworkOS
966e257
A non-POSIX operating system.
Theme:
Default
Round
Robot
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)
8
extern
"C"
9
{
10
#endif
11
12
typedef
struct
rect
13
{
14
int32_t
left
;
15
int32_t
top
;
16
int32_t
right
;
17
int32_t
bottom
;
18
}
rect_t
;
19
20
typedef
struct
21
{
22
rect_t
rects[4];
23
uint8_t
count
;
24
}
rect_subtract_t
;
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
stdint.h
int32_t
__INT32_TYPE__ int32_t
Definition
stdint.h:14
uint8_t
__UINT8_TYPE__ uint8_t
Definition
stdint.h:11
rect_subtract_t
Definition
rect.h:21
rect_subtract_t::count
uint8_t count
Definition
rect.h:23
rect_t
Definition
rect.h:13
rect_t::bottom
int32_t bottom
Definition
rect.h:17
rect_t::top
int32_t top
Definition
rect.h:15
rect_t::right
int32_t right
Definition
rect.h:16
rect_t::left
int32_t left
Definition
rect.h:14
math.h
include
libpatchwork
rect.h
Generated on Mon Dec 15 2025 21:55:53 for PatchworkOS by
1.9.8