PatchworkOS
966e257
A non-POSIX operating system.
Theme:
Default
Round
Robot
Loading...
Searching...
No Matches
polygon.c
Go to the documentation of this file.
1
#include <
libpatchwork/polygon.h
>
2
#include <math.h>
3
4
void
polygon_rotate
(
point_t
* points,
uint64_t
pointCount,
double
angle,
point_t
center)
5
{
6
if
(points ==
NULL
|| pointCount == 0)
7
{
8
return
;
9
}
10
11
double
cosAngle =
cos
(angle);
12
double
sinAngle =
sin
(angle);
13
14
for
(
uint64_t
i = 0; i < pointCount; i++)
15
{
16
double
translatedX = points[i].
x
- center.
x
;
17
double
translatedY = points[i].
y
- center.
y
;
18
19
int64_t
rotatedX = (
int64_t
)
round
(translatedX * cosAngle - translatedY * sinAngle);
20
int64_t
rotatedY = (
int64_t
)
round
(translatedX * sinAngle + translatedY * cosAngle);
21
22
points[i].
x
= rotatedX + center.
x
;
23
points[i].
y
= rotatedY + center.
y
;
24
}
25
}
26
27
bool
polygon_contains
(
double
px,
double
py,
const
point_t
* points,
uint64_t
pointCount)
28
{
29
int
winding = 0;
30
31
for
(
uint64_t
i = 0; i < pointCount; i++)
32
{
33
point_t
p1 = points[i];
34
point_t
p2 = points[(i + 1) % pointCount];
35
36
if
(p1.
y
<= py)
37
{
38
if
(p2.
y
> py)
39
{
40
double
vt = (py - p1.
y
) / (
double
)(p2.
y
- p1.
y
);
41
if
(px < p1.
x
+ vt * (p2.
x
- p1.
x
))
42
{
43
winding++;
44
}
45
}
46
}
47
else
48
{
49
if
(p2.
y
<= py)
50
{
51
double
vt = (py - p1.
y
) / (
double
)(p2.
y
- p1.
y
);
52
if
(px < p1.
x
+ vt * (p2.
x
- p1.
x
))
53
{
54
winding--;
55
}
56
}
57
}
58
}
59
60
return
winding != 0;
61
}
polygon_rotate
void polygon_rotate(point_t *points, uint64_t pointCount, double angle, point_t center)
Rotate a polygon around a center point.
Definition
polygon.c:4
polygon_contains
bool polygon_contains(double px, double py, const point_t *points, uint64_t pointCount)
Check if a point is inside a polygon.
Definition
polygon.c:27
NULL
#define NULL
Pointer error value.
Definition
NULL.h:23
cos
double cos(double x)
Definition
cos.c:6
round
double round(double x)
Definition
round.c:4
sin
double sin(double x)
Definition
sin.c:6
polygon.h
uint64_t
__UINT64_TYPE__ uint64_t
Definition
stdint.h:17
int64_t
__INT64_TYPE__ int64_t
Definition
stdint.h:16
point_t
Definition
point.h:12
point_t::y
int64_t y
Definition
point.h:14
point_t::x
int64_t x
Definition
point.h:13
src
libpatchwork
polygon.c
Generated on Mon Dec 15 2025 21:55:53 for PatchworkOS by
1.9.8