-
Notifications
You must be signed in to change notification settings - Fork 0
/
Geometry-Riaz.cpp
359 lines (314 loc) · 11.1 KB
/
Geometry-Riaz.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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
#define EPS 1e-12
#define NEX(x) ((x+1)%n)
#define PRV(x) ((x-1+n)%n)
#define RAD(x) ((x*pi)/180)
#define DEG(x) ((x*180)/pi)
const double PI=acos(-1.0);
inline int dcmp (double x) { return x < -EPS ? -1 : (x > EPS); }
//inline int dcmp (int x) { return (x>0) - (x<0); }
class PT {
public:
double x, y;
PT() {}
PT(double x, double y) : x(x), y(y) {}
PT(const PT &p) : x(p.x), y(p.y) {}
double Magnitude() {return sqrt(x*x+y*y);}
bool operator == (const PT& u) const { return dcmp(x - u.x) == 0 && dcmp(y - u.y) == 0; }
bool operator != (const PT& u) const { return !(*this == u); }
bool operator < (const PT& u) const { return dcmp(x - u.x) < 0 || (dcmp(x-u.x)==0 && dcmp(y-u.y) < 0); }
bool operator > (const PT& u) const { return u < *this; }
bool operator <= (const PT& u) const { return *this < u || *this == u; }
bool operator >= (const PT& u) const { return *this > u || *this == u; }
PT operator + (const PT& u) const { return PT(x + u.x, y + u.y); }
PT operator - (const PT& u) const { return PT(x - u.x, y - u.y); }
PT operator * (const double u) const { return PT(x * u, y * u); }
PT operator / (const double u) const { return PT(x / u, y / u); }
double operator * (const PT& u) const { return x*u.y - y*u.x; }
};
double dot(PT p, PT q) { return p.x*q.x+p.y*q.y; }
double dist2(PT p, PT q) { return dot(p-q,p-q); }
double dist(PT p, PT q) { return sqrt(dist2(p,q)); }
double cross(PT p, PT q) { return p.x*q.y-p.y*q.x; }
double myAsin(double val) {
if(val>1) return PI*0.5;
if(val<-1) return -PI*0.5;
return asin(val);
}
double myAcos(double val) {
if(val>1) return 0;
if(val<-1) return PI;
return acos(val);
}
ostream &operator<<(ostream &os, const PT &p) {
os << "(" << p.x << "," << p.y << ")";
}
istream &operator>>(istream &is, PT &p) {
is >> p.x >> p.y;
return is;
}
// rotate a point CCW or CW around the origin
PT RotateCCW90(PT p) { return PT(-p.y,p.x); }
PT RotateCW90(PT p) { return PT(p.y,-p.x); }
PT RotateCCW(PT p,double t) {
return PT(p.x*cos(t)-p.y*sin(t),p.x*sin(t)+p.y*cos(t));
}
PT RotateAroundPointCCW(PT p,PT pivot,double t) {
PT trans=p-pivot;
PT ret=RotateCCW(trans,t);
ret=ret+pivot;
return ret;
}
// project point c onto line through a and b
// assuming a != b
PT ProjectPointLine(PT a, PT b, PT c) {
return a + (b-a)*dot(c-a, b-a)/dot(b-a, b-a);
}
double DistancePointLine(PT a,PT b,PT c) {
return dist(c,ProjectPointLine(a,b,c));
}
// project point c onto line segment through a and b
PT ProjectPointSegment(PT a, PT b, PT c) {
double r = dot(b-a,b-a);
if (fabs(r) < EPS) return a;
r = dot(c-a, b-a)/r;
if (r < 0) return a;
if (r > 1) return b;
return a + (b-a)*r;
}
// compute distance from c to segment between a and b
double DistancePointSegment(PT a, PT b, PT c) {
return sqrt(dist2(c, ProjectPointSegment(a, b, c)));
}
// compute distance between point (x,y,z) and plane ax+by+cz=d
double DistancePointPlane(double x, double y, double z,
double a, double b, double c, double d)
{
return fabs(a*x+b*y+c*z-d)/sqrt(a*a+b*b+c*c);
}
// determine if lines from a to b and c to d are parallel or collinear
bool LinesParallel(PT a, PT b, PT c, PT d) {
return dcmp(cross(b-a, c-d)) == 0;
}
bool LinesCollinear(PT a, PT b, PT c, PT d) {
return LinesParallel(a, b, c, d)
&& dcmp(cross(a-b, a-c)) == 0
&& dcmp(cross(c-d, c-a)) == 0;
}
//UNTESTED CODE SEGMENT
// determine if line segment from a to b intersects with
// line segment from c to d
bool SegmentsIntersect(PT a, PT b, PT c, PT d) {
if (LinesCollinear(a, b, c, d)) {
if (dcmp(dist2(a, c)) == 0 || dcmp(dist2(a, d)) == 0 ||
dcmp(dist2(b, c)) == 0 || dcmp(dist2(b, d)) == 0) return true;
if (dcmp(dot(c-a, c-b)) > 0 && dcmp(dot(d-a, d-b)) > 0 && dcmp(dot(c-b, d-b)) > 0)
return false;
return true;
}
if (dcmp(cross(d-a, b-a)) * dcmp(cross(c-a, b-a)) > 0) return false;
if (dcmp(cross(a-c, d-c)) * dcmp(cross(b-c, d-c)) > 0) return false;
return true;
}
// compute intersection of line passing through a and b
// with line passing through c and d, assuming that unique
// intersection exists; for segment intersection, check if
// segments intersect first
PT ComputeLineIntersection(PT a, PT b, PT c, PT d) {
b=b-a; d=c-d; c=c-a;
assert(dot(b, b) > EPS && dot(d, d) > EPS);
return a + b*cross(c, d)/cross(b, d);
}
// compute center of circle given three points
PT ComputeCircleCenter(PT a, PT b, PT c) {
b=(a+b)/2;
c=(a+c)/2;
return ComputeLineIntersection(b, b+RotateCW90(a-b), c, c+RotateCW90(a-c));
}
bool PointOnSegment(PT s, PT e, PT p) {
if(p == s || p == e) return 1;
return dcmp(cross(s-p, s-e)) == 0
&& dcmp(dot(PT(s.x-p.x, s.y-p.y), PT(e.x-p.x, e.y-p.y))) < 0;
}
int PointInPolygon(vector < PT > p, PT q) {
int i, j, cnt = 0;
int n = p.size();
for(i = 0, j = n-1; i < n; j = i++) {
if(PointOnSegment(p[i], p[j], q))
return 1;
if(p[i].y > q.y != p[j].y > q.y &&
q.x < (double)(p[j].x-p[i].x)*(q.y-p[i].y)/(double)(p[j].y-p[i].y) + p[i].x)
cnt++;
}
return cnt&1;
}
// determine if point is on the boundary of a polygon
bool PointOnPolygon(const vector<PT> &p, PT q) {
for (int i = 0; i < p.size(); i++)
if (dist2(ProjectPointSegment(p[i], p[(i+1)%p.size()], q), q) < EPS)
return true;
return false;
}
// compute intersection of line through points a and b with
// circle centered at c with radius r > 0
//THIS DOESN'T WORK FOR a == b
vector<PT> CircleLineIntersection(PT a, PT b, PT c, double r) {
vector<PT> ret;
b = b-a;
a = a-c;
double A = dot(b, b);
double B = dot(a, b);
double C = dot(a, a) - r*r;
double D = B*B - A*C;
if (D < -EPS) return ret;
ret.push_back(c+a+b*(-B+sqrt(D+EPS))/A);
if (D > EPS)
ret.push_back(c+a+b*(-B-sqrt(D))/A);
return ret;
}
// compute intersection of circle centered at a with radius r
// with circle centered at b with radius R
vector<PT> CircleCircleIntersection(PT a, PT b, double r, double R) {
vector<PT> ret;
double d = sqrt(dist2(a, b));
if (d > r+R || d+min(r, R) < max(r, R)) return ret;
double x = (d*d-R*R+r*r)/(2*d);
double y = sqrt(r*r-x*x);
PT v = (b-a)/d;
ret.push_back(a+v*x + RotateCCW90(v)*y);
if (y > 0)
ret.push_back(a+v*x - RotateCCW90(v)*y);
return ret;
}
// This code computes the area or centroid of a (possibly nonconvex)
// polygon, assuming that the coordinates are listed in a clockwise or
// counterclockwise fashion. Note that the centroid is often known as
// the "center of gravity" or "center of mass".
double ComputeSignedArea(const vector<PT> &p) {
double area = 0;
for(int i = 0; i < p.size(); i++) {
int j = (i+1) % p.size();
area += p[i].x*p[j].y - p[j].x*p[i].y;
}
return area / 2.0;
}
double ComputeArea(const vector<PT> &p) {
return fabs(ComputeSignedArea(p));
}
double ShoeLace(vector<PT> &vec) {
int i,n;
double ans=0;
n=vec.size();
for(i=0;i<n;i++) ans+=vec[i].x*vec[NEX(i)].y-vec[i].y*vec[NEX(i)].x;
return fabs(ans)*0.5;
}
PT ComputeCentroid(const vector<PT> &p) {
PT c(0,0);
double scale = 6.0 * ComputeSignedArea(p);
for (int i = 0; i < p.size(); i++){
int j = (i+1) % p.size();
c = c + (p[i]+p[j])*(p[i].x*p[j].y - p[j].x*p[i].y);
}
return c / scale;
}
double PAngle(PT a,PT b,PT c) { //Returns positive angle abc
PT temp1(a.x-b.x,a.y-b.y),temp2(c.x-b.x,c.y-b.y);
double ans=myAsin((temp1.x*temp2.y-temp1.y*temp2.x)/(temp1.Magnitude()*temp2.Magnitude()));
if((ans<0&&(temp1.x*temp2.x+temp1.y*temp2.y)<0)||(ans>=0&&(temp1.x*temp2.x+temp1.y*temp2.y)<0))
ans=PI-ans;
ans=ans<0?2*PI+ans:ans;
return ans;
}
double SignedArea(PT a,PT b,PT c){ //The area is positive if abc is in counter-clockwise direction
PT temp1(b.x-a.x,b.y-a.y),temp2(c.x-a.x,c.y-a.y);
return 0.5*(temp1.x*temp2.y-temp1.y*temp2.x);
}
bool XYasscending(PT a,PT b) {
if(abs(a.x-b.x)<EPS) return a.y<b.y;
return a.x<b.x;
}
//Makes convex hull in counter-clockwise direction without repeating first point
//undefined if all points in given[] are collinear
//to allow 180' angle replace <= with <
void MakeConvexHull(vector<PT>given,vector<PT>&ans){
int i,n=given.size(),j=0,k=0;
vector<PT>U,L;
ans.clear();
sort(given.begin(),given.end(),XYasscending);
for(i=0;i<n;i++){
while(true){
if(j<2) break;
if(SignedArea(L[j-2],L[j-1],given[i])<=EPS) j--;
else break;
}
if(j==L.size()){
L.push_back(given[i]);
j++;
}
else{
L[j]=given[i];
j++;
}
}
for(i=n-1;i>=0;i--){
while(1){
if(k<2) break;
if(SignedArea(U[k-2],U[k-1],given[i])<=EPS) k--;
else break;
}
if(k==U.size()){
U.push_back(given[i]);
k++;
}
else{
U[k]=given[i];
k++;
}
}
for(i=0;i<j-1;i++) ans.push_back(L[i]);
for(i=0;i<k-1;i++) ans.push_back(U[i]);
}
typedef PT Vector;
typedef vector<PT> Polygon;
struct DirLine {
PT p;
Vector v;
double ang;
DirLine () {}
// DirLine (PT p, Vector v): p(p), v(v) { ang = atan2(v.y, v.x); }
//adds the left of line p-q
DirLine (PT p, PT q) { this->p = p; this->v.x = q.x-p.x; this->v.y = q.y-p.y; ang = atan2(v.y, v.x); }
bool operator < (const DirLine& u) const { return ang < u.ang; }
};
bool getIntersection (PT p, Vector v, PT q, Vector w, PT& o) {
if (dcmp(cross(v, w)) == 0) return false;
Vector u = p - q;
double k = cross(w, u) / cross(v, w);
o = p + v * k;
return true;
}
bool onLeft(DirLine l, PT p) { return dcmp(l.v * (p-l.p)) >= 0; }
void halfPlaneIntersection(vector<DirLine> &li,vector<PT> &poly) {
int n = li.size();
sort(li.begin(),li.end());
int first, last;
PT* p = new PT[n];
DirLine* q = new DirLine[n];
q[first=last=0] = li[0];
for (int i = 1; i < n; i++) {
while (first < last && !onLeft(li[i], p[last-1])) last--;
while (first < last && !onLeft(li[i], p[first])) first++;
q[++last] = li[i];
if (dcmp(q[last].v * q[last-1].v) == 0) {
last--;
if (onLeft(q[last], li[i].p)) q[last] = li[i];
}
if (first < last)
getIntersection(q[last-1].p, q[last-1].v, q[last].p, q[last].v, p[last-1]);
}
while (first < last && !onLeft(q[first], p[last-1])) last--;
if (last - first <= 1) { delete [] p; delete [] q; return; }
getIntersection(q[last].p, q[last].v, q[first].p, q[first].v, p[last]);
int m = 0;
for (int i = first; i <= last; i++) poly.pb(p[i]);
delete [] p; delete [] q;
}