-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnon_trivial_taco.cpp
71 lines (66 loc) · 1.98 KB
/
non_trivial_taco.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
typedef pair<PT, double> circle;
bool inCircle (circle c, PT p){
return cmp(dist(c.first, p), c.second) <= 0;
}
PT circumcenter (PT p, PT q, PT r){
PT a = p-r, b = q-r;
PT c = PT(dot(a, p+r)/2, dot(b, q+r)/2);
return PT(cross(c, PT(a.y,b.y)), cross(PT(a.x,b.x), c)) / cross(a, b);
}
circle spanningCircle (vector<PT> &v) {
int n = v.size();
random_shuffle(v.begin(), v.end());
circle C(PT(), -1);
for (int i = 0; i < n; i++) if (!inCircle(C, v[i])) {
C = circle(v[i], 0);
for (int j = 0; j < i; j++) if (!inCircle(C, v[j])) {
C = circle((v[i]+v[j])/2, dist(v[i], v[j])/2);
for(int k = 0; k < j; k++) if (!inCircle(C, v[k])){
PT o = circumcenter(v[i], v[j], v[k]);
C = circle(o,dist(o,v[k]));
}
}
}
return C;
}
vector<PT> convexHull (vector<PT> p) {
int n = p.size(), k = 0;
vector<PT> h(2 * n);
sort(p.begin(), p.end());
for (int i = 0; i < n; i++) {
while (k >= 2 && cmp(cross(h[k - 1] - h[k - 2], p[i] - h[k - 2])) <= 0) k--;
h[k++] = p[i];
}
for (int i = n - 2, t = k + 1; i >= 0; i--) {
while (k >= t && cmp(cross(h[k - 1] - h[k - 2], p[i] - h[k - 2])) <= 0) k--;
h[k++] = p[i];
}
h.resize(k); // n+1 points where the first is equal to the last
return h;
}
vector<PT> graham (vector<PT> v) {
sort(v.begin(), v.end());
sortByAngle(v.begin(), v.end(), v[0]);
vector<PT> u (v.size());
int top = 0;
for (int i = 0; i < v.size(); i++) {
while (top > 1 && cmp(cross(u[top-1] - u[top-2], v[i]-u[top-2])) <= 0) top--;
u[top++] = v[i];
}
u.resize(top);
return u;
}
vector<PT> cutPolygon (vector<PT> Q, PT a, PT b) {
PT vec = normalize(rotateCW90(b-a));
vector<PT> resp;
for (int i = 0; i < Q.size(); i++) {
int j = (i+1)%Q.size();
double n1 = dot(Q[i]-a, vec);
double n2 = dot(Q[j]-a, vec);
if (n1 >= -eps) resp.push_back(Q[i]);
if ((n1 < -eps && n2 > eps) || (n1 > eps && n2 < -eps)) {
resp.push_back(computeLineIntersection(a, b, Q[i], Q[j]));
}
}
return resp;
}