-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpolygon-triangulation.html
More file actions
544 lines (465 loc) · 16.5 KB
/
polygon-triangulation.html
File metadata and controls
544 lines (465 loc) · 16.5 KB
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="msapplication-TileColor" content="#603cba">
<meta name="theme-color" content="#ffffff">
<link rel="apple-touch-icon" sizes="180x180" href="/favicon/apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="32x32" href="/favicon/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="/favicon/favicon-16x16.png">
<link rel="manifest" href="/favicon/site.webmanifest">
<link rel="mask-icon" href="/favicon/safari-pinned-tab.svg" color="#5bbad5">
<link rel="stylesheet" href="/assets/css/reveal.css">
<link rel="stylesheet" href="/assets/css/theme/custom.css">
<link rel="stylesheet" href="/assets/icons/style.css">
<link rel="stylesheet" href="/assets/js/reveal-plugins/highlight/night-owl.min.css">
<link rel="stylesheet" href="/assets/css/common.css">
<link rel="stylesheet" href="/assets/css/page.css">
<script src="/assets/js/reveal-plugins/highlight/highlight.min.js"></script>
<script src="/assets/js/reveal-plugins/highlight/highlightjs-line-numbers.min.js"></script>
<script type="text/x-mathjax-config">
MathJax.Hub.Register.StartupHook("End", function () {
setTimeout(function(){
screenh = window.innerHeight;
headerh = document.getElementById('header-nav').offsetHeight;
document.querySelector('.reveal').style.height = (screenh - headerh) + 'px';
update_height(screenh, headerh);
handleMathOverflow();
handleSvgSize(screenh, headerh);
handleCode(screenh, headerh);
}, 500)
});
</script>
<!-- Begin Jekyll SEO tag v2.7.1 -->
<title>Polygon Triangulation | SlideMe</title>
<meta name="generator" content="Jekyll v4.2.1" />
<meta property="og:title" content="Polygon Triangulation" />
<meta name="author" content="Edgardo Estanga" />
<meta property="og:locale" content="en_US" />
<meta name="description" content="The idea is to break up a polygon into triangles. To do this, we show how to implement the “ear clipping” algorithm." />
<meta property="og:description" content="The idea is to break up a polygon into triangles. To do this, we show how to implement the “ear clipping” algorithm." />
<link rel="canonical" href="https://isedgar.github.io/polygon-triangulation.html" />
<meta property="og:url" content="https://isedgar.github.io/polygon-triangulation.html" />
<meta property="og:site_name" content="SlideMe" />
<meta property="og:image" content="https://isedgar.github.io/assets/images/polygon-triangulation.gif" />
<meta property="og:type" content="article" />
<meta property="article:published_time" content="2021-07-17T00:00:00-04:00" />
<meta name="twitter:card" content="summary_large_image" />
<meta property="twitter:image" content="https://isedgar.github.io/assets/images/polygon-triangulation.gif" />
<meta property="twitter:title" content="Polygon Triangulation" />
<script type="application/ld+json">
{"@type":"BlogPosting","url":"https://isedgar.github.io/polygon-triangulation.html","image":"https://isedgar.github.io/assets/images/polygon-triangulation.gif","headline":"Polygon Triangulation","dateModified":"2021-07-17T00:00:00-04:00","datePublished":"2021-07-17T00:00:00-04:00","author":{"@type":"Person","name":"Edgardo Estanga"},"description":"The idea is to break up a polygon into triangles. To do this, we show how to implement the “ear clipping” algorithm.","mainEntityOfPage":{"@type":"WebPage","@id":"https://isedgar.github.io/polygon-triangulation.html"},"@context":"https://schema.org"}</script>
<!-- End Jekyll SEO tag -->
</head>
<body>
<header id="header-nav" class="page-flex flex-align-center">
<a href="/#/topics" id="go2topics"><i class="icon-back-s"></i></a>
<p class="p-font"> 2021-07-17</p>
</header>
<div class="reveal">
<div class="slides">
<section id="s0">
<div style="position: relative;">
<div class="div-outer">
<div class="div-middle">
<div class="div-inner">
<h1 class="h1-font">Polygon Triangulation</h1>
<h3 class="h3-font txt-yellow">"Ear Clipping" Algorithm</h3>
</div>
</div>
</div>
</div>
</section>
<section id="s1">
<div style="position: relative;">
<div class="div-outer">
<div class="div-middle">
<div class="div-inner">
<h3 class="h3-font">The idea is to <mark>break up a polygon into triangles</mark>.</h3>
</div>
</div>
</div>
</div>
</section>
<section id="s2">
<div style="position: relative;">
<div class="div-outer">
<div class="div-middle">
<div class="div-inner">
<h3 class="h3-font">So instead of having a "BIG polygon"...</h3>
</div>
</div>
</div>
</div>
</section>
<section id="s3">
<div style="position: relative;">
<div class="div-outer">
<div class="div-middle">
<div class="div-inner">
<div class="svg"></div>
</div>
</div>
</div>
</div>
</section>
<section id="s4">
<div style="position: relative;">
<div class="div-outer">
<div class="div-middle">
<div class="div-inner">
<h3 class="h3-font">...we have \(n\) "mini polygons". In this case 3:</h3>
</div>
</div>
</div>
</div>
</section>
<section id="s5">
<div style="position: relative;">
<div class="div-outer">
<div class="div-middle">
<div class="div-inner">
<div class="svg"></div>
</div>
</div>
</div>
</div>
</section>
<section id="s6">
<div style="position: relative;">
<div class="div-outer">
<div class="div-middle">
<div class="div-inner">
<h3 class="h3-font">To do this, we implement an algorithm called <a href="https://arxiv.org/pdf/1212.6038.pdf" target="_blank">"ear clipping"</a>.</h3>
</div>
</div>
</div>
</div>
</section>
<section id="s7">
<div style="position: relative;">
<div class="div-outer">
<div class="div-middle">
<div class="div-inner">
<h3 class="h3-font">For example, let's triangulate this polygon with vertices:</h3>
<div class="h3-font math-overflow">
\[
a=(20,20), b=(80,20), c=(50,50), d=(80,80), e=(20,80)
\]
</div>
</div>
</div>
</div>
</div>
</section>
<section id="s8">
<div style="position: relative;">
<div class="div-outer">
<div class="div-middle">
<div class="div-inner">
<div class="svg"></div>
</div>
</div>
</div>
</div>
</section>
<section id="s9">
<div style="position: relative;">
<div class="div-outer">
<div class="div-middle">
<div class="div-inner">
<h3 class="h3-font">1. Check the <a href="/polygon-properties.html#/s15" target="_blank">orientation of the polygon</a>.</h3>
</div>
</div>
</div>
</div>
</section>
<section id="s10">
<div style="position: relative;">
<div class="div-outer">
<div class="div-middle">
<div class="div-inner">
<div class="svg"></div>
</div>
</div>
</div>
</div>
</section>
<section id="s11">
<div style="position: relative;">
<div class="div-outer">
<div class="div-middle">
<div class="div-inner">
<h3 class="h3-font">2. For each vertex, get its <a href="/angles-from-3-points.html" target="_blank">interior angle</a>...</h3>
</div>
</div>
</div>
</div>
</section>
<section id="s12">
<div style="position: relative;">
<div class="div-outer">
<div class="div-middle">
<div class="div-inner">
<div class="svg"></div>
</div>
</div>
</div>
</div>
</section>
<section id="s13">
<div style="position: relative;">
<div class="div-outer">
<div class="div-middle">
<div class="div-inner">
<h3 class="h3-font">...and the orientation of the triangle formed by the previous, current and next vertices.</h3>
</div>
</div>
</div>
</div>
</section>
<section id="s14">
<div style="position: relative;">
<div class="div-outer">
<div class="div-middle">
<div class="div-inner">
<h3 class="h3-font">For example: angle at \(a\), orientation of \(\overline{eab}\); angle at \(b\), orientation of \(\overline{abc}\); and so on.</h3>
</div>
</div>
</div>
</div>
</section>
<section id="s15">
<div style="position: relative;">
<div class="div-outer">
<div class="div-middle">
<div class="div-inner">
<h3 class="h3-font">If the orientation of the polygon and a triangle do not match, then that vertex has a reflex angle (greater than 180°).</h3>
</div>
</div>
</div>
</div>
</section>
<section id="s16">
<div style="position: relative;">
<div class="div-outer">
<div class="div-middle">
<div class="div-inner">
<h3 class="h3-font">In that case subtract the angle from 360°:</h3>
<div class="h3-font math-overflow">
\[
\theta = 360^{\circ} - \theta
\]
</div>
</div>
</div>
</div>
</div>
</section>
<section id="s17">
<div style="position: relative;">
<div class="div-outer">
<div class="div-middle">
<div class="div-inner">
<h3 class="h3-font">For example the triangle \(\overline{bcd}\) is counterclockwise, so the angle at vertex \(c\) is reflex:</h3>
</div>
</div>
</div>
</div>
</section>
<section id="s18">
<div style="position: relative;">
<div class="div-outer">
<div class="div-middle">
<div class="div-inner">
<div class="svg"></div>
</div>
</div>
</div>
</div>
</section>
<section id="s19">
<div style="position: relative;">
<div class="div-outer">
<div class="div-middle">
<div class="div-inner">
<h3 class="h3-font">3. Until three angles remain (one triangle), select the triangle with the smallest angle and remove it (store it), updating the remaining angles affected.</h3>
</div>
</div>
</div>
</div>
</section>
<section id="s20">
<div style="position: relative;">
<div class="div-outer">
<div class="div-middle">
<div class="div-inner">
<div class="svg"></div>
</div>
</div>
</div>
</div>
</section>
<section id="s21">
<div style="position: relative;">
<div class="div-outer">
<div class="div-middle">
<div class="div-inner">
<div class="svg"></div>
</div>
</div>
</div>
</div>
</section>
<section id="s22">
<div style="position: relative;">
<div class="div-outer">
<div class="div-middle">
<div class="div-inner">
<div class="svg"></div>
</div>
</div>
</div>
</div>
</section>
<section id="s23">
<div style="position: relative;">
<div class="div-outer">
<div class="div-middle">
<div class="div-inner">
<h3 class="h3-font"><a href="https://gist.github.com/isedgar/ab9fe581adc588355fad9d2b3dbf2fe5" target="_blank">JS code</a>:</h3>
</div>
</div>
</div>
</div>
</section>
<section id="s24">
<div style="position: relative;">
<div class="div-outer">
<div class="div-middle">
<div class="div-inner">
<h3 class="h3-font">
<pre class="code">
<code class="language-js">var app={
angle: function(a,b,c){ // angle at point b
var v1=[b[0] - a[0], b[1] - a[1]],
v2=[b[0] - c[0], b[1] - c[1]];
return Math.acos(this.inner(v1, v2) / (this.norm(v1) * this.norm(v2)));
},
is_clockwise: function(a,b,c){
return ((b[0]-a[0]) * (c[1]-a[1]) - (c[0]-a[0]) * (b[1]-a[1])) < 0 ? true : false;
},
clamp_index: function(x,a,b){
x=x < a ? b : x;
x=x > b ? a : x;
return x;
},
norm: function(v){
var s=0, n=v.length;
for(var i=0; i < n; ++i){
s += Math.pow(v[i], 2);
}
return Math.sqrt(s);
},
inner: function(a,b){
var s=0;
for(var i=0; i < a.length; ++i){
s += (a[i]*b[i]);
}
return s;
},
triangulate: function(points){
var n=points.length, m, angles=[], triangles=[], poly_cw, ear_cw, a, b, c, theta, min_ang, i, max_x;
if(points[0][0] == points[n-1][0] && points[0][1] == points[n-1][1]){
n=n-1;
}
m=n;
max_x=points[0][0], i;
for(var k=1; k < n; ++k){
if(points[k][0] > max_x){
max_x=points[k][0];
i=k;
}
}
h=this.clamp_index(i-1,0,n-1);
j=this.clamp_index(i+1,0,n-1);
a=points[h]; b=points[i]; c=points[j];
poly_cw=this.is_clockwise(a,b,c);
for(var k=0; k < n; ++k){
a=points[k];
b=points[(k+1)%n];
c=points[(k+2)%n];
theta=this.angle(a,b,c);
ear_cw=this.is_clockwise(a,b,c);
if(ear_cw != poly_cw){
theta = 2*Math.PI - theta;
}
angles[(k+1)%n] = theta;
}
for(var k=0; k < m-2; ++k){
min_ang=Math.min(...angles);
i=angles.indexOf(min_ang);
h=this.clamp_index(i-1,0,n-1);
j=this.clamp_index(i+1,0,n-1);
triangles.push([points[h], points[i], points[j]]);
//==================== UPDATE ANGLE k - 1 ====================
a=points[this.clamp_index(h-1,0,n-1)]; b=points[h]; c=points[j];
ear_cw=this.is_clockwise(a,b,c);
theta = this.angle(a,b,c);
if(ear_cw != poly_cw) theta=2*Math.PI - theta;
angles[h] = theta;
//============================================================
//==================== UPDATE ANGLE k + 1 ====================
a=points[h]; b=points[j]; c=points[this.clamp_index(j+1,0,n-1)];
ear_cw=this.is_clockwise(a,b,c);
theta = this.angle(a,b,c);
if(ear_cw != poly_cw) theta=2*Math.PI - theta;
angles[j] = theta;
//============================================================
points.splice(i,1);
angles.splice(i,1);
n--;
}
return triangles;
}
}
//==============================================
var points=[
[343,392], [475,103], [245,151], [193,323],
[91, 279], [51, 301], [25, 381], [80, 334],
[142,418], [325,480], [340,564], [468,597]
];
var triangles=app.triangulate(points);
console.log(triangles);
</code>
</pre>
</h3>
</div>
</div>
</div>
</div>
</section>
<section id="s25">
<div style="position: relative;">
<div class="div-outer">
<div class="div-middle">
<div class="div-inner">
<p class="p-font">Watch the video:</p>
<iframe width="560" height="315" src="https://www.youtube.com/embed/d9tytAQbpXM" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</div>
</div>
</div>
</div>
</section>
</div>
</div>
<script src="/assets/js/reveal.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@svgdotjs/svg.js@latest/dist/svg.min.js"></script>
<script src="/assets/js/reveal-plugins/math/math.js"></script>
<script src="/assets/js/common.js"></script>
<script src="/assets/js/page.js"></script>
<script src="/assets/js/polygon-triangulation.js"></script>
</body>
</html>