-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathvoronoi.cpp
165 lines (146 loc) · 5.62 KB
/
voronoi.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
//
// ボロノイ図 (O(N^3))
//
// verified:
// AOJ 3049 A Polygon And Circles
// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=3049
//
//------------------------------//
// 基本要素 (点, 線分, 円)
//------------------------------//
#include <iostream>
#include <vector>
#include <cmath>
#include <iomanip>
#include <algorithm>
using namespace std;
using DD = double;
const DD INF = 1LL<<60; // to be set appropriately
const DD EPS = 1e-10; // to be set appropriately
const DD PI = acosl(-1.0);
DD torad(int deg) {return (DD)(deg) * PI / 180;}
DD todeg(DD ang) {return ang * 180 / PI;}
/* Point */
struct Point {
DD x, y;
Point(DD x = 0.0, DD y = 0.0) : x(x), y(y) {}
friend ostream& operator << (ostream &s, const Point &p) {return s << '(' << p.x << ", " << p.y << ')';}
};
inline Point operator + (const Point &p, const Point &q) {return Point(p.x + q.x, p.y + q.y);}
inline Point operator - (const Point &p, const Point &q) {return Point(p.x - q.x, p.y - q.y);}
inline Point operator * (const Point &p, DD a) {return Point(p.x * a, p.y * a);}
inline Point operator * (DD a, const Point &p) {return Point(a * p.x, a * p.y);}
inline Point operator * (const Point &p, const Point &q) {return Point(p.x * q.x - p.y * q.y, p.x * q.y + p.y * q.x);}
inline Point operator / (const Point &p, DD a) {return Point(p.x / a, p.y / a);}
inline Point conj(const Point &p) {return Point(p.x, -p.y);}
inline Point rot(const Point &p, DD ang) {return Point(cos(ang) * p.x - sin(ang) * p.y, sin(ang) * p.x + cos(ang) * p.y);}
inline Point rot90(const Point &p) {return Point(-p.y, p.x);}
inline DD cross(const Point &p, const Point &q) {return p.x * q.y - p.y * q.x;}
inline DD dot(const Point &p, const Point &q) {return p.x * q.x + p.y * q.y;}
inline DD norm(const Point &p) {return dot(p, p);}
inline DD abs(const Point &p) {return sqrt(dot(p, p));}
inline DD amp(const Point &p) {DD res = atan2(p.y, p.x); if (res < 0) res += PI*2; return res;}
inline bool eq(const Point &p, const Point &q) {return abs(p - q) < EPS;}
inline bool operator < (const Point &p, const Point &q) {return (abs(p.x - q.x) > EPS ? p.x < q.x : p.y < q.y);}
inline bool operator > (const Point &p, const Point &q) {return (abs(p.x - q.x) > EPS ? p.x > q.x : p.y > q.y);}
inline Point operator / (const Point &p, const Point &q) {return p * conj(q) / norm(q);}
/* Line */
struct Line : vector<Point> {
Line(Point a = Point(0.0, 0.0), Point b = Point(0.0, 0.0)) {
this->push_back(a);
this->push_back(b);
}
friend ostream& operator << (ostream &s, const Line &l) {return s << '{' << l[0] << ", " << l[1] << '}';}
};
/* Circle */
struct Circle : Point {
DD r;
Circle(Point p = Point(0.0, 0.0), DD r = 0.0) : Point(p), r(r) {}
friend ostream& operator << (ostream &s, const Circle &c) {return s << '(' << c.x << ", " << c.y << ", " << c.r << ')';}
};
//------------------------------//
// 多角形
//------------------------------//
// 多角形の面積
DD calc_area(const vector<Point> &pol) {
DD res = 0.0;
for (int i = 0; i < pol.size(); ++i) {
res += cross(pol[i], pol[(i+1)%pol.size()]);
}
return res/2.0L;
}
// convex cut
int ccw_for_convexcut(const Point &a, const Point &b, const Point &c) {
if (cross(b-a, c-a) > EPS) return 1;
if (cross(b-a, c-a) < -EPS) return -1;
if (dot(b-a, c-a) < -EPS) return 2;
if (norm(b-a) < norm(c-a) - EPS) return -2;
return 0;
}
vector<Point> crosspoint_for_convexcut(const Line &l, const Line &m) {
vector<Point> res;
DD d = cross(m[1] - m[0], l[1] - l[0]);
if (abs(d) < EPS) return vector<Point>();
res.push_back(l[0] + (l[1] - l[0]) * cross(m[1] - m[0], m[1] - l[0]) / d);
return res;
}
vector<Point> convex_cut(const vector<Point> &pol, const Line &l) {
vector<Point> res;
for (int i = 0; i < pol.size(); ++i) {
Point p = pol[i], q = pol[(i+1)%pol.size()];
if (ccw_for_convexcut(l[0], l[1], p) != -1) {
if (res.size() == 0) res.push_back(p);
else if (!eq(p, res[res.size()-1])) res.push_back(p);
}
if (ccw_for_convexcut(l[0], l[1], p) * ccw_for_convexcut(l[0], l[1], q) < 0) {
vector<Point> temp = crosspoint_for_convexcut(Line(p, q), l);
if (temp.size() == 0) continue;
else if (res.size() == 0) res.push_back(temp[0]);
else if (!eq(temp[0], res[res.size()-1])) res.push_back(temp[0]);
}
}
return res;
}
// Voronoi-diagram
// pol: outer polygon, ps: points
// find the polygon nearest to ps[ind]
Line bisector(const Point &p, const Point &q) {
Point c = (p + q) / 2.0L;
Point v = (q - p) * Point(0.0L, 1.0L);
v = v / abs(v);
return Line(c - v, c + v);
}
vector<Point> voronoi(const vector<Point> &pol, const vector<Point> &ps, int ind) {
vector<Point> res = pol;
for (int i = 0; i < ps.size(); ++i) {
if (i == ind) continue;
Line l = bisector(ps[ind], ps[i]);
res = convex_cut(res, l);
}
return res;
}
//------------------------------//
// Examples
//------------------------------//
void AOJ3049() {
int N, M;
cin >> N;
vector<Point> pol(N);
for (int i = 0; i < N; ++i) cin >> pol[i].x >> pol[i].y;
cin >> M;
vector<Point> ps(M);
for (int i = 0; i < M; ++i) cin >> ps[i].x >> ps[i].y;
double res = 0.0;
for (int i = 0; i < M; ++i) {
vector<Point> npol = voronoi(pol, ps, i);
for (auto p : npol) {
double tmp = 1LL<<60;
for (int j = 0; j < M; ++j) tmp = min(tmp, abs(ps[j] - p));
res = max(res, tmp);
}
}
cout << fixed << setprecision(10) << res << endl;
}
int main() {
AOJ3049();
}