forked from migurski/canvas-warp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
delaunay.html
175 lines (146 loc) · 5.25 KB
/
delaunay.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
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>Canvas Triangulate</title>
<style type="text/css" title="text/css">
<!--
body { background-color: #bbb; }
#c { border: 1px solid black; background-color: #ccc; }
-->
</style>
</head>
<body>
<canvas id="c" width="650" height="390"></canvas>
<script type="text/javascript" language="javascript1.5">
<!--
var c = document.getElementById('c').getContext('2d');
var w = document.getElementById('c').width;
var h = document.getElementById('c').height;
var points = [];
/*
points.push([w/4, 0]);
points.push([w * 3/4, 0]);
points.push([w/4, h/2]);
points.push([w * 3/4, h/2]);
*/
for(var i = 0; i < 15; i += 1)
{
var x = Math.round(w/4 + Math.random() * w/2);
var y = Math.round( 0 + Math.random() * h/2);
points.push([x, y]);
}
function circumcircle(p1, p2, p3)
{
var x1 = p1[0];
var y1 = p1[1];
var x2 = p2[0];
var y2 = p2[1];
var x3 = p3[0];
var y3 = p3[1];
var sq = function(x) { return Math.pow(x, 2); };
var d = 2 * (x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2));
var x = ((sq(y1) + sq(x1)) * (y2 - y3) + (sq(y2) + sq(x2)) * (y3 - y1) + (sq(y3) + sq(x3)) * (y1 - y2)) / d;
var y = ((sq(y1) + sq(x1)) * (x3 - x2) + (sq(y2) + sq(x2)) * (x1 - x3) + (sq(y3) + sq(x3)) * (x2 - x1)) / d;
var r1 = Math.sqrt(sq(x1 - x) + sq(y1 - y));
var r2 = Math.sqrt(sq(x2 - x) + sq(y2 - y));
var r3 = Math.sqrt(sq(x3 - x) + sq(y3 - y));
var r = (r1 + r2 + r3) / 3;
return [x, y, r];
}
function inCircle(point, circle)
{
var distance = Math.sqrt(Math.pow(point[0] - circle[0], 2) + Math.pow(point[1] - circle[1], 2));
return distance < circle[2];
}
//var supertriangle = [[-w/2, 0], [w*3/2, 0], [w/2, h*2]];
var supertriangle = [[0, 0], [w, 0], [w/2, h]];
points.push(supertriangle[0]);
points.push(supertriangle[1]);
points.push(supertriangle[2]);
var triangles = [supertriangle];
/**
*/
function addPoint(point, triangles)
{
var edges = [];
for(var i = 0; i < triangles.length; i += 1)
{
var tri = triangles[i];
var cc = circumcircle(tri[0], tri[1], tri[2]);
if(inCircle(point, cc))
{
edges.push([tri[0], tri[1]]);
edges.push([tri[1], tri[2]]);
edges.push([tri[2], tri[0]]);
triangles.splice(i, 1);
i -= 1;
}
}
var allEdges = edges;
var uniqEdges = [];
for(var i = 0; i < edges.length; i += 1)
{
var unique = true;
for(var j = 0; j < edges.length; j += 1)
{
if(i != j && edgesMatch(edges[i], edges[j]))
{
unique = false;
}
}
if(unique)
{
uniqEdges.push(edges[i]);
}
}
edges = uniqEdges;
while(edges.length)
{
var e = edges.pop();
var tri = [point, e[0], e[1]];
triangles.push(tri);
}
}
function edgesMatch(e1, e2)
{
var e1p1 = e1[0];
var e1p2 = e1[1];
var e2p1 = e2[0];
var e2p2 = e2[1];
return ((e1p1[0] == e2p1[0] && e1p1[1] == e2p1[1] && e1p2[0] == e2p2[0] && e1p2[1] == e2p2[1])
|| (e1p1[0] == e2p2[0] && e1p1[1] == e2p2[1] && e1p2[0] == e2p1[0] && e1p2[1] == e2p1[1]));
}
function drawTriangle(c, p1, p2, p3)
{
c.beginPath();
c.moveTo(p1[0], p1[1]);
c.lineTo(p2[0], p2[1]);
c.lineTo(p3[0], p3[1]);
c.lineTo(p1[0], p1[1]);
c.closePath();
c.strokeStyle = '#999';
c.stroke();
}
for(var i = 0; i < points.length - 3; i += 1)
{
addPoint(points[i], triangles);
}
for(var i = 0; i < triangles.length; i += 1)
{
var t = triangles[i];
drawTriangle(c, t[0], t[1], t[2]);
}
for(var i = 0; i < points.length; i += 1)
{
c.beginPath();
c.arc(points[i][0], points[i][1], 3, 0, Math.PI * 2, true);
c.closePath();
c.fillStyle = '#666666';
c.fill();
}
//-->
</script>
</body>
</html>