/
Voronoi.cs
258 lines (219 loc) · 5.61 KB
/
Voronoi.cs
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
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public struct LineSegment{
public Vector2 right;
public Vector2 left;
}
// Parabola represents half the distance between (each point of the) sweepline and a point
public class Parabola{
public Vector2 point;
public Parabola previous = null;
public Parabola next = null;
public Parabola(Vector2 p) {point = p;}
public Parabola(Parabola para) {
point = para.point;
previous = para.previous;
next = para.next;
}
}
// Circle Event
public class Event{
public Vector2 point; // Where the event will meet the sweepline
public Vector2 breakPoint; // Where parabolas meet
public Parabola parabola; // The parabola who hold the event
public Event(Vector2 p, Vector2 bp, Parabola para){
point = p ;
breakPoint = bp;
parabola = para;
}
}
public class Voronoi{
private List<Vector2> m_sites;
private List<LineSegment> m_edges;
private List<Event> m_events;
// First parabola of the beachline
private Parabola m_root;
// We assume that we are in a rectangle and the sweepline goes from left to right
Rect m_area;
float m_sweepLineX;
public Voronoi(Rect area, List<Vector2> sites)
{
m_area = area;
m_sites = sites;
Sorting.QuickSort(m_sites, 0, m_sites.Count - 1);
m_sweepLineX = m_area.left;
m_edges = new List<LineSegment>();
}
/*
* Diagram
* We are using Fortune's algorithm
* Returns the list of segments forming the voronoi diagram
*/
public List<LineSegment> Diagram()
{
while (m_sites.Count != 0)
{
// Process circle events prior to sites event
if (m_events.Count != 0 && m_events[0].point.x <= m_sites[0].x)
ProcessEvent();
else
ProcessSite();
}
return m_edges;
}
private void ProcessEvent()
{
Event e = m_events[0];
m_sweepLineX = e.point.x;
m_events.RemoveAt(0);
CreateSegment(e.parabola, e.breakPoint);
RemoveParabola(e.parabola);
}
private void ProcessSite()
{
Vector2 site = m_sites[0];
m_sweepLineX = site.x;
m_sites.RemoveAt(0);
AddParabola(site);
}
private void CreateSegment(Parabola p, Vector2 circleCenter)
{
}
private void RemoveParabola(Parabola para)
{
Parabola p = para.previous;
Parabola n = para.next;
n.previous = p;
p.next = n;
para.previous = null;
para.next = null;
}
private void AddParabola(Vector2 point)
{
if (m_root != null)
{
m_root = new Parabola(point);
return;
}
Parabola para = new Parabola(point);
Parabola i;
// Look for intersections with the beach line
for (i = m_root ; i != null; i = i.next)
{
Vector2 intersection = ParabolaIntersection(i,para, m_sweepLineX);
if (IsValid(intersection))
{
// Duplicate i and insert new parabola between i and i'
para.next = new Parabola(i);
para.next.previous = para;
i.next = para;
para.previous = i;
return;
}
}
// If no intersection in our area, find where to insert it
for (i = m_root ; i != null ; i = i.next)
{
if (para.point.y < i.point.y)
{
para.next = i;
i = para;
para.next.previous = para;
return;
}
}
// Add parabola to the end : find last parabola
for (i = m_root ; i.next != null ; i = i.next);
i.next = para;
para.previous = i;
}
/*
* ParabolaIntersection
* Assuming the sweepline is vertical and the parabolas are on the left of it.
* Returns the intersection between p1 and p2.
*/
private Vector2 ParabolaIntersection( Parabola p1, Parabola p2, float slx /* SweepLineX */)
{
Vector2 result = new Vector2(-1.0f,-1.0f);
// Parabola equation for parabola 1:
// X = ((Y1² - Y²) + X1² - slx²) / 2( X1 - slx)
float x1 = p1.point.x;
float x2 = p2.point.x;
float y1 = p1.point.y;
float y2 = p2.point.y;
Vector2 p = p1.point;
// specific and zero-divider cases
if (x1 == x2)
{
result.y = (y1 + y2) / 2.0f;
}
else if (x1 == slx)
{
result.y = y1;
p = p2.point;
}
else if (x2 == slx)
{
result.y = y2;
}
else
{
// solve 2nd degree equation
float z1 = 2*(x1 - slx);
float z2 = 2*(x2 - slx);
float a = 1/z1 - 1/z2;
float b = 2*(x2/z2 - x1/z1);
float c = (x1*x1 + y1*y1 - slx*slx)/z1 - (x2*x2 + y2*y2 - slx*slx)/z2;
result.y = (-b - Mathf.Sqrt(b*b - 4*a*c))/(2*a);
// 2nd solution
//result.y = (-b + Mathf.Sqrt(b*b - 4*a*c))/(2*a);
}
result.x = ((p.y*p.y - result.y*result.y) + p.x*p.x - slx*slx) / (2*(p.x - slx));
return result;
}
private void CheckCircleEvent(Parabola p)
{
if ( p.previous == null || p.next == null)
return;
// center corresponds to the new breakpoint
Vector2 cc = CircleCenter(p.previous.point, p.point, p.next.point);
if (IsValid(cc))
{
// point event represents when the sweepline meets the event.
Vector2 point = new Vector2(cc.x + Distance(p.point, cc), cc.y);
// Check if the point is located before the sweepline
if (point.x < m_sweepLineX)
return;
m_events.Add(new Event(point,cc,p));
}
}
private float Distance(Vector2 a, Vector2 b)
{
return Mathf.Sqrt((a.x - b.x)*(a.x - b.x) + (a.y - b.y)*(a.y - b.y));
}
private Vector2 CircleCenter(Vector2 a, Vector2 b, Vector2 c)
{
Vector2 result = new Vector2(-1.0f,-1.0f);
float Xba = b.x - a.x;
float Xca = c.x - a.x;
float Yba = b.y - a.y;
float Yca = c.y - a.y;
// check division by zero
float d = 2*(Yba*Xca - Yca*Xba);
if ( d == 0 )
return result;
float Yca2 = c.y*c.y - a.y*a.y;
float Xca2 = c.x*c.x - a.x*a.x;
float Yba2 = b.y*b.y - a.y*a.y;
float Xba2 = b.x*b.x - a.x*a.x;
float n = Xba*(Yca2+Xca2) - Xca*(Yba2+Xba2);
result.y = n/d;
result.x = (Yba2 + 2*result.y*Yba + Xba2) / (2*Xba);
return result;
}
private bool IsValid(Vector2 p)
{
return m_area.Contains(p);
}
}