-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathvortest3.html
247 lines (201 loc) · 5.79 KB
/
vortest3.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Demo</title>
<script type="text/javascript" src="d3-master/d3.js"></script>
<script type="text/javascript" src="ConvexHull.js"></script>
<script src="jquery-1.11.0.min.js"></script>
</head>
<body>
<style>
body{ background-color:#EEF; } .name{ cursor: pointer; cursor:
hand; } div.first{ width:650px;
}
#chart {
width:750px;
height:750px;
background:white;
}
path {
stroke: #000;
stroke-widthL .5px;
}
</style>
<div>
<h1> VORONOI TESTING </h1>
<p id="chart"></p>
<script>
function printFacets(facets){
for (var i = 0; i < facets.length; i++){
console.log("Face: " + i);
for (var j = 0; j < 3; j++){
console.log("\t" + j + ": " + facets[i].verts[j].x + " : "
+ facets[i].verts[j].y + " : " + facets[i].verts[j].z + " : ");
}
}
}
function getRandomArbitary (min, max) {
return Math.random() * (max - min) + min;
}
// IN: sites and weights
// OUT: sites with Z coordinate based on X,Y,and W
function applyDeltaPi(S, W){
var result = [];
for (var i in S){
var x = S[i][0], y = S[i][1], w = W[i];
result[i] = [x,y, (x*x) + (y*y) - w];
}
return result;
}
// IN: HEdge edge
function getFacesOfDestVertex(edge) {
var faces = [];
var previous = edge;
var first = edge.dest;
var site = first;
var neighbours = [];
do {
previous = previous.twin.prev;
// add neighbour to the neighbourlist
var siteOrigin = previous.orig;
if (!siteOrigin.isDummy) {
neighbours.push(siteOrigin);
}
var iFace = previous.iFace;
if (iFace.isVisibleFromBelow()) {
faces.push(iFace);
}
} while (previous !== edge);
site.neighbours = neighbours; // TODO
return faces;
}
// IN: Omega = convex bounding polygon
// IN: S = unique set of sites
// IN: W = set of weights for sites
// OUT: Set of lines making up the voronoi power diagram
function computePowerDiagram(S, W, boundingPolygon){
var sStar = applyDeltaPi(S, W);
ConvexHull.init(boundingPolygon, sStar);
var facets = ConvexHull.compute(sStar);
alert("done computing CH!");
var vertexCount = ConvexHull.points.length;
var verticesVisited = [];
var facetCount = facets.length;
for (var i = 0; i < facetCount; i++) {
var facet = facets[i];
if (facet.isVisibleFromBelow()) {
for (var e = 0; e < 3; e++) {
// got through the edges and start to build the polygon by
// going through the double connected edge list
var edge = facet.edges[e];
var destVertex = edge.dest;
var site = destVertex;
if (!verticesVisited[destVertex.index]) {
verticesVisited[destVertex.index] = true;
if (site.isDummy) { // Check if this is one of the
// sites making the bounding polygon
continue;
}
// faces around the vertices which correspond to the
// polygon corner points
var faces = getFacesOfDestVertex(edge);
// var poly = new PolygonSimple(); // TODO replace
var protopoly = [];
// with D3 or some other polygon
var lastX = NaN;
var lastY = NaN;
var dx = 1;
var dy = 1;
for (var i =0; i < faces.length; i++) {
var point = faces[i].getDualPoint();
var x1 = point.x;
var y1 = point.y;
if (lastX !== NaN){
dx = lastX - x1;
dy = lastY - y1;
if (dx < 0) {
dx = -dx;
}
if (dy < 0) {
dy = -dy;
}
}
if (dx > epsilon || dy > epsilon) {
protopoly.push([x1, y1]);
lastX = x1;
lastY = y1;
}
}
site.nonClippedPolygon = d3.geom.polygon(protopoly);
if (!site.isDummy) {
site.polygon =
boundingPolygon.clip(site.nonClippedPolygon);
console.log(site.polygon);
}
}
}
}
}
alert("done!");
}
var w = 1000;
var h = 1000;
boundingPoly = d3.geom.polygon([[-w,-h],[-w,2 *h],[2*w,2*h],[2*w,-h]]);
var sites = d3.range(1000).map(function(d, i) {
var r =[Math.random() * w, Math.random()*w, Math.random()*w];
console.log(r);
return(r);
});
var weights = d3.range(1000).map(function() {
var r =Math.random()*10;
console.log(r);
return(r);
});
for (var i = 0; i < sites.length; i++){
for (var j = 0; j < sites.length; j++){
if (i !== j && sites[i][0] === sites[j][0] && sites[i][1] === sites[j][1]
&& sites[i][2] === sites[j][2]){
alert("Discarding: " + sites[i] + " : " + sites[j]);
sites.splice(i,1);
}
}
}
computePowerDiagram(sites, weights, boundingPoly);
function draw(location, w, h, vertices, voronoi, enclosingPolygon) {
$("#chart").empty();
var svg = d3.select(location).append("svg").attr("width", w+100).attr("height", h+100);
svg.selectAll("voronoi")
.data(voronoi)
.enter().append("path")
.attr("class", function(d, i) { return i ? "q" + (i % 9) + "-9" : null; })
.attr("d", function(d) {
return "M" + enclosingPolygon.clip(d).join("L") + "Z";
})
.attr("fill", function(d) {
return "#33CC33";
});
svg.selectAll("circle").data(vertices).enter().append("circle")
.attr("transform", function(d) {
if(!isNaN(d[0])) {
return "translate(" + d[0] + "," + d[1] + ")";
}
})
.attr("r", function(d) {
return 5;
});
svg.selectAll("text").data(vertices).enter().append("text")
.attr("transform", function(d) {
if(!isNaN(d[0])) {
return "translate(" + d[0] + "," + d[1] + ")";
}
})
.text(function(d) {
return d[2];
})
.attr("fill", "white");
}
</script>
</div>
</body>
</html>