Loading...
Searching...
No Matches
Go to the documentation of this file. 1#ifndef PATCHWORK_RECT_H
2#define PATCHWORK_RECT_H 1
7#if defined(__cplusplus)
26#define RECT_INIT(left, top, right, bottom) \
29 left, top, right, bottom, \
32#define RECT_INIT_DIM(x, y, width, height) \
35 (x), (y), (x) + (width), (y) + (height), \
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))
42#define RECT_HAS_NEGATIVE_DIMS(rect) (RECT_WIDTH(rect) < 0 || RECT_HEIGHT(rect) < 0)
44#define RECT_EXPAND_TO_CONTAIN(rect, other) \
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); \
52#define RECT_EQUAL(rect, other) \
53 ((other)->left == (rect)->left && (other)->right == (rect)->right && (other)->top == (rect)->top && \
54 (other)->bottom == (rect)->bottom)
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)
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)
65#define RECT_OVERLAP(rect, other) \
66 (!((rect)->right <= (other)->left || (rect)->left >= (other)->right || (rect)->bottom <= (other)->top || \
67 (rect)->top >= (other)->bottom))
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))
73#define RECT_FIT(rect, parent) \
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); \
81#define RECT_SHRINK(rect, margin) \
83 (rect)->left += margin; \
84 (rect)->top += margin; \
85 (rect)->right -= margin; \
86 (rect)->bottom -= margin; \
89#define RECT_EXPAND(rect, margin) \
91 (rect)->left -= margin; \
92 (rect)->top -= margin; \
93 (rect)->right += margin; \
94 (rect)->bottom += margin; \
97#define RECT_SUBTRACT(result, rect, other) \
99 rect_subtract_t res = {.count = 0}; \
100 if (!RECT_OVERLAP(rect, other)) \
102 res.rects[0] = *(rect); \
107 if ((other)->top > (rect)->top) \
109 res.rects[res.count++] = (rect_t){(rect)->left, (rect)->top, (rect)->right, (other)->top}; \
111 if ((other)->bottom < (rect)->bottom) \
113 res.rects[res.count++] = (rect_t){(rect)->left, (other)->bottom, (rect)->right, (rect)->bottom}; \
115 if ((other)->left > (rect)->left) \
117 res.rects[res.count++] = (rect_t){(rect)->left, MAX((rect)->top, (other)->top), (other)->left, \
118 MIN((rect)->bottom, (other)->bottom)}; \
120 if ((other)->right < (rect)->right) \
122 res.rects[res.count++] = (rect_t){(other)->right, MAX((rect)->top, (other)->top), (rect)->right, \
123 MIN((rect)->bottom, (other)->bottom)}; \
129#define RECT_INTERSECT(dest, src1, src2) \
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); \
137#if defined(__cplusplus)