Blog of RuSun

\begin {array}{c} \mathfrak {One Problem Is Difficult} \\\\ \mathfrak {Because You Don't Know} \\\\ \mathfrak {Why It Is Diffucult} \end {array}

计算几何全家桶

计算几何模板。

  • Pick 定理:给定顶点均为整点的简单多边形,其面积 $S$ 和内部格点数目 $a$ 、边上格点数目 $b$ 的关系:$S = a + \frac b 2 - 1$ 。
  • 欧拉公式:多面体的点数 $V$ 与边数 $E$ 与面数 $F$ 的关系:$V - E + F = 2$ 。
查看代码
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
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#define pb push_back
using namespace std;
const int N = 1e5 + 10;
const double pi = acos(-1), eps = 1e-12, inf = 1e20;
int cmp (double a, double b)
{
if (a - b > eps) return 1;
if (b - a > eps) return -1;
return 0;
}
int sign (double a) { return cmp(a, 0); }
struct Point
{
double x, y;
Point () { }
Point (double a, double b)
{ x = a, y = b; }
void input () { scanf("%lf%lf", &x, &y); }
void output () { printf("%.10lf %.10lf\n", x + eps, y + eps); }
bool operator < (const Point &_) const
{
int t = cmp(x, _.x);
if (t == 1) return false;
if (t == -1) return true;
return y < _.y;
}
};
Point mid (Point a, Point b)
{ return Point((a.x + b.x) / 2, (a.y + b.y) / 2); }
struct Vector
{
double x, y;
Vector () { }
Vector (double a, double b)
{ x = a, y = b; }
Vector (Point a, Point b)
{ x = b.x - a.x, y = b.y - a.y; }
void input () { scanf("%lf%lf", &x, &y); }
void output () { printf("%.10lf %.10lf\n", x + eps, y + eps); }
Vector operator + (const Vector &_)
{ return Vector(x + _.x, y + _.y); }
Vector operator - (const Vector &_)
{ return Vector(x - _.x, y - _.y); }
Vector operator * (const double &_)
{ return Vector(x * _, y * _); }
Vector operator / (const double &_)
{ return Vector(x / _, y / _); }
double operator ^ (const Vector &_) const
{ return x * _.x + y * _.y; }
double operator & (const Vector &_) const
{ return x * _.y - y * _.x; }
double len () { return sqrt(*this ^ *this); }
Vector uint () { return *this / len(); }
Vector rotate (double th)
{ return Vector(x * cos(th) - y * sin(th), x * sin(th) + y * cos(th)); }
};
Point move (Point p, Vector t)
{ return Point(p.x + t.x, p.y + t.y); }
double area (Point a, Point b, Point c)
{ return abs(Vector(a, b) & Vector(a, c)); }
double angle (Vector a, Vector b) { return acos((a ^ b) / a.len() / b.len()); }
double dis (Point a, Point b)
{
double dx = a.x - b.x, dy = a.y - b.y;
return sqrt(dx * dx + dy * dy);
}
struct Segment
{
Point p; Vector t;
Segment () { }
Segment (Point a, Point b)
{ p = a, t = Vector(a, b); }
Segment (Point _p, Vector _t)
{ p = _p, t = _t; }
double angle () { return atan2(t.y, t.x); }
int on (Point _)
{ return sign(t & Vector(p, _)); }
bool operator < (Segment &_)
{
double a = angle(), b = _.angle();
if (!cmp(a, b)) return on(_.p) < 0;
return a < b;
}
Point proj (Point _)
{ return move(p, t * (t ^ Vector(p, _)) / (t ^ t)); }
double dis (Point _)
{ return abs(t & Vector(p, _)) / t.len(); }
};
Segment vertical (Point a, Point b)
{ return Segment(mid(a, b), Vector(a, b).rotate(pi / 2)); }
Point intersection (Segment a, Segment b)
{ return move(a.p, a.t * (b.t & Vector(b.p, a.p)) / (a.t & b.t)); }
struct Circle
{
Point p; double r;
Circle () { }
Circle (Point _p, double _r)
{ p = _p, r = _r; }
Circle (Point a, Point b, Point c)
{
p = intersection(vertical(a, b), vertical(a, c));
r = dis(p, a);
}
};
struct Polygon
{
vector <Point> p;
Polygon () { }
void input ()
{
int n; scanf("%d", &n);
p.resize(n);
for (int i = 0; i < n; ++i)
p[i].input();
}
double PolygonArea ()
{
double res = 0;
for (int i = 1; i + 1 < p.size(); ++i)
res += area(p[0], p[i], p[i + 1]);
return res / 2;
}
double PolygonCircumference ()
{
double res = dis(p.front(), p.back());
for (int i = 0; i + 1 < p.size(); ++i)
res += dis(p[i], p[i + 1]);
return res;
}
Polygon ConvexHull ()
{
if (p.size() < 3) return *this;
static bool vis[N];
sort(p.begin(), p.end());
static int top = 0, stk[N];
for (int i = 0; i < p.size(); ++i)
{
while (top >= 2 && Segment(p[stk[top - 1]], p[stk[top]]).on(p[i]) >= 0)
vis[stk[top--]] = false;
vis[stk[++top] = i] = true;
}
vis[0] = false;
for (int i = p.size() - 1; ~i; --i)
{
if (vis[i]) continue;
while (top >= 2 && Segment(p[stk[top - 1]], p[stk[top]]).on(p[i]) >= 0)
vis[stk[top--]] = false;
vis[stk[++top] = i] = true;
}
Polygon res;
for (int i = 1; i < top; ++i)
res.p.pb(p[stk[i]]);
return res;
}
double diameter ()
{
if (p.size() <= 2) return dis(p.front(), p.back());
double res = 0;
for (int i = 0, j = 1; i < p.size(); ++i)
{
Point d = p[i], e = p[(i + 1) % p.size()];
while (cmp(area(d, e, p[j]), area(d, e, p[(j + 1) % p.size()])) == -1)
j = (j + 1) % p.size();
res = max(res, max(dis(d, p[j]), dis(e, p[j])));
}
return res;
}
Circle CircleCover ()
{
random_shuffle(p.begin(), p.end());
Circle c;
c.p = p[0], c.r = 0;
for (int i = 1; i < p.size(); ++i)
{
if (cmp(c.r, dis(c.p, p[i])) >= 0) continue;
c.p = p[i], c.r = 0;
for (int j = 0; j < i; ++j)
{
if (cmp(c.r, dis(c.p, p[j])) >= 0) continue;
c.p = mid(p[i], p[j]), c.r = dis(p[i], p[j]) / 2;
for (int k = 0; k < j; ++k)
if (cmp(c.r, dis(c.p, p[k])) < 0)
c = Circle(p[i], p[j], p[k]);
}
}
return c;
}
Polygon RectangleCover ()
{
double mn = inf;
Polygon res; res.p.resize(4);
for (int i = 0, a = 2, b = 1, c = 2; i < p.size(); ++i)
{
Segment l(p[i], p[(i + 1) % p.size()]);
while (cmp(l.dis(p[a]), l.dis(p[(a + 1) % p.size()])) < 0)
a = (a + 1) % p.size();
while (cmp(l.t ^ Vector(l.p, p[b]), l.t ^ Vector(l.p, p[(b + 1) % p.size()])) < 0)
b = (b + 1) % p.size();
if (!i) c = a;
while (cmp(l.t ^ Vector(l.p, p[c]), l.t ^ Vector(l.p, p[(c + 1) % p.size()])) > 0)
c = (c + 1) % p.size();
double h = l.dis(p[a]), w = dis(l.proj(p[b]), l.proj(p[c]));
if (h * w < mn)
{
mn = h * w;
res.p[0] = l.proj(p[b]), res.p[1] = l.proj(p[c]);
Vector u = l.t.uint().rotate(-pi / 2) * h;
res.p[2] = move(res.p[1], u), res.p[3] = move(res.p[0], u);
}
}
return res;
}
};
Polygon HalfPlaneIntersection (vector <Segment> &l) //取直线左侧
{
sort(l.begin(), l.end());
static int hd, tl, q[N];
hd = 0, tl = -1;
for (int i = 0; i < l.size(); ++i)
{
if (i && !cmp(l[i].angle(), l[i - 1].angle())) continue;
while (hd < tl && l[i].on(intersection(l[q[tl - 1]], l[q[tl]])) <= 0) --tl;
while (hd < tl && l[i].on(intersection(l[q[hd + 1]], l[q[hd]])) <= 0) ++hd;
q[++tl] = i;
}
while (hd + 1 < tl && l[q[hd]].on(intersection(l[q[tl - 1]], l[q[tl]])) <= 0) --tl;
while (hd + 1 < tl && l[q[tl]].on(intersection(l[q[hd + 1]], l[q[hd]])) <= 0) ++hd;
q[++tl] = q[hd];
Polygon res;
for (int i = hd; i < tl; ++i)
res.p.pb(intersection(l[q[i]], l[q[i + 1]]));
return res;
}
int main ()
{

return 0;
}