PatchworkOS
Loading...
Searching...
No Matches
polygon.c
Go to the documentation of this file.
2#include <math.h>
3
4void 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
27bool 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}
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
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
#define NULL
Pointer error value.
Definition NULL.h:23
double cos(double x)
Definition cos.c:6
double round(double x)
Definition round.c:4
double sin(double x)
Definition sin.c:6
__UINT64_TYPE__ uint64_t
Definition stdint.h:17
__INT64_TYPE__ int64_t
Definition stdint.h:16
int64_t y
Definition point.h:14
int64_t x
Definition point.h:13