-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathgeometry.h
158 lines (128 loc) · 3.92 KB
/
geometry.h
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#ifndef _GEOMETRY_H
#define _GEOMETRY_H
#include "pygame.h"
#include <float.h>
#include <stddef.h>
#include <math.h>
typedef struct {
double x, y, r;
} pgCircleBase;
typedef struct {
PyObject_HEAD pgCircleBase circle;
PyObject *weakreflist;
} pgCircleObject;
#define pgCircle_CAST(o) ((pgCircleObject *)(o))
#define pgCircle_AsCircle(o) (pgCircle_CAST(o)->circle)
#define pgCircle_GETX(self) (pgCircle_CAST(self)->circle.x)
#define pgCircle_GETY(self) (pgCircle_CAST(self)->circle.y)
#define pgCircle_GETR(self) (pgCircle_CAST(self)->circle.r)
typedef struct {
double xa, ya;
double xb, yb;
} pgLineBase;
typedef struct {
PyObject_HEAD pgLineBase line;
PyObject *weakreflist;
} pgLineObject;
#define pgLine_CAST(o) ((pgLineObject *)(o))
#define pgLine_GETLINE(o) (pgLine_CAST(o)->line)
#define pgLine_AsLine(o) (pgLine_CAST(o)->line)
#define pgLine_GETX1(self) (pgLine_CAST(self)->line.xa)
#define pgLine_GETY1(self) (pgLine_CAST(self)->line.ya)
#define pgLine_GETX2(self) (pgLine_CAST(self)->line.xb)
#define pgLine_GETY2(self) (pgLine_CAST(self)->line.yb)
// return 1 if success and 0 if failure
static int
pgLine_FromObject(PyObject *obj, pgLineBase *out);
// return 1 if success and 0 if failure
static int
pgLine_FromObjectFastcall(PyObject *const *args, Py_ssize_t nargs,
pgLineBase *out);
// return 1 if success and 0 if failure
static int
pgCircle_FromObject(PyObject *obj, pgCircleBase *out);
// return 1 if success and 0 if failure
static int
pgCircle_FromObjectFastcall(PyObject *const *args, Py_ssize_t nargs,
pgCircleBase *out);
static PyTypeObject pgCircle_Type;
static PyTypeObject pgLine_Type;
static PyTypeObject pgPolygon_Type;
typedef struct {
Py_ssize_t verts_num;
double *vertices;
double centerx, centery;
} pgPolygonBase;
typedef struct {
PyObject_HEAD pgPolygonBase polygon;
PyObject *weakreflist;
} pgPolygonObject;
#define pgPolygon_CAST(o) ((pgPolygonObject *)(o))
#define pgPolygon_AsPolygon(o) (pgPolygon_CAST(o)->polygon)
#define pgPolygon_GETVERTICES(o) (pgPolygon_AsPolygon(o).vertices)
#define pgPolygon_GETVERTSNUM(o) (pgPolygon_AsPolygon(o).verts_num)
#define pgPolygon_GETCENTER_X(o) (pgPolygon_AsPolygon(o).centerx)
#define pgPolygon_GETCENTER_Y(o) (pgPolygon_AsPolygon(o).centery)
// return 1 if success and 0 if failure
static int
pgPolygon_FromObject(PyObject *obj, pgPolygonBase *out, int *was_sequence);
static int
pgPolygon_FromObjectFastcall(PyObject *const *args, Py_ssize_t nargs,
pgPolygonBase *out, int *was_sequence);
#define pgCircle_Check(o) ((o)->ob_type == &pgCircle_Type)
#define pgLine_Check(o) ((o)->ob_type == &pgLine_Type)
#define pgPolygon_Check(o) ((o)->ob_type == &pgPolygon_Type)
/* Constants */
/* PI */
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
/* 2PI */
#ifndef M_TWOPI
#define M_TWOPI 6.28318530717958647692
#endif
/* PI/2 */
#ifndef M_PI_QUO_2
#define M_PI_QUO_2 1.57079632679489661923
#endif
/* PI/4 */
#ifndef M_PI_QUO_4
#define M_PI_QUO_4 0.78539816339744830962
#endif
/* PI/180 */
#ifndef M_PI_QUO_180
#define M_PI_QUO_180 0.01745329251994329577
#endif
/* 180/PI */
#ifndef M_180_QUO_PI
#define M_180_QUO_PI 57.29577951308232087680
#endif
/* Functions */
/* Converts degrees to radians */
static PG_FORCE_INLINE double
DEG_TO_RAD(double deg)
{
return deg * M_PI_QUO_180;
}
/* Converts radians to degrees */
static PG_FORCE_INLINE double
RAD_TO_DEG(double rad)
{
return rad * M_180_QUO_PI;
}
/* Frees the polygon's vertices if they were allocated from a sequence. */
static PG_FORCE_INLINE void
PG_FREEPOLY_COND(pgPolygonBase *poly, int was_sequence)
{
if (was_sequence) {
PyMem_Free(poly->vertices);
}
}
static int
double_compare(double a, double b)
{
/* Uses both a fixed epsilon and an adaptive epsilon */
const double e = 1e-6;
return fabs(a - b) < e || fabs(a - b) <= e * MAX(fabs(a), fabs(b));
}
#endif /* ~_GEOMETRY_H */