-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathearclip.html
709 lines (636 loc) · 28.1 KB
/
earclip.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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
<!-- vim: sw=2 ts=2 expandtab smartindent ft=javascript
-->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Earclipping Impl + Vis</title>
<style>
document, body { margin: 0px; padding: 0px; overflow: hidden; }
.hud {
position: absolute;
top: 0px;
left: 0px;
padding: 0.5rem;
display: flex;
flex-direction: column;
gap: 0.4rem;
button {
&:hover { opacity: 1.0 }
opacity: 0.8;
border: 2px solid rgba(255, 120, 120, 1);
background-color: red;
color: white;
font-size: 2rem;
}
}
.description {
a { color: turquoise; }
position: absolute;
padding: 0.4rem;
p { margin: 0px; }
bottom: 0px;
left: 0px;
font-family: sans-serif;
color: white;
}
.legend {
position: absolute;
padding: 0.4rem;
p { margin: 0px; }
bottom: 0px;
right: 0px;
font-family: sans-serif;
color: white;
}
</style>
</head>
<body>
<div class="hud">
<button id="earclip_vis_next">next (hold)</button>
<button id="earclip_vis_restart">restart</button>
<div style="color:white;font-family:sans-serif;">
<label for="earclip_vis_preset_shape">Example:</label>
<select id="earclip_vis_preset_shape"></select>
</div>
<div style="color:white;font-family:sans-serif;">
<label for="earclip_vis_show_det">Show <span style="color:orange">determinant</span>:</label>
<input checked=true type="checkbox" id="earclip_vis_show_det"></input>
</div>
</div>
<div class="description">
<p> Earclipping is a technique used to <a href="https://en.wikipedia.org/wiki/Tessellation_(computer_graphics)">tesselate</a> shapes. </p>
<p> You can use it for <a href="https://www.chrishecker.com/My_Liner_Notes_for_Spore#cite_ref-3">creature creation</a> and other generative graphics tasks. </p>
<p> This page is a minimal shape editor and earclipping visualizer. It helps you to understand how earclipping works. </p>
<p> Click and drag a <span style="color: magenta">diamond shaped midpoint</span> to add and begin moving a new vertex. </p>
<p> Click and drag a <span style="color: rgb(150, 50, 150)">circular vertex</span> to manipulate the shape, or hover and use Backspace to remove a vertex. </p>
<p> Click the "next" button to begin the earclipping process.
See also: <a href="https://github.com/memononen/libtess2">libtess</a>, <a href="https://github.com/mapbox/earcut">earcut</a>. </p>
</div>
<div class="legend">
<b> Legend </b>
<p style="color: rgb(255, 100, 100)"> Vertex is concave (innie, dip), not convex (outie, ear). </p>
<p style="color: rgb(100, 100, 255)"> This vertex is convex, but has other points inside it. </p>
<p style="color: rgb(100, 255, 100)"> Good triangle! It's an ear! </p>
<p style="color: orange"> Determinant of this vertex; used to determine concavity. </p>
</div>
<canvas id="glcanvas"></canvas>
<script>"use strict";
const canvas = document.getElementById("glcanvas");
const ctx = canvas.getContext('2d');
(window.onresize = () => {
canvas.width = window.innerWidth*window.devicePixelRatio,
canvas.height = window.innerHeight*window.devicePixelRatio
canvas.style.width = window.innerWidth + 'px';
canvas.style.height = window.innerHeight + 'px';
})();
const state = {
hovered_vertex: null,
hovered_midpoint: null,
shape: [],
ms_to_triangulate: Infinity,
self_intersecting: false,
show_determinant: true,
earclip_progress: {
active_tri_color: [100, 20, 100],
active_tri: null,
indices: [],
vertices: [],
phase: ""
},
restart() {
state.triangulator = step_triangulate(state.shape);
state.earclip_progress.phase = '';
state.earclip_progress.active_tri = null;
state.earclip_progress.indices = [];
state.earclip_progress.vertices = structuredClone(state.shape);
{
function line_hits_line(from0, to0, from1, to1) {
const a = from0.x, b = from0.y,
c = to0.x, d = to0.y,
p = from1.x, q = from1.y,
r = to1.x, s = to1.y;
let det, gamma, lambda;
det = (c - a) * (s - q) - (r - p) * (d - b);
if (det === 0) {
return false;
} else {
lambda = ((s - q) * (r - a) + (p - r) * (s - b)) / det;
gamma = ((b - d) * (r - a) + (c - a) * (s - b)) / det;
return (0.001 < lambda && lambda < 0.999) && (0.001 < gamma && gamma < 0.999);
// return { x: lerp(from0.x, to0.x, lambda), y: lerp(from0.y, to0.y, lambda) };
}
}
const shape = state.shape;
let hit = false;
self_intersection:
for (let i = 0; i < shape.length; i++) {
const a = (i + 0) % shape.length;
const b = (i + 1) % shape.length;
for (let j = 0; j < shape.length; j++) {
const c = (j + 0) % shape.length;
const d = (j + 1) % shape.length;
if (a == c && b == d) continue;
if (line_hits_line(shape[a], shape[b], shape[c], shape[d])) {
hit = true;
break self_intersection;
}
}
}
state.self_intersection = hit;
}
if (!state.self_intersection) {
const vertices = structuredClone(state.shape);
const p = performance.now();
let runs = 1000;
for (let i = 0; i < runs; i++) earclip(vertices);
state.ms_to_triangulate = (performance.now() - p) / runs;
}
},
};
{
const presets = new Map();
presets.set("hollow hexagon", [
{ x: (0.25 - 0.5)*600, y: (0.933013 - 0.5)*600 },
{ x: (0.00 - 0.5)*600, y: (0.500000 - 0.5)*600 },
{ x: (0.25 - 0.5)*600, y: (0.066987 - 0.5)*600 },
{ x: (0.75 - 0.5)*600, y: (0.066987 - 0.5)*600 },
{ x: (1.00 - 0.5)*600, y: (0.500000 - 0.5)*600 },
{ x: (0.75 - 0.5)*600, y: (0.933013 - 0.5)*600 },
{ x: (0.25 - 0.5)*600, y: (0.933013 - 0.5)*600 },
{ x: (0.25 - 0.5)*400, y: (0.933013 - 0.5)*400 },
{ x: (0.75 - 0.5)*400, y: (0.933013 - 0.5)*400 },
{ x: (1.00 - 0.5)*400, y: (0.500000 - 0.5)*400 },
{ x: (0.75 - 0.5)*400, y: (0.066987 - 0.5)*400 },
{ x: (0.25 - 0.5)*400, y: (0.066987 - 0.5)*400 },
{ x: (0.00 - 0.5)*400, y: (0.500000 - 0.5)*400 },
{ x: (0.25 - 0.5)*400, y: (0.933013 - 0.5)*400 },
]);
presets.set("W", [
{ "x": 97, "y": 220 }, { "x": 13, "y": 60 }, { "x": 183, "y": 140 }, { "x": 65, "y": -504 },
{ "x": 105, "y": 50 }, { "x": -315, "y": -472 }, { "x": -27, "y": 118 }, { "x": -691, "y": -38 }
]);
presets.set("blade", [
{ "x": -45, "y": 248 }, { "x": 71, "y": 224 }, { "x": 355, "y": 714 }, { "x": 423, "y": 672 },
{ "x": 127, "y": 214 }, { "x": 183, "y": 140 }, { "x": 359, "y": 6 }, { "x": 253, "y": -76 },
{ "x": 89, "y": 68 }, { "x": -73, "y": -208 }, { "x": -15, "y": -326 }, { "x": -149, "y": -308 },
{ "x": -291, "y": -564 }, { "x": -579, "y": -926 }, { "x": -389, "y": -548 }, { "x": -215, "y": -274 },
{ "x": -313, "y": -112 }, { "x": -149, "y": -160 }, { "x": -27, "y": 118 }, { "x": -337, "y": 180 },
{ "x": -319, "y": 326 }
]);
presets.set("box", [
{ "x": -150, "y": -150 },
{ "x": -150, "y": 150 },
{ "x": 150, "y": 150 },
{ "x": 150, "y": -150 }
]);
presets.set("spacestation", [
{ "x": 573, "y": -16 }, { "x": 300, "y": 0 }, { "x": 285.31695488854604, "y": 92.70509831248422 },
{ "x": 242.70509831248424, "y": 176.33557568774194 }, { "x": 176.33557568774194, "y": 242.70509831248424 },
{ "x": 327, "y": 500 }, { "x": 517, "y": 440 }, { "x": 569, "y": 552 }, { "x": 105, "y": 684 }, { "x": 77, "y": 568 },
{ "x": 237, "y": 526 }, { "x": 92.70509831248424, "y": 285.31695488854604 }, { "x": 1.8369701987210297e-14, "y": 300 },
{ "x": -92.7050983124842, "y": 285.3169548885461 }, { "x": -176.3355756877419, "y": 242.70509831248424 },
{ "x": -242.7050983124842, "y": 176.33557568774197 }, { "x": -285.31695488854604, "y": 92.70509831248425 },
{ "x": -577, "y": 96 }, { "x": -567, "y": 332 }, { "x": -339, "y": 322 }, { "x": -327, "y": 394 }, { "x": -559, "y": 394 },
{ "x": -553, "y": 530 }, { "x": -571, "y": 522 }, { "x": -635, "y": 526 }, { "x": -653, "y": 100 }, { "x": -901, "y": 94 },
{ "x": -901, "y": -2 }, { "x": -619, "y": 2 }, { "x": -631, "y": -456 }, { "x": -543, "y": -460 }, { "x": -529, "y": -90 },
{ "x": -285.31695488854604, "y": -92.70509831248432 }, { "x": -242.70509831248427, "y": -176.3355756877419 },
{ "x": -176.33557568774197, "y": -242.7050983124842 }, { "x": -92.70509831248427, "y": -285.31695488854604 },
{ "x": -5.510910596163089e-14, "y": -300 }, { "x": 59, "y": -480 }, { "x": -197, "y": -572 }, { "x": -173, "y": -638 },
{ "x": 87, "y": -550 }, { "x": 125, "y": -654 }, { "x": 297, "y": -598 }, { "x": 176.33557568774188, "y": -242.70509831248427 },
{ "x": 242.7050983124842, "y": -176.335575687742 }, { "x": 277, "y": -108 }, { "x": 389, "y": -122 }, { "x": 361, "y": -440 },
{ "x": 441, "y": -442 }, { "x": 467, "y": -122 }, { "x": 565, "y": -128 }
]);
presets.set("circle", Array.from({ length: 20 }, (_, i) => {
const x = Math.cos(i / 20 * Math.PI * 2) * 300;
const y = Math.sin(i / 20 * Math.PI * 2) * 300;
return ({ x, y });
}));
presets.set("abomination", [
{ "x": 573, "y": -16 }, { "x": 705, "y": -68 }, { "x": 687, "y": 22 }, { "x": 787, "y": 0 }, { "x": 807, "y": 54 }, { "x": 769, "y": 90 },
{ "x": 801, "y": 148 }, { "x": 763, "y": 182 }, { "x": 681, "y": 124 }, { "x": 657, "y": 208 }, { "x": 601, "y": 182 }, { "x": 551, "y": 172 },
{ "x": 615, "y": 76 }, { "x": 537, "y": 90 }, { "x": 443, "y": 62 }, { "x": 300, "y": 0 }, { "x": 285.31695488854604, "y": 92.70509831248422 },
{ "x": 242.70509831248424, "y": 176.33557568774194 }, { "x": 176.33557568774194, "y": 242.70509831248424 }, { "x": 267, "y": 352 }, { "x": 347, "y": 358 },
{ "x": 427, "y": 406 }, { "x": 327, "y": 500 }, { "x": 439, "y": 488 }, { "x": 517, "y": 440 }, { "x": 667, "y": 460 }, { "x": 723, "y": 508 }, { "x": 769, "y": 588 },
{ "x": 653, "y": 618 }, { "x": 503, "y": 634 }, { "x": 405, "y": 614 }, { "x": 283, "y": 662 }, { "x": 105, "y": 684 }, { "x": 77, "y": 568 }, { "x": 237, "y": 526 },
{ "x": 143, "y": 488 }, { "x": 69, "y": 446 }, { "x": 97, "y": 404 }, { "x": 121, "y": 382 }, { "x": 175, "y": 372 }, { "x": 92.70509831248424, "y": 285.31695488854604 },
{ "x": 1.8369701987210297e-14, "y": 300 }, { "x": -92.7050983124842, "y": 285.3169548885461 }, { "x": -176.3355756877419, "y": 242.70509831248424 },
{ "x": -242.7050983124842, "y": 176.33557568774197 }, { "x": -285.31695488854604, "y": 92.70509831248425 }, { "x": -577, "y": 96 }, { "x": -567, "y": 332 },
{ "x": -321, "y": 326 }, { "x": -327, "y": 394 }, { "x": -559, "y": 394 }, { "x": -551, "y": 524 }, { "x": -635, "y": 526 }, { "x": -653, "y": 100 }, { "x": -901, "y": 94 },
{ "x": -901, "y": -2 }, { "x": -619, "y": 2 }, { "x": -631, "y": -456 }, { "x": -543, "y": -460 }, { "x": -529, "y": -90 }, { "x": -439, "y": -86 }, { "x": -429, "y": 24 },
{ "x": -363, "y": 22 }, { "x": -365, "y": -84 }, { "x": -285.31695488854604, "y": -92.70509831248432 }, { "x": -381, "y": -158 }, { "x": -333, "y": -244 }, { "x": -365, "y": -358 },
{ "x": -265, "y": -386 }, { "x": -163, "y": -374 }, { "x": -195, "y": -176 }, { "x": -179, "y": -52 }, { "x": -83, "y": -74 }, { "x": -1, "y": -4 }, { "x": -97, "y": 54 },
{ "x": -7, "y": 100 }, { "x": -91, "y": 160 }, { "x": 7, "y": 164 }, { "x": 37, "y": 222 }, { "x": 99, "y": 228 }, { "x": 121, "y": 164 }, { "x": 157, "y": 124 }, { "x": 73, "y": 30 },
{ "x": 187, "y": -8 }, { "x": 79, "y": -90 }, { "x": 71, "y": -182 }, { "x": -65, "y": -152 }, { "x": -92.70509831248427, "y": -285.31695488854604 }, { "x": -5.510910596163089e-14, "y": -300 },
{ "x": 59, "y": -480 }, { "x": -197, "y": -572 }, { "x": -173, "y": -638 }, { "x": 87, "y": -550 }, { "x": 125, "y": -654 }, { "x": 297, "y": -598 },
{ "x": 176.33557568774188, "y": -242.70509831248427 }, { "x": 242.7050983124842, "y": -176.335575687742 }, { "x": 277, "y": -108 }, { "x": 389, "y": -122 }, { "x": 469, "y": -334 },
{ "x": 361, "y": -440 }, { "x": 399, "y": -524 }, { "x": 441, "y": -442 }, { "x": 537, "y": -404 }, { "x": 655, "y": -464 }, { "x": 513, "y": -548 }, { "x": 611, "y": -572 },
{ "x": 615, "y": -622 }, { "x": 525, "y": -656 }, { "x": 681, "y": -668 }, { "x": 819, "y": -684 }, { "x": 909, "y": -636 }, { "x": 809, "y": -552 }, { "x": 941, "y": -536 },
{ "x": 1015, "y": -440 }, { "x": 1049, "y": -264 }, { "x": 895, "y": -378 }, { "x": 893, "y": -272 }, { "x": 773, "y": -294 }, { "x": 817, "y": -364 }, { "x": 755, "y": -380 },
{ "x": 759, "y": -456 }, { "x": 689, "y": -404 }, { "x": 671, "y": -346 }, { "x": 537, "y": -300 }, { "x": 615, "y": -284 }, { "x": 575, "y": -222 }, { "x": 665, "y": -250 },
{ "x": 779, "y": -198 }, { "x": 687, "y": -170 }, { "x": 467, "y": -122 }, { "x": 565, "y": -128 }
]);
presets.set("treeman", [
{ "x": 97, "y": 220 }, { "x": 512, "y": 802 }, { "x": 448, "y": 558 }, { "x": 714, "y": 616 }, { "x": 183, "y": 140 }, { "x": 238, "y": -246 }, { "x": 314, "y": -480 },
{ "x": 228, "y": -362 }, { "x": 180, "y": -530 }, { "x": 184, "y": -298 }, { "x": 105, "y": 50 }, { "x": -18, "y": -184 }, { "x": -112, "y": -186 }, { "x": -58, "y": -274 },
{ "x": -136, "y": -622 }, { "x": -172, "y": -338 }, { "x": -315, "y": -472 }, { "x": -248, "y": -264 }, { "x": -530, "y": -330 }, { "x": -248, "y": -150 }, { "x": -126, "y": -180 },
{ "x": -214, "y": -106 }, { "x": -27, "y": 118 }, { "x": -402, "y": -8 }, { "x": -720, "y": -114 }, { "x": -756, "y": -312 }, { "x": -774, "y": -134 }, { "x": -932, "y": -262 },
{ "x": -798, "y": -98 }, { "x": -1116, "y": -64 }, { "x": -780, "y": -54 }, { "x": -1020, "y": 178 }, { "x": -691, "y": -38 }
]);
const dropdown = earclip_vis_preset_shape;
for (const example of presets.keys()) {
const option = document.createElement("option");
option.textContent = example;
option.value = example;
dropdown.appendChild(option);
}
(dropdown.oninput = () => {
state.shape = structuredClone(presets.get(dropdown.value));
state.restart();
})()
earclip_vis_show_det.oninput = () => {
state.show_determinant = earclip_vis_show_det.checked;
}
}
function *step_triangulate(shape) {
const vertices = structuredClone(shape);
const is_ear = new Map(vertices.map(x => [x, false]));
const original_index = new Map(vertices.map((x, i) => [x, i]));
/* reverse shape if necessary */
{
let sum = 0;
/* signed area! */
for (let i = 0, j = shape.length - 1; i < shape.length; i++) {
sum += (shape[j].x - shape[i].x) * (shape[i].y + shape[j].y);
j = i;
}
if (sum < 0)
vertices.reverse();
}
const REJECTION_NONE = 0
const REJECTION_DETERMINANT = 1
const REJECTION_TRI_INSIDE = 2
function set_is_ear(a, b, c) {
/* it's an ear if the determinant is negative (convex)
* and there are no points inside of it */
/* convex check */
{
const d1x = vertices[a].x - vertices[b].x;
const d1y = vertices[a].y - vertices[b].y;
const d2x = vertices[b].x - vertices[c].x;
const d2y = vertices[b].y - vertices[c].y;
if ((d1x*d2y - d1y*d2x) < 0) {
is_ear.set(vertices[b], false);
return REJECTION_DETERMINANT;
}
}
/* make sure the triangle is empty inside, akin to this author */
let hit = false;
{
/* make sure no points from other triangles are inside this triangle */
for (let j = 0; j < vertices.length; j++) {
if (j == a || j == b || j == c) continue;
const p = vertices[j], v1 = vertices[a], v2 = vertices[b], v3 = vertices[c];
const alpha = ((v2.y - v3.y) * ( p.x - v3.x) + (v3.x - v2.x) * ( p.y - v3.y)) /
((v2.y - v3.y) * (v1.x - v3.x) + (v3.x - v2.x) * (v1.y - v3.y));
const beta = ((v3.y - v1.y) * ( p.x - v3.x) + (v1.x - v3.x) * ( p.y - v3.y)) /
((v2.y - v3.y) * (v1.x - v3.x) + (v3.x - v2.x) * (v1.y - v3.y));
const gamma = 1.0 - alpha - beta;
const contained = (alpha > 0 && beta > 0 && gamma > 0)
if (contained) {
hit = true;
break;
}
}
}
is_ear.set(vertices[b], !hit);
return hit ? REJECTION_TRI_INSIDE : REJECTION_NONE;
}
let indices = [];
while (vertices.length >= 3) {
for (let i = 0; i < vertices.length; i++) {
const a = (i + 0) % vertices.length;
const b = (i + 1) % vertices.length;
const c = (i + 2) % vertices.length;
const result = set_is_ear(a, b, c);
yield ({
phase: (vertices.length - 2) + ' remaining',
vertices,
active_tri_color: ({
[REJECTION_NONE ]: [100, 255, 100],
[REJECTION_DETERMINANT]: [255, 0, 0],
[REJECTION_TRI_INSIDE ]: [ 0, 0, 255],
})[result],
active_tri: [
original_index.get(vertices[a]),
original_index.get(vertices[b]),
original_index.get(vertices[c])
],
known_ears: vertices.filter(x => is_ear.get(x)).map(x => {
const v_idx = vertices.indexOf(x);
return [
original_index.get(vertices.at(v_idx - 1)),
original_index.get(vertices.at(v_idx + 0)),
original_index.get(vertices.at((v_idx + 1) % vertices.length))
];
}),
indices
})
if (is_ear.get(vertices[b])) {
indices.push(original_index.get(vertices[a]),
original_index.get(vertices[b]),
original_index.get(vertices[c]));
vertices.splice(b, 1);
break;
}
}
}
}
let canvas_transform;
let mouse_screen_x = 0;
let mouse_screen_y = 0;
let mouse_down = false;
canvas.onmouseup = e => {
mouse_down = false;
}
canvas.onmousedown = e => {
mouse_down = true;
}
canvas.onmousemove = e => {
if (!canvas_transform) return;
const mouse = canvas_transform.transformPoint(new DOMPoint(e.offsetX * window.devicePixelRatio, e.offsetY * window.devicePixelRatio))
mouse_screen_x = mouse.x;
mouse_screen_y = mouse.y;
if (mouse_down && state.hovered_vertex) {
if (state.hovered_midpoint) {
state.shape.splice(state.shape.indexOf(state.hovered_vertex), 0, state.hovered_midpoint);
state.hovered_vertex = state.hovered_midpoint;
state.hovered_midpoint = null;
}
state.hovered_vertex.x = mouse_screen_x;
state.hovered_vertex.y = mouse_screen_y;
state.restart()
}
}
{
function next() {
const progress = state.triangulator.next().value;
if (progress) {
state.earclip_progress = progress;
} else {
state.earclip_progress.active_tri = null;
state.earclip_progress.phase = 'done!';
}
}
earclip_vis_next.onclick = next;
earclip_vis_next.onmousedown = () => {
let skip_first = true;
const x = setInterval(() => {
if (skip_first) { skip_first = false; return; }
next();
}, 100);
earclip_vis_next.onmouseup = () => clearInterval(x);
}
earclip_vis_restart.onclick = () => state.restart();
window.onkeydown = e => {
if (state.hovered_vertex && e.key == "Backspace" || e.key == "Delete") {
state.shape.splice(state.shape.indexOf(state.hovered_vertex), 1);
state.restart();
}
if (e.key == " ") next();
if (e.key == "Escape") state.restart()
}
}
/* naive copy-pastable version of this algorithm, used for performance timing */
function earclip(vertices) {
const original_index = new Map(vertices.map((x, i) => [x, i]));
/* reverse shape if necessary */
{
let sum = 0;
/* signed area! */
for (let i = 0, j = vertices.length - 1; i < vertices.length; i++) {
sum += (vertices[j].x - vertices[i].x) * (vertices[i].y + vertices[j].y);
j = i;
}
if (sum < 0)
vertices.reverse();
}
function is_ear(a, b, c) {
/* it's an ear if the determinant is negative (convex) and there are no points inside of it */
/* convex check */
{
const d1x = vertices[a].x - vertices[b].x;
const d1y = vertices[a].y - vertices[b].y;
const d2x = vertices[b].x - vertices[c].x;
const d2y = vertices[b].y - vertices[c].y;
if ((d1x*d2y - d1y*d2x) < 0) {
return false;
}
}
/* make sure the triangle is empty inside */
let hit = false;
/* make sure no points from other triangles are inside this triangle */
for (let j = 0; j < vertices.length; j++) {
if (j == a || j == b || j == c) continue;
const p = vertices[j], v1 = vertices[a], v2 = vertices[b], v3 = vertices[c];
const alpha = ((v2.y - v3.y) * ( p.x - v3.x) + (v3.x - v2.x) * ( p.y - v3.y)) /
((v2.y - v3.y) * (v1.x - v3.x) + (v3.x - v2.x) * (v1.y - v3.y));
const beta = ((v3.y - v1.y) * ( p.x - v3.x) + (v1.x - v3.x) * ( p.y - v3.y)) /
((v2.y - v3.y) * (v1.x - v3.x) + (v3.x - v2.x) * (v1.y - v3.y));
const gamma = 1.0 - alpha - beta;
const contained = (alpha > 0 && beta > 0 && gamma > 0)
if (contained) {
hit = true;
break;
}
}
return !hit;
}
let indices = [];
let escape_hatch = 0;
while (vertices.length >= 3) {
for (let i = 0; i < vertices.length; i++) {
const a = (i + 0) % vertices.length;
const b = (i + 1) % vertices.length;
const c = (i + 2) % vertices.length;
if (is_ear(a, b, c)) {
indices.push(original_index.get(vertices[a]),
original_index.get(vertices[b]),
original_index.get(vertices[c]));
vertices.splice(b, 1);
break;
}
}
if (escape_hatch++ > 1e6) break;
}
return indices;
}
requestAnimationFrame(function render(now) {
requestAnimationFrame(render);
ctx.fillStyle = "black";
ctx.fillRect(0, 0, canvas.width, canvas.height);
const shape = state.shape;
{
let y = 100;
if (state.earclip_progress.phase) {
ctx.fillStyle = 'turquoise';
ctx.font = '75px sans-serif';
const text = state.earclip_progress.phase;
ctx.fillText(text, canvas.width - ctx.measureText(text).width - 30, y);
y += 100
}
if (!state.self_intersection) {
ctx.font = '50px sans-serif';
ctx.fillStyle = 'chartreuse';
const text = 'triangulated in ' + state.ms_to_triangulate.toFixed(5) + 'ms';
const w = ctx.measureText(text)
ctx.fillText(text, canvas.width - w.width - 30, y);
y += 70
}
if (state.self_intersection) {
ctx.font = '50px sans-serif';
ctx.fillStyle = 'red';
const text = 'self intersection!';
const w = ctx.measureText(text)
ctx.fillText(text, canvas.width - w.width - 30, y);
y += 70
}
}
ctx.save();
{
ctx.translate(canvas.width*0.5, canvas.height*0.5);
canvas_transform = ctx.getTransform().inverse();
ctx.lineWidth = 5;
ctx.strokeStyle = 'white';
{
let hovered_dist = Infinity;
if (state.hovered_vertex)
hovered_dist = Math.sqrt((state.hovered_vertex.x - mouse_screen_x)*(state.hovered_vertex.x - mouse_screen_x) +
(state.hovered_vertex.y - mouse_screen_y)*(state.hovered_vertex.y - mouse_screen_y));
if (state.hovered_midpoint)
hovered_dist = Math.sqrt((state.hovered_midpoint.x - mouse_screen_x)*(state.hovered_midpoint.x - mouse_screen_x) +
(state.hovered_midpoint.y - mouse_screen_y)*(state.hovered_midpoint.y - mouse_screen_y));
let hovered_vertex, hovered_midpoint;
for (let i = 0; i < shape.length; i++) {
const last = shape.at(i - 1);
const next = shape.at(i);
ctx.beginPath();
ctx.moveTo(last.x, last.y)
ctx.lineTo(next.x, next.y)
ctx.stroke();
const center_x = lerp(last.x, next.x, 0.5);
const center_y = lerp(last.y, next.y, 0.5);
const dist = Math.sqrt((center_x - mouse_screen_x)*(center_x - mouse_screen_x) +
(center_y - mouse_screen_y)*(center_y - mouse_screen_y));
const hover = state.hovered_midpoint && (state.hovered_midpoint.x == center_x && state.hovered_midpoint.y == center_y);
const size = (hover) ? 25 : 15;
if ((dist < 30) && dist <= hovered_dist) {
hovered_vertex = next;
hovered_dist = dist;
hovered_midpoint = { x: center_x, y: center_y };
}
ctx.fillStyle = "magenta";
ctx.translate(center_x, center_y);
ctx.rotate(-Math.PI*0.25)
ctx.fillRect(-size*0.5, -size*0.5, size, size);
ctx.rotate( Math.PI*0.25)
ctx.translate(-center_x, -center_y);
}
for (let i = 0; i < shape.length; i++) {
const p = shape.at(i);
const dist = Math.sqrt((p.x - mouse_screen_x)*(p.x - mouse_screen_x) +
(p.y - mouse_screen_y)*(p.y - mouse_screen_y));
const hover = state.hovered_vertex == p && !state.hovered_midpoint;
const size = hover ? 27 : 20;
if ((dist < 30) && dist <= hovered_dist) {
hovered_dist = dist;
hovered_vertex = p;
}
ctx.fillStyle = "rgb(150, 50, 150)";
ctx.beginPath()
ctx.arc(p.x, p.y, size, 0, Math.PI*2);
ctx.fill()
if (state.show_determinant) {
const a = i == 0 ? shape.length - 1 : i - 1;
const b = i;
const c = (i + 1) % shape.length;
let det;
{
let d1x = shape[a].x - shape[b].x;
let d1y = shape[a].y - shape[b].y;
d1x /= Math.sqrt(d1x*d1x + d1y*d1y);
d1y /= Math.sqrt(d1x*d1x + d1y*d1y);
let d2x = shape[b].x - shape[c].x;
let d2y = shape[b].y - shape[c].y;
d2x /= Math.sqrt(d2x*d2x + d2y*d2y);
d2y /= Math.sqrt(d2x*d2x + d2y*d2y);
det = (d1x*d2y - d1y*d2x);
}
ctx.fillStyle = 'orange';
ctx.font = '50px sans-serif';
ctx.fillText(det.toFixed(2), p.x + 20, p.y + 12);
}
}
state.hovered_vertex = hovered_vertex;
state.hovered_midpoint = hovered_midpoint;
}
if (state.earclip_progress.active_tri) {
const { vertices, active_tri, active_tri_color } = state.earclip_progress;
for (let i = 0; i < vertices.length; i++) {
const last = vertices.at(i - 1);
const next = vertices.at(i);
ctx.beginPath();
ctx.moveTo(last.x, last.y)
ctx.lineTo(next.x, next.y)
ctx.strokeStyle = 'turquoise';
ctx.stroke();
}
{
ctx.beginPath();
ctx.moveTo(shape[active_tri[0]].x, shape[active_tri[0]].y);
ctx.lineTo(shape[active_tri[1]].x, shape[active_tri[1]].y);
ctx.lineTo(shape[active_tri[2]].x, shape[active_tri[2]].y);
ctx.fillStyle = 'rgba(' + active_tri_color.join(', ') + ', 0.4)';
ctx.fill();
}
for (const ear of state.earclip_progress.known_ears) {
ctx.beginPath();
ctx.moveTo(shape[ear[0]].x, shape[ear[0]].y);
ctx.lineTo(shape[ear[1]].x, shape[ear[1]].y);
ctx.lineTo(shape[ear[2]].x, shape[ear[2]].y);
ctx.fillStyle = 'rgba(100, 255, 100, 0.2)';
ctx.fill();
}
}
// state.earclip_progress.indices = earclip(structuredClone(shape));
{
const indices = state.earclip_progress.indices;
for (let idx = 0; idx < indices.length; idx += 3) {
ctx.beginPath();
ctx.moveTo(shape[indices[idx + 0]].x, shape[indices[idx + 0]].y);
ctx.lineTo(shape[indices[idx + 1]].x, shape[indices[idx + 1]].y);
ctx.lineTo(shape[indices[idx + 2]].x, shape[indices[idx + 2]].y);
ctx.fillStyle = 'rgba(100, 20, 100, 0.4)';
ctx.fill();
}
}
{
const indices = state.earclip_progress.indices;
for (let idx = 0; idx < indices.length; idx += 3) {
ctx.beginPath();
ctx.moveTo(shape[indices[idx + 0]].x, shape[indices[idx + 0]].y);
ctx.lineTo(shape[indices[idx + 1]].x, shape[indices[idx + 1]].y);
ctx.lineTo(shape[indices[idx + 2]].x, shape[indices[idx + 2]].y);
ctx.lineTo(shape[indices[idx + 0]].x, shape[indices[idx + 0]].y);
ctx.strokeStyle = 'rgba(100, 20, 100, 0.2)';
ctx.stroke();
}
}
}
ctx.restore();
})
function lerp(v0, v1, t) { return (1 - t) * v0 + t * v1; }
function inv_lerp(min, max, p) { return (p - min) / (max - min); }
</script>
</body>
</html>