forked from ACM-UCI/DSA_Library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeometry.cpp
270 lines (214 loc) · 5.83 KB
/
geometry.cpp
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
//below controls type of data being stored
#include <cmath>
typedef double TYPE;
struct point{
TYPE x;
TYPE y;
friend bool operator==(const point&a, const point&b);
friend bool operator!=(const point&a, const point&b);
};
bool operator==(const point &a, const point &b){
return a.x == b.x && a.y == b.y;
}
bool operator!=(const point&a, const point&b){
return !(a.x==b.x && a.y==b.y);
}
struct point3 {
TYPE x{},y{},z{};
point3(TYPE x, TYPE y, TYPE z) : x{x}, y{y}, z{z} { }
point3(const point &p) : x{p.x}, y{p.y} { }
};
struct gvector{
point start;
point end;
TYPE x;
TYPE y;
gvector(TYPE x, TYPE y) : x{x}, y{y} { }
gvector(point a, point b){
start = a;
end = b;
x = b.x - a.x;
y = b.y - a.y;
}
TYPE dotProduct(const gvector &other) const {
return x*other.x + y*other.y;
}
void scale(TYPE sc) { x *= sc; y *= sc; }
double magnitude() const { return std::hypot(x, y); }
double angle() const { return std::atan2(y, x); }
/*
* This will only work if TYPE is double
gvector unitVector() const {
double mag = magnitude();
return gvector(x/mag, y/mag);
}
*/
};
struct g3vector {
TYPE x{}, y{}, z{};
g3vector(TYPE x, TYPE y, Type z) : x{x}, y{y}, z{z} { }
g3vector(const point3 &a, const point3 &b) : x{b.x-a.x}, y{b.y-a.y}, z{b.z-a.z} { }
g3vector crossProduct(const g3vector &other) const {
return g3vector(y*other.z - z*other.y, z*other.x - x*other.z, x*other.y - y*other.x);
}
void scale(Type sc) { x *= sc; y *= sc; z *= sc; }
double magnitude() const { return std::sqrt(x*x + y*y + z*z); }
};
struct line{
//ax + by = c
TYPE a;
TYPE b;
TYPE c;
};
double angle(TYPE x1, TYPE y1, TYPE x2, TYPE y2, TYPE x3, TYPE y3){
TYPE vx1 = x1-x2;
TYPE vy1 = y1-y2;
TYPE vx2 = x3-x2;
TYPE vy2 = y3-y2;
TYPE dot = vx1*vx2 + vy1*vy2;
TYPE det = vx1*vy2 - vy1*vx2;
return atan2(det, dot);
}
double angle(point a, point b, point c){
TYPE vx1 = a.x-b.x;
TYPE vy1 = a.y-b.y;
TYPE vx2 = c.x-b.x;
TYPE vy2 = c.y-b.y;
TYPE dot = vx1*vx2 + vy1*vy2;
TYPE det = vx1*vy2 - vy1*vx2;
return atan2(det, dot);
}
double angle(gvector ab, gvector bc){
if(ab.start != bc.start)
return -10000000;
TYPE dot = ab.x*bc.x + ab.y*bc.y;
TYPE det = ab.x*bc.y - ab.y*bc.x;
return atan2(det, dot);
}
void rotateCounterClockwise(double x1, double y1, double x2, double y2, double angle, double &outx, double &outy){
//rotates x1, y1 around x2, y2
//angle in radians
double s = sin(angle);
double c = cos(angle);
x1 -= x2;
y1 -= y2;
double xnew = x1*c-y1*s;
double ynew = x1*s+y1*c;
outx = xnew + x2;
outy = ynew + y2;
}
void rotateCounterClockwise(point a, point b, double angle, point &out){
//rotates x1, y1 around x2, y2
//angle in radians
double s = sin(angle);
double c = cos(angle);
a.x -= b.x;
a.y -= b.y;
double xnew = a.x*c-a.y*s;
double ynew = a.x*s+a.y*c;
out.x = xnew + b.x;
out.y = ynew + b.y;
}
point rotateCounterClockwise(point a, point b, double angle){
//rotates x1, y1 around x2, y2
//angle in radians
double s = sin(angle);
double c = cos(angle);
a.x -= b.x;
a.y -= b.y;
double xnew = a.x*c-a.y*s;
double ynew = a.x*s+a.y*c;
return {xnew + b.x, ynew + b.y};
}
void rotateClockwise(double x1, double y1, double x2, double y2, double angle, double &outx, double &outy){
//rotates x1, y1 around x2, y2
//angle in radians
double s = sin(angle);
double c = cos(angle);
x1 -= x2;
y1 -= y2;
double xnew = x1*c+y1*s;
double ynew = -x1*s+y1*c;
outx = xnew + x2;
outy = ynew + y2;
}
void rotateClockwise(point a, point b, double angle, point &out){
//rotates x1, y1 around x2, y2
//angle in radians
double s = sin(angle);
double c = cos(angle);
a.x -= b.x;
a.y -= b.y;
double xnew = a.x*c+a.y*s;
double ynew = -a.x*s+a.y*c;
out.x = xnew + b.x;
out.y = ynew + b.y;
}
point rotateClockwise(point a, point b, double angle){
//rotates x1, y1 around x2, y2
//angle in radians
double s = sin(angle);
double c = cos(angle);
a.x -= b.x;
a.y -= b.y;
double xnew = a.x*c+a.y*s;
double ynew = -a.x*s+a.y*c;
return {xnew + b.x, ynew + b.y};
}
point intersection(point a, point b, point c, point d){
double a1 = b.y - a.y;
double b1 = a.x - b.x;
double c1 = a1*a.x + b1*a.y;
double a2 = d.y-c.y;
double b2 = c.x - d.x;
double c2 = a2*c.x + b2*c.y;
double determinant = a1*b2-a2*b1;
if(determinant == 0){
return {0,0};
}else{
double x = (b2*c1-b1*c2)/determinant;
double y = (a1*c2-a2*c1)/determinant;
return {x, y};
}
}
point intersection(gvector a, gvector b){
double a1 = a.end.y - a.start.y;
double b1 = a.start.x - a.end.x;
double c1 = a1*a.start.x + b1*a.start.y;
double a2 = b.end.y-b.start.y;
double b2 = b.start.x - b.end.x;
double c2 = a2*b.start.x + b2*b.start.y;
double determinant = a1*b2-a2*b1;
if(determinant == 0){
return {0,0};
}else{
double x = (b2*c1-b1*c2)/determinant;
double y = (a1*c2-a2*c1)/determinant;
return {x, y};
}
}
point intersection(line a, line b){
double determinant = a.a*b.b - b.a*a.b;
if(determinant == 0){
return {0,0};
}else{
double x = (b.b*a.c-a.b*b.c)/determinant;
double y = (a.a*b.c-b.a*a.c)/determinant;
return {x, y};
}
}
bool is_left(TYPE a, TYPE b, TYPE c, TYPE d, TYPE e, TYPE f) {
double vx = c-a;
double vy = d-b;
double _vx = e-a;
double _vy = f-b;
return vx * _vy - _vx * vy > 0;
}
bool is_left(gvector a, point b){
double _vx = b.x-a.start.x;
double _vy = b.y-a.start.y;
return a.x*_vy - _vx*a.y > 0;
}
double dist(point a, point b){
return sqrt((a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y));
}