-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathDelaunayTriangulation_test.cpp
207 lines (202 loc) · 5.68 KB
/
DelaunayTriangulation_test.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
#include <bits/stdc++.h>
using namespace std;
typedef long long type;
typedef pair<type,type> Pt;
typedef pair<Pt,Pt> Line;
typedef pair<Pt,type> Circle;
#define X first
#define Y second
#define O first
#define R second
Pt operator+( const Pt& p1 , const Pt& p2 ){
return { p1.X + p2.X , p1.Y + p2.Y };
}
Pt operator-( const Pt& p1 , const Pt& p2 ){
return { p1.X - p2.X , p1.Y - p2.Y };
}
Pt operator*( const Pt& tp , const type& tk ){
return { tp.X * tk , tp.Y * tk };
}
Pt operator/( const Pt& tp , const type& tk ){
return { tp.X / tk , tp.Y / tk };
}
type operator*( const Pt& p1 , const Pt& p2 ){
return p1.X * p2.X + p1.Y * p2.Y;
}
type operator^( const Pt& p1 , const Pt& p2 ){
return p1.X * p2.Y - p1.Y * p2.X;
}
type norm2( const Pt& tp ){
return tp * tp;
}
double norm( const Pt& tp ){
return sqrt( norm2( tp ) );
}
Pt perp( const Pt& tp ){
return { tp.Y , -tp.X };
}
/*
Delaunay Triangulation:
Given a sets of points on 2D plane, find a triangulation
such that no points will strictly inside circumcircle
of any triangle.
find : return a triangle contain given point
add_point : add a point into triangulation
A Triangle is in triangulation iff. its has_chd is 0.
Region of triangle u: iterate each u.edge[i].tri,
each points are u.p[(i+1)%3], u.p[(i+2)%3]
calculation involves O(|V|^6)
*/
const int N = 100000 + 5;
const type inf = 2e3;
type eps = 1e-6; // 0 when integer
type sqr(type x) { return x*x; }
// return p4 is in circumcircle of tri(p1,p2,p3)
bool in_cc(const Pt& p1, const Pt& p2, const Pt& p3, const Pt& p4){
type u11 = p1.X - p4.X; type u12 = p1.Y - p4.Y;
type u21 = p2.X - p4.X; type u22 = p2.Y - p4.Y;
type u31 = p3.X - p4.X; type u32 = p3.Y - p4.Y;
type u13 = sqr(p1.X)-sqr(p4.X)+sqr(p1.Y)-sqr(p4.Y);
type u23 = sqr(p2.X)-sqr(p4.X)+sqr(p2.Y)-sqr(p4.Y);
type u33 = sqr(p3.X)-sqr(p4.X)+sqr(p3.Y)-sqr(p4.Y);
type det = -u13*u22*u31 + u12*u23*u31 + u13*u21*u32
-u11*u23*u32 - u12*u21*u33 + u11*u22*u33;
return det > eps;
}
type side(const Pt& a, const Pt& b, const Pt& p)
{ return (b - a) ^ (p - a); }
typedef int SdRef;
struct Tri;
typedef Tri* TriRef;
struct Edge {
TriRef tri; SdRef side;
Edge():tri(0), side(0){}
Edge(TriRef _tri, SdRef _side):tri(_tri), side(_side){}
};
struct Tri {
Pt p[3];
Edge edge[3];
TriRef chd[3];
Tri() {}
Tri(const Pt& p0, const Pt& p1, const Pt& p2) {
p[0] = p0; p[1] = p1; p[2] = p2;
chd[0] = chd[1] = chd[2] = 0;
}
bool has_chd() const { return chd[0] != 0; }
int num_chd() const {
return chd[0] == 0 ? 0
: chd[1] == 0 ? 1
: chd[2] == 0 ? 2 : 3;
}
bool contains(Pt const& q) const {
for( int i = 0 ; i < 3 ; i ++ )
if( side(p[i], p[(i + 1) % 3] , q) < -eps )
return false;
return true;
}
} pool[ N * 10 ], *tris;
void edge( Edge a, Edge b ){
if(a.tri) a.tri->edge[a.side] = b;
if(b.tri) b.tri->edge[b.side] = a;
}
struct Trig { // Triangulation
Trig(){
the_root = // Tri should at least contain all points
new(tris++)Tri(Pt(-inf,-inf),Pt(+inf+inf,-inf),Pt(-inf,+inf+inf));
}
TriRef find(Pt p)const{ return find(the_root,p); }
void add_point(const Pt& p){ add_point(find(the_root,p),p); }
TriRef the_root;
static TriRef find(TriRef root, const Pt& p) {
while( true ){
if( !root->has_chd() )
return root;
for( int i = 0; i < 3 && root->chd[i] ; ++i )
if (root->chd[i]->contains(p)) {
root = root->chd[i];
break;
}
}
assert( false ); // "point not found"
}
void add_point(TriRef root, Pt const& p) {
TriRef tab,tbc,tca;
/* split it into three triangles */
tab=new(tris++) Tri(root->p[0],root->p[1],p);
tbc=new(tris++) Tri(root->p[1],root->p[2],p);
tca=new(tris++) Tri(root->p[2],root->p[0],p);
edge(Edge(tab,0), Edge(tbc,1));
edge(Edge(tbc,0), Edge(tca,1));
edge(Edge(tca,0), Edge(tab,1));
edge(Edge(tab,2), root->edge[2]);
edge(Edge(tbc,2), root->edge[0]);
edge(Edge(tca,2), root->edge[1]);
root->chd[0] = tab;
root->chd[1] = tbc;
root->chd[2] = tca;
flip(tab,2);
flip(tbc,2);
flip(tca,2);
}
void flip(TriRef tri, SdRef pi) {
TriRef trj = tri->edge[pi].tri;
int pj = tri->edge[pi].side;
if (!trj) return;
if (!in_cc(tri->p[0],tri->p[1],tri->p[2],trj->p[pj])) return;
/* flip edge between tri,trj */
TriRef trk = new(tris++) Tri(tri->p[(pi+1)%3], trj->p[pj], tri->p[pi]);
TriRef trl = new(tris++) Tri(trj->p[(pj+1)%3], tri->p[pi], trj->p[pj]);
edge(Edge(trk,0), Edge(trl,0));
edge(Edge(trk,1), tri->edge[(pi+2)%3]);
edge(Edge(trk,2), trj->edge[(pj+1)%3]);
edge(Edge(trl,1), trj->edge[(pj+2)%3]);
edge(Edge(trl,2), tri->edge[(pi+1)%3]);
tri->chd[0]=trk; tri->chd[1]=trl; tri->chd[2]=0;
trj->chd[0]=trk; trj->chd[1]=trl; trj->chd[2]=0;
flip(trk,1); flip(trk,2);
flip(trl,1); flip(trl,2);
}
};
vector<TriRef> triang;
set<TriRef> vst;
void go( TriRef now ){
if( vst.find( now ) != vst.end() )
return;
vst.insert( now );
if( !now->has_chd() ){
triang.push_back( now );
return;
}
for( int i = 0 ; i < now->num_chd() ; i ++ )
go( now->chd[ i ] );
}
void build( int n , Pt* ps ){
tris = pool;
random_shuffle(ps, ps + n);
Trig tri;
for(int i = 0; i < n; ++ i)
tri.add_point(ps[i]);
go( tri.the_root );
}
int n;
Pt ps[N];
int rand_int( int lb , int rb ){
return (long long)rand() * rand() % ( rb - lb + 1 ) + lb;
}
int main(){
#ifdef RANDOM
n = 1000;
set< pair<int,int> > S;
while( (int)S.size() < n )
S.insert( { rand_int( 0 , 1e2 ) ,
rand_int( 0 , 1e2 ) } );
n = 0;
for( auto i : S )
ps[ n ++ ] = i;
#else
cin >> n;
for( int i = 0 ; i < n ; i ++ )
cin >> ps[ i ].X >> ps[ i ].Y;
#endif
build( n , ps );
}