forked from gorhill/Javascript-Voronoi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rhill-voronoi-demo5.html
254 lines (244 loc) · 8.16 KB
/
rhill-voronoi-demo5.html
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
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Javascript implementation of Steven Fortune's algorithm to compute Voronoi diagrams: Demo 5</title>
<meta name="Keywords" lang="en" content="voronoi, fortune, javascript, raymond hill"/>
<!--[if lte IE 8]><script type="text/javascript" src="excanvas/excanvas.compiled.js"></script><![endif]-->
<script type="text/javascript" src="rhill-voronoi-core.min.js"></script>
<script type="text/javascript" src="QuadTree.js"></script>
<style type="text/css">
body {font-family:tahoma,verdana,arial;font-size:13px;margin:0;padding:0}
body > div {margin-left:4px;margin-right:4px;}
body > div > div {margin:0;border:1px solid #ccc;border-top:0;padding:4px;}
h1 {margin:0 0 0.5em 0;padding: 4px 5em 4px 4px;font:bold large sans-serif;background-color:#c9d7f1;}
h4 {font-size:14px;margin:0.5em 0 0 0;border:0;border-bottom:solid 1px #c9d7f1;padding:2px;background-color:#e5ecf9;}
#canvasParent {margin-top:0;margin-bottom:1em;padding:0;border:0}
#voronoiCode {font:11px monospace;overflow:auto;color:#666;}
#voronoiCode span {color:green;font-weight:bold;}
</style>
<script id="script" type="text/javascript">
var VoronoiDemo = {
voronoi: new Voronoi(),
diagram: null,
margin: 0.1,
canvas: null,
bbox: {xl:0,xr:800,yt:0,yb:600},
sites: [],
timeoutDelay: 100,
init: function() {
this.canvas = document.getElementById('voronoiCanvas');
this.randomSites(100,true);
},
clearSites: function() {
this.compute([]);
},
randomSites: function(n, clear) {
var sites = [];
if (!clear) {
sites = this.sites.slice(0);
}
// create vertices
var xmargin = this.canvas.width*this.margin,
ymargin = this.canvas.height*this.margin,
xo = xmargin,
dx = this.canvas.width-xmargin*2,
yo = ymargin,
dy = this.canvas.height-ymargin*2;
for (var i=0; i<n; i++) {
sites.push({x:self.Math.round((xo+self.Math.random()*dx)*10)/10,y:self.Math.round((yo+self.Math.random()*dy)*10)/10});
}
this.compute(sites);
// relax sites
if (this.timeout) {
clearTimeout(this.timeout)
this.timeout = null;
}
var me = this;
this.timeout = setTimeout(function(){
me.relaxSites();
}, this.timeoutDelay);
},
relaxSites: function() {
if (!this.diagram) {return;}
var cells = this.diagram.cells,
iCell = cells.length,
cell,
site, sites = [],
again = false,
rn, dist;
var p = 1 / iCell * 0.1;
while (iCell--) {
cell = cells[iCell];
rn = Math.random();
// probability of apoptosis
if (rn < p) {
continue;
}
site = this.cellCentroid(cell);
dist = this.distance(site, cell.site);
again = again || dist > 1;
// don't relax too fast
if (dist > 2) {
site.x = (site.x+cell.site.x)/2;
site.y = (site.y+cell.site.y)/2;
}
// probability of mytosis
if (rn > (1-p)) {
dist /= 2;
sites.push({
x: site.x+(site.x-cell.site.x)/dist,
y: site.y+(site.y-cell.site.y)/dist,
});
}
sites.push(site);
}
this.compute(sites);
if (again) {
var me = this;
this.timeout = setTimeout(function(){
me.relaxSites();
}, this.timeoutDelay);
}
},
distance: function(a, b) {
var dx = a.x-b.x,
dy = a.y-b.y;
return Math.sqrt(dx*dx+dy*dy);
},
cellArea: function(cell) {
var area = 0,
halfedges = cell.halfedges,
iHalfedge = halfedges.length,
halfedge,
p1, p2;
while (iHalfedge--) {
halfedge = halfedges[iHalfedge];
p1 = halfedge.getStartpoint();
p2 = halfedge.getEndpoint();
area += p1.x * p2.y;
area -= p1.y * p2.x;
}
area /= 2;
return area;
},
cellCentroid: function(cell) {
var x = 0, y = 0,
halfedges = cell.halfedges,
iHalfedge = halfedges.length,
halfedge,
v, p1, p2;
while (iHalfedge--) {
halfedge = halfedges[iHalfedge];
p1 = halfedge.getStartpoint();
p2 = halfedge.getEndpoint();
v = p1.x*p2.y - p2.x*p1.y;
x += (p1.x+p2.x) * v;
y += (p1.y+p2.y) * v;
}
v = this.cellArea(cell) * 6;
return {x:x/v,y:y/v};
},
compute: function(sites) {
this.sites = sites;
this.voronoi.recycle(this.diagram);
this.diagram = this.voronoi.compute(sites, this.bbox);
this.updateStats();
this.render();
},
updateStats: function() {
if (!this.diagram) {return;}
var e = document.getElementById('voronoiStats');
if (!e) {return;}
e.innerHTML = '('+this.diagram.cells.length+' Voronoi cells computed from '+this.diagram.cells.length+' Voronoi sites in '+this.diagram.execTime+' ms – rendering <i>not</i> included)';
},
render: function() {
var ctx = this.canvas.getContext('2d');
// background
ctx.globalAlpha = 1;
ctx.beginPath();
ctx.rect(0,0,this.canvas.width,this.canvas.height);
ctx.fillStyle = 'white';
ctx.fill();
ctx.strokeStyle = '#888';
ctx.stroke();
// voronoi
if (!this.diagram) {return;}
// edges
ctx.beginPath();
ctx.strokeStyle = '#000';
var edges = this.diagram.edges,
iEdge = edges.length,
edge, v;
while (iEdge--) {
edge = edges[iEdge];
v = edge.va;
ctx.moveTo(v.x,v.y);
v = edge.vb;
ctx.lineTo(v.x,v.y);
}
ctx.stroke();
// sites
ctx.beginPath();
ctx.fillStyle = '#44f';
var sites = this.sites,
iSite = sites.length;
while (iSite--) {
v = sites[iSite];
ctx.rect(v.x-2/3,v.y-2/3,2,2);
}
ctx.fill();
},
};
// -->
</script>
</head>
<body onload="VoronoiDemo.init();">
<a href="https://github.com/gorhill/Javascript-Voronoi"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://s3.amazonaws.com/github/ribbons/forkme_right_red_aa0000.png" alt="Fork me on GitHub"></a>
<h1>Javascript implementation of Steven Fortune's algorithm to compute Voronoi diagrams<br/>Demo 5: Lloyd's relaxation</h1>
<div id="divroot" style="width:800px;">
<p style="margin-top:0;margin-bottom:0"><a href="/voronoi/rhill-voronoi.html">< Back to main page</a><ul style="margin-top:0">
<li><a href="rhill-voronoi-demo1.html">Demo 1: measuring peformance</a>
<li><a href="rhill-voronoi-demo2.html">Demo 2: a bit of interactivity</a>
<li><a href="rhill-voronoi-demo3.php">Demo 3: Fancy tiling</a>
<li><a href="rhill-voronoi-demo4.html">Demo 4: Looking up a Voronoi cell using a quadtree</a>
<li><b>Demo 5: Lloyd's relaxation</b>
<li><a href="http://www.raymondhill.net/blog/?p=458#comments">Comments</a>
</ul></p>
<h4 class="divhdr">Sites generator</h4>
<div class="divinfo" id="voronoiGenerator">
<input type="button" value="Generate" onclick="VoronoiDemo.randomSites(parseInt(document.getElementById('voronoiNumberSites').value,10),true);VoronoiDemo.render();"/> or <input type="button" value="Add" onclick="VoronoiDemo.randomSites(parseInt(document.getElementById('voronoiNumberSites').value,10),false);VoronoiDemo.render();"/><input id="voronoiNumberSites" type="text" value="100" size="5" maxlength="5"/> sites randomly (Warning: performance might suffer the more sites you add.)
<br/><input id="voronoiClearSites" type="button" value="Clear all sites" onclick="VoronoiDemo.clearSites();VoronoiDemo.render();"/>
</div>
<h4 class="divhdr">Canvas <span id="voronoiStats" style="font:normal 11px sans"></span> <span style="font:normal 11px sans"></h4>
<div id="canvasParent">
<noscript>You need to enable Javascript in your browser for this page to display properly.</noscript>
<canvas id="voronoiCanvas" width="800" height="600"></canvas>
<div id="voronoiNoCanvasAlert" style="display:none;padding:1em;background-color:#fcc;color:black;">
<p>Your browser doesn't support the HTML5 <canvas> element technology.</p>
<p>See <a target="_blank" href="http://en.wikipedia.org/wiki/Canvas_(HTML_element)">Wikipedia</a> for information on which browsers support the <u>HTML5 <canvas></u> technology.</p>
</div>
</div>
<h4 class="divhdr">Javascript source code for this page</h4>
<div class="divinfo" id="voronoiCode">
<pre>
<span><script type="text/javascript" src="<a href="rhill-voronoi-core.js" target="_blank">rhill-voronoi-core.js</a>"></script></span>
...
<div id="scriptContainer"></div>
...
</pre>
</div>
</div>
<script>
(function(){
var srcElem = document.getElementById("script");
if (srcElem) {
var dstElem = document.getElementById("scriptContainer");
if (dstElem) {
dstElem.innerText = srcElem.innerHTML;
}
}
})();
</script>
</body>
</html>