forked from google/xsecurelock
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrect.c
More file actions
127 lines (108 loc) · 3.11 KB
/
Copy pathrect.c
File metadata and controls
127 lines (108 loc) · 3.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include "config.h"
#include "rect.h"
#include <limits.h>
#include <stdint.h>
static bool Int64ToInt(int64_t value, int *out) {
if (value < INT_MIN || value > INT_MAX) {
return false;
}
*out = (int)value;
return true;
}
static bool RectEndChecked(int start, int size, int *end) {
if (size < 0) {
return false;
}
return Int64ToInt((int64_t)start + (int64_t)size, end);
}
static bool RectIsEmpty(Rect rect) { return rect.w <= 0 || rect.h <= 0; }
static int64_t RectRight(Rect rect) { return (int64_t)rect.x + rect.w; }
static int64_t RectBottom(Rect rect) { return (int64_t)rect.y + rect.h; }
bool RectIntersect(Rect a, Rect b, Rect *out) {
int rect_right = 0;
int rect_bottom = 0;
int clip_right = 0;
int clip_bottom = 0;
if (RectIsEmpty(a) || RectIsEmpty(b) ||
!RectEndChecked(a.x, a.w, &rect_right) ||
!RectEndChecked(a.y, a.h, &rect_bottom) ||
!RectEndChecked(b.x, b.w, &clip_right) ||
!RectEndChecked(b.y, b.h, &clip_bottom)) {
return false;
}
int left = a.x > b.x ? a.x : b.x;
int top = a.y > b.y ? a.y : b.y;
int right = rect_right < clip_right ? rect_right : clip_right;
int bottom = rect_bottom < clip_bottom ? rect_bottom : clip_bottom;
if (right <= left || bottom <= top) {
return false;
}
int width = 0;
int height = 0;
if (!Int64ToInt((int64_t)right - (int64_t)left, &width) ||
!Int64ToInt((int64_t)bottom - (int64_t)top, &height)) {
return false;
}
*out = (Rect){
.x = left,
.y = top,
.w = width,
.h = height,
};
return true;
}
bool RectContainsPoint(Rect rect, int x, int y) {
int right = 0;
int bottom = 0;
return !RectIsEmpty(rect) && RectEndChecked(rect.x, rect.w, &right) &&
RectEndChecked(rect.y, rect.h, &bottom) && x >= rect.x && x < right &&
y >= rect.y && y < bottom;
}
size_t RectSubtract(Rect old_rect, Rect new_rect, Rect out[4]) {
if (RectIsEmpty(old_rect)) {
return false;
}
Rect intersection;
if (!RectIntersect(old_rect, new_rect, &intersection)) {
out[0] = old_rect;
return true;
}
size_t count = 0;
int64_t old_right = RectRight(old_rect);
int64_t old_bottom = RectBottom(old_rect);
int64_t intersection_right = RectRight(intersection);
int64_t intersection_bottom = RectBottom(intersection);
if (intersection.y > old_rect.y) {
out[count++] = (Rect){
.x = old_rect.x,
.y = old_rect.y,
.w = old_rect.w,
.h = intersection.y - old_rect.y,
};
}
if (intersection_bottom < old_bottom) {
out[count++] = (Rect){
.x = old_rect.x,
.y = (int)intersection_bottom,
.w = old_rect.w,
.h = (int)(old_bottom - intersection_bottom),
};
}
if (intersection.x > old_rect.x) {
out[count++] = (Rect){
.x = old_rect.x,
.y = intersection.y,
.w = intersection.x - old_rect.x,
.h = intersection.h,
};
}
if (intersection_right < old_right) {
out[count++] = (Rect){
.x = (int)intersection_right,
.y = intersection.y,
.w = (int)(old_right - intersection_right),
.h = intersection.h,
};
}
return count;
}