-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrect.c
53 lines (44 loc) · 1.53 KB
/
rect.c
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
#include "shape.h"
// true if pixel is in rect centerPosed at rectPos
int
abRectCheck(const AbRect *rect, const Vec2 *centerPos, const Vec2 *pixel)
{
Region bounds;
abRectGetBounds(rect, centerPos, &bounds);
int within = 1, axis;
for (axis = 0; axis < 2; axis ++) {
int p = pixel->axes[axis];
if (p > bounds.botRight.axes[axis] || p < bounds.topLeft.axes[axis])
within = 0;
}
return within;
}
// compute bounding box in screen coordinates for rect at centerPos
void abRectGetBounds(const AbRect *rect, const Vec2 *centerPos, Region *bounds)
{
vec2Sub(&bounds->topLeft, centerPos, &rect->halfSize);
vec2Add(&bounds->botRight, centerPos, &rect->halfSize);
}
// true if pixel is in rect centerPosed at rectPos
int
abRectOutlineCheck(const AbRectOutline *rect, const Vec2 *centerPos, const Vec2 *pixel)
{
Region bounds;
abRectOutlineGetBounds(rect, centerPos, &bounds);
int col = pixel->axes[0], row = pixel->axes[1];
return (
((col == bounds.topLeft.axes[0] || col == bounds.botRight.axes[0])
&&
(row >= bounds.topLeft.axes[1] && row <= bounds.botRight.axes[1]))
||
((row == bounds.topLeft.axes[1] || row == bounds.botRight.axes[1])
&&
(col >= bounds.topLeft.axes[0] && col <= bounds.botRight.axes[0]))
);
}
// compute bounding box in screen coordinates for rect at centerPos
void abRectOutlineGetBounds(const AbRectOutline *rect, const Vec2 *centerPos, Region *bounds)
{
vec2Sub(&bounds->topLeft, centerPos, &rect->halfSize);
vec2Add(&bounds->botRight, centerPos, &rect->halfSize);
}