-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
351 lines (263 loc) · 10.4 KB
/
index.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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
<!DOCTYPE html>
<meta charset="utf-8">
<style>
body {
background-color: black;
}
svg {
position: absolute;
cursor: none;
width: 640px;
height: 480px;
}
svg {
pointer-events: none;
}
#playground {
position: relative;
}
circle {
stroke: rgba(0.5, 0.5, 0.5, 0.5);
stroke-width: 3px;
animation: tilt-n-move-shaking 1000ms infinite;
transform-origin: center;
filter:drop-shadow(2px 2px black)
}
path {
stroke: white;
stroke-width: 0.5px;
fill: none;
transition: x1, y1, x2, y2 200ms linear;
}
* {
transition: all 200ms linear;
}
@keyframes tilt-n-move-shaking {
0% {
transform: translate(0, 0) rotate(0deg);
}
25% {
transform: translate(1px, 1px) rotate(1deg);
}
50% {
transform: translate(0, 0) rotate(0eg);
}
75% {
transform: translate(-1px, 1px) rotate(-1deg);
}
100% {
transform: translate(0, 0) rotate(0deg);
}
}
.highlight {
animation: tilt-n-move-shaking 100ms infinite;
stroke: orange;
stroke-width: 4px;
}
</style>
<body>
<div id="playground">
<svg id="svg" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 640 480">
</svg>
</div>
<script>
const DELAYATBEGINNING = 2000;
const INTERVAL = 500;
const ECARTRANDOM = 12;
const ECART = 16;
const BEND = 10;
const RADIUSNODE = 16;
function d2(a, b) {
return (a.x - b.x) ** 2 + (a.y - b.y) ** 2;
}
class Graph {
constructor() {
this.nodes = [];
this.edges = [];
this.classes = [];
}
fillRandom(n) {
for (let i = 0; i < n; i++) {
const node = { x: 10 + Math.random() * 620, y: 10 + Math.random() * 460 };
if (this.nodes.every((no) => d2(no, node) > 64 ** 2)) {
this.addNode(node);
}
else
i--;
}
n = this.nodes.length;
function isSegmentCrossing(a, b, c, d) {
function toTheRight(a, b, c) {
return (c.x - a.x) * (b.y - a.y) + (c.y - a.y) * (a.x - b.x);
}
return toTheRight(a, b, c) * toTheRight(a, b, d) < 0 && (toTheRight(c, d, a) * toTheRight(c, d, b) < 0);
}
const isEdgesCrossing = (edge) => {
for (const e of this.edges) {
if (isSegmentCrossing(this.nodes[e.from], this.nodes[e.to], this.nodes[edge.from], this.nodes[edge.to]))
return true;
}
return false;
}
for (let i = 0; i < this.nodes.length; i++)
for (let j = i + 1; j < this.nodes.length; j++)
if (d2(this.nodes[i], this.nodes[j]) < (640 / 4) ** 2) {
if (!isEdgesCrossing({ from: i, to: j })) {
this.addEdge(i, j);
}
}
this.update();
}
addNode(node) {
function generateColor() {
const r = Math.floor(255 * Math.random());
const g = Math.floor(255 * Math.random());
const b = Math.floor(255 * Math.random());
if (r + g + b < 555)
return generateColor();
return `rgb(${r}, ${g}, ${b})`;
}
const circle = document.createElementNS('http://www.w3.org/2000/svg', "circle");
circle.style.fill = generateColor();
circle.style.animationDelay = "10000000ms";
node.svgElement = circle;
this.nodes.push(node);
svg.appendChild(circle);
this.classes.push([this.nodes.length - 1]);
}
addEdge(from, to) {
const svgElement = document.createElementNS('http://www.w3.org/2000/svg', "path");
this.edges.push({ from, to, svgElement: svgElement, offset: BEND * Math.random() - BEND / 2 })
svg.prepend(svgElement);
}
sameClass(i, j) {
const ci = this.classes.findIndex((c) => c.indexOf(i) >= 0);
const cj = this.classes.findIndex((c) => c.indexOf(j) >= 0);
return ci == cj;
}
mergeNodes(i, j) {
let ci = this.classes.findIndex((c) => c.indexOf(i) >= 0);
const cj = this.classes.findIndex((c) => c.indexOf(j) >= 0);
this.classes[ci] = [...this.classes[ci], ...this.classes[cj]];
this.classes.splice(cj, 1);
if (cj < ci)
ci--;
let m = { x: 0, y: 0 };
for (const inode of this.classes[ci]) {
const node = this.nodes[inode];
m.x += node.x;
m.y += node.y;
}
m.x /= this.classes[ci].length;
m.y /= this.classes[ci].length;
for (const inode of this.classes[ci]) {
const node = this.nodes[inode];
node.x = m.x;
node.y = m.y;
node.svgElement.style.transformOrigin = m.x + "px " + m.y + "px";
node.svgElement.style.animationDelay = (500 * (this.nodes.length - this.classes[ci].length) / this.nodes.length + Math.random() * 500) + "ms";
function generateVector() {
const v = { x: 2 * Math.random() - 1, y: 2 * Math.random() - 1 };
const d = Math.sqrt(v.x ** 2 + v.y ** 2);
const f = ECART * Math.random();
v.x *= f / d;
v.y *= f / d;
return v;
}
const rv = generateVector();
node.x += rv.x;
node.y += rv.y;
// node.x = m.x + 16 * Math.random() - 8;
// node.y = m.y + 16 * Math.random() - 8;
}
this.edges = this.edges.filter((e) => {
if (this.sameClass(e.from, e.to)) {
e.svgElement.remove();
return false;
}
else
return true;
}
);
this.update();
}
update() {
for (const node of this.nodes) {
const circle = node.svgElement;
circle.setAttribute("cx", node.x + "");
circle.setAttribute("cy", node.y + "");
circle.setAttribute("r", RADIUSNODE);
}
}
updateEdges() {
function updateSVGElementPath(curveElement, p1, p2, offset) {
// mid-point of line:
var mpx = (p2.x + p1.x) * 0.5;
var mpy = (p2.y + p1.y) * 0.5;
// angle of perpendicular to line:
var theta = Math.atan2(p2.y - p1.y, p2.x - p1.x) - Math.PI / 2;
var c1x = mpx + offset * Math.cos(theta);
var c1y = mpy + offset * Math.sin(theta);
// construct the command to draw a quadratic curve
const curve = "M" + p1.x + " " + p1.y + " Q " + c1x + " " + c1y + " " + p2.x + " " + p2.y;
curveElement.setAttribute("d", curve);
}
const svgr = svg.getBoundingClientRect();
for (const edge of this.edges) {
const svgElement = edge.svgElement;
function getCenter(n) {
const r = n.getBoundingClientRect();
return { x: -svgr.left + (r.left + r.right) / 2, y: -svgr.top + (r.top + r.bottom) / 2 };
}
const n1 = getCenter(this.nodes[edge.from].svgElement);
const n2 = getCenter(this.nodes[edge.to].svgElement);
updateSVGElementPath(svgElement, n1, n2, edge.offset)
/* line.setAttribute("x1", n1.x + "");
line.setAttribute("y1", n1.y + "");
line.setAttribute("x2", n2.x + "");
line.setAttribute("y2", n2.y + "");*/
}
}
}
let graph = new Graph();
const R = 100;
for (let i = 0; i < 6; i++) {
const angle = 2 * Math.PI * i / 6;
graph.addNode({ x: 150 + R * Math.cos(angle), y: 200 +R * Math.sin(angle) });
for (let j = 0; j < i; j++)
graph.addEdge(i, j);
}
for (let i = 0; i < 6; i++) {
const angle = 2 * Math.PI * i / 6;
graph.addNode({ x: 480 + R * Math.cos(angle), y: 200 + R * Math.sin(angle) });
for (let j = 0; j < i; j++)
graph.addEdge(6 + i, 6 + j);
}
graph.addEdge(0, 8);
graph.addEdge(5, 10);
graph.update();
// graph.fillRandom(40);
let state = 0;
let edge = undefined;
function oneStep() {
if (graph.edges.length == 0)
return;
if (graph.classes.length > 2) {
if (state == 0) {
new Audio("./select.ogg").play();
const ie = Math.floor(graph.edges.length * Math.random());
edge = graph.edges[ie];
edge.svgElement.classList.add("highlight");
state = 1;
}
else {
new Audio("./merge.ogg").play();
graph.mergeNodes(edge.from, edge.to);
state = 0;
}
}
}
setTimeout(() =>
setInterval(oneStep, INTERVAL), DELAYATBEGINNING);
setInterval(() => graph.updateEdges(), 10);
</script>