forked from kevin00036/ioi-lecture-geometry
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
1831 lines (1769 loc) · 86.9 KB
/
Copy pathindex.html
File metadata and controls
1831 lines (1769 loc) · 86.9 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
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
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>reveal.js - The HTML Presentation Framework</title>
<meta name="description" content="A framework for easily creating beautiful presentations using HTML">
<meta name="author" content="Hakim El Hattab">
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, minimal-ui">
<link rel="stylesheet" href="css/reveal.css">
<link rel="stylesheet" href="css/theme/serif.css" id="theme">
<!-- Code syntax highlighting -->
<link rel="stylesheet" href="lib/css/zenburn.css">
<link rel="stylesheet" href="css/style.css">
<link rel="stylesheet" type="text/css" href="//jsxgraph.uni-bayreuth.de/distrib/jsxgraph.css" />
<script type="text/javascript" src="//jsxgraph.uni-bayreuth.de/distrib/jsxgraphcore.js"></script>
<!-- Printing and PDF exports -->
<script>
var link = document.createElement( 'link' );
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = window.location.search.match( /print-pdf/gi ) ? 'css/print/pdf.css' : 'css/print/paper.css';
document.getElementsByTagName( 'head' )[0].appendChild( link );
</script>
<!--[if lt IE 9]>
<script src="lib/js/html5shiv.js"></script>
<![endif]-->
</head>
<body>
<div class="reveal">
<!-- Any section element inside of this container is displayed as a slide -->
<div class="slides">
\(
\newcommand{\ord}[1]{\opord\left(#1\right)}
\newcommand{\abs}[1]{\lvert #1 \rvert}
\newcommand{\floor}[1]{\lfloor #1 \rfloor}
\newcommand{\ceil}[1]{\lceil #1 \rceil}
\newcommand{\opord}{\operatorname{\mathcal{O}}}
\newcommand{\fail}{\operatorname{\mathcal{F}}}
\newcommand{\flk}{\operatorname{\mathfrak{F}}}
\newcommand{\suf}{\operatorname{\sigma}}
\newcommand{\rank}{\operatorname{\mathcal{R}}}
\newcommand{\sa}{\operatorname{\mathcal{SA}}}
\newcommand{\hei}{\operatorname{\mathcal{H}}}
\newcommand{\edps}{\operatorname{\mathcal{E}}}
\newcommand{\mx}{\operatorname{\mathcal{M}}}
\newcommand{\argmax}{\operatorname{arg\,max}}
\newcommand{\cons}[1]{\left[ \: #1 \: \right]}
\newcommand{\str}[1]{\texttt{"#1"}}
\newcommand{\vec}[1]{\overrightarrow{#1}}
\)
<section>
<h1> <small>Computational Geometry</small> <br> 計算幾何 </h1>
<h3> step5 </h3>
<h3> 2017/02/09 </h3>
Slides:
https://kevin00036.github.io/ioi-lecture-geometry/#/3
</section>
<!--<section>-->
<!--<section>-->
<!--<h3> WHO AMD I?</h3>-->
<!--<ul>-->
<!--<li class="fragment"> 黃凱祺 / STEP5 / kevin00050 / 衄 </li>-->
<!--<li class="fragment"> <b><font color="blue">bcw</font></b>0x1bd2 (<font color="blue">$\beta$</font>obogei / kai-<font color="blue">$\varsigma$</font>hi / hanhan<font color="blue">$\omega$</font>) </li>-->
<!--<li class="fragment"> $\alpha \beta \gamma \delta \epsilon \ \zeta \eta \theta \iota \ \kappa \lambda \mu \nu \xi \omicron \ \pi \rho \sigma \tau \upsilon \ \phi \chi \psi \omega$</li>-->
<!--</ul>-->
<!--</section>-->
<!--</section>-->
<section>
<section>
<h3> 計算幾何能吃嗎?</h3>
<ul>
<li class="fragment"> 不能 </li>
<li class="fragment"> 用程式來計算一些幾何問題 </li>
<li class="fragment"> IOI 通常不會出 </li>
<li class="fragment"> ACM 通常一定會出 </li>
<li class="fragment"> code 通常很難寫 </li>
<li class="fragment"> 200 行是正常現象(?) </li>
<li class="fragment"> 各種機八 Case </li>
<li class="fragment"> <font color="red">WA</font> 到你不想寫... </li>
</ul>
</section>
</section>
<section>
<section>
<h3> 解析幾何 </h3>
<p> 什麼?解析度很高的幾何嗎?</p>
</section>
<section>
<h3> 解析幾何 </h3>
<ul>
<li class="fragment"> 純幾何:長度、角度、形狀、全等、相似...</li>
<li class="fragment"> <b>解析</b>幾何:座標、向量、內積、外積、行列式...難算到要人命</li>
</ul>
<p class="fragment"> 電腦上只能做四則運算... </p>
<p class="fragment"> 還是用解析幾何吧!</p>
</section>
</section>
<section>
<section>
<h3> 點 </h3>
<p> 這輕鬆! 只需要兩個數字$(x, y)$</p>
<div id="box" class="jxgbox" style="width:300px; height:200px;"></div>
<script type="text/javascript">
{
var board = JXG.JSXGraph.initBoard('box', {boundingbox: [-5, 5, 10, -5], axis:true});
board.create('point', [5,3], {name:'(5, 3)', strokecolor:'red'});
}
</script>
</section>
<section>
<h3> 向量 </h3>
<p> 同樣的$(x, y)$也可以代表一個向量</p>
<p> 兩者有什麼關聯呢? </p>
<p class="fragment"><b>位置向量</b>:從原點指到$(x, y)$的向量</p>
<div id="box2" class="jxgbox" style="width:300px; height:200px;"></div>
<script type="text/javascript">
{
var board = JXG.JSXGraph.initBoard('box2', {boundingbox: [-5, 5, 10, -5], axis:true});
var pt = board.create('point', [5,3], {name:'(5, 3)', strokecolor:'red'});
var li3 = board.create('line', [[0,0],pt], {straightFirst:false, straightLast:false, strokeWidth:2, lastArrow:true});
}
</script>
</section>
<section>
<h3> 向量運算 </h3>
<ul>
<li class="fragment"> 加法 </li>
<li class="fragment"> 減法 </li>
<li class="fragment"> 內積 : $(x_1, y_1) \cdot (x_2, y_2) = x_1y_1+x_2y_2$ </li>
<li class="fragment"> 外積 : $(x_1, y_1) \times (x_2, y_2) = x_1y_2 - x_2y_1$ </li>
<li class="fragment"> 除法? A__A</li>
</ul>
</section>
<section>
<h3> 向量運算 </h3>
<pre><code data-trim class="cpp">
#define F first
#define S second
typedef pair<double, double> pdd;
pdd operator+ (const pdd &a, const pdd &b) { return pdd(a.F+b.F, a.S+b.S);}
pdd operator- (const pdd &a, const pdd &b) { return pdd(a.F-b.F, a.S-b.S);}
pdd operator* (const pdd &a, const double &b) { return pdd(a.F*b, a.S*b);}
pdd operator/ (const pdd &a, const double &b) { return pdd(a.F/b, a.S/b);}
double dot(const pdd &a, const pdd &b) { return a.F*b.F + a.S*b.S;}
double cross(const pdd &a, const pdd &b) { return a.F*b.S - a.S*b.F;}
double abs2(const pdd &a) { return dot(a, a);}
double abs(const pdd &a) { return sqrt(dot(a, a));}
</code></pre>
</section>
</section>
<section>
<section>
<h3> 直線 </h3>
<p> 點斜式!</p>
$$ y = m x + b $$
<p> $m$ : 斜率 </p>
<p> 垂直線? G___G</p>
</section>
<section>
<h3> 直線 </h3>
<p> 點向式</p>
$$ (x, y) = (x_0, y_0) + t \cdot (d_x, d_y), \ t \in \mathbb{R}$$
<p> $\vec{P_0} = (x_0, y_0)$ : 直線上任意一點 </p>
<p> $\vec{d} = (d_x, d_y)$ : <b>方向</b>向量 </p>
<div id="box3" class="jxgbox" style="width:300px; height:200px;"></div>
<script type="text/javascript">
{
var board = JXG.JSXGraph.initBoard('box3', {boundingbox: [-5, 5, 10, -5], axis:true});
var A = [1, 1];
var B = [8, 3];
board.create('point', A, {name:'P0', strokecolor:'red'});
board.create('line',[A,B], {strokeColor:'#00ff00',strokeWidth:2});
board.create('line', [A,B], {straightFirst:false, straightLast:false, strokeWidth:2, lastArrow:true, withLabel:true, name:'d', label:{position:'top', display: 'internal'}});
}
</script>
</section>
<section>
<h3> 線段 </h3>
<p> 線段 $\overline{P_1P_2}$ </p>
$$ (x, y) = P_1 + t \cdot (P_2 - P_1), \ \color{red}{0 \le t \le 1}$$
<p> $\vec{d} = P_2 - P_1$ : <b>方向</b>向量 </p>
<div id="box4" class="jxgbox" style="width:300px; height:200px;"></div>
<script type="text/javascript">
{
var board = JXG.JSXGraph.initBoard('box4', {boundingbox: [-5, 5, 10, -5], axis:true});
var A = [1, 1];
var B = [8, 3];
board.create('point', A, {name:'P1', strokecolor:'red'});
board.create('point', B, {name:'P2', strokecolor:'red'});
board.create('segment', [A,B], {strokeWidth:2, lastArrow:true, withLabel:true, name:'d', label:{position:'top', display: 'internal'}});
}
</script>
</section>
<section>
<h3> 多邊形 </h3>
<p> $N$ 個頂點,兩兩連線繞成一圈</p>
<p> $P_1, P_2, P_3, \cdots, P_N$</p>
<p> $\overline{P_1P_2}, \overline{P_2P_3}, \cdots, \overline{P_NP_1}$ </p>
<div id="box5" class="jxgbox" style="width:300px; height:200px;"></div>
<script type="text/javascript">
{
var board = JXG.JSXGraph.initBoard('box5', {boundingbox: [-5, 5, 10, -5]});
var poly = [[0, 4], [3, 2], [3, -1], [2, 0], [-2, -2], [-3, 1]];
board.create('polygon', poly);
for(i=0; i<6; i++)
{
board.create('point', poly[i], {name:'P'+(6-i), strokecolor:'red'});
}
}
</script>
</section>
</section>
<section>
<section>
<h3> 有向直線 </h3>
$$ (x, y) = P_0 + t \cdot \color{green}{\vec{d}}, \ t \in \mathbb{R}$$
<div id="box6" class="jxgbox" style="width:300px; height:200px;"></div>
<script type="text/javascript">
{
var board = JXG.JSXGraph.initBoard('box6', {boundingbox: [-5, 5, 10, -5]});
var A = [1, 1];
var B = [8, 3];
board.create('point', A, {name:'P0', strokecolor:'red'});
board.create('line',[A,B], {strokeColor:'#00ff00',strokeWidth:2});
board.create('line', [A,B], {straightFirst:false, straightLast:false, strokeWidth:2, lastArrow:true, withLabel:true, name:'d', label:{position:'top', display: 'internal'}});
}
</script>
</section>
<section>
<h3> 有向直線 </h3>
<p> 直線<font color="blue">左</font>邊(<font color="blue">正</font>方向) :
$\color{green}{\vec{d}} \times \vec{P_0P} \color{blue}{>} 0$</p>
<p> 直線<font color="red">右</font>邊(<font color="red">負</font>方向) :
$\color{green}{\vec{d}} \times \vec{P_0P} \color{red}{<} 0$</p>
<div id="box7" class="jxgbox" style="width:300px; height:200px;"></div>
<script type="text/javascript">
{
var board = JXG.JSXGraph.initBoard('box7', {boundingbox: [-5, 5, 10, -5]});
var A = [1, 1];
var B = [8, 3];
var C = [1, 4];
var D = [6, -2];
board.create('point', A, {name:'P0', strokecolor:'red'});
board.create('point', C, {name:'P', strokecolor:'red'});
board.create('point', D, {name:'P', strokecolor:'red'});
board.create('line',[A,B], {strokeColor:'#00ff00',strokeWidth:2});
var l = board.create('segment', [A,B], {lastArrow:true, withLabel:true, name:'d', label:{position:'top', display: 'internal'}, strokeColor:'green'});
var l2 = board.create('segment', [A,C], {lastArrow:true});
var l3 = board.create('segment', [A,D], {lastArrow:true, strokeColor:'red'});
ineq = board.create('inequality', [l]);
ineq = board.create('inequality', [l], {inverse: true, fillColor: 'blue'});
}
</script>
</section>
<section>
<h3> 有向角度 </h3>
<p> 與轉的方向有關!</p>
<p> 逆時針(往<font color="blue">左</font>邊轉) : <font color="blue">正</font>角度 </p>
<p> 順時針(往<font color="red">右</font>邊轉) : <font color="red">負</font>角度 </p>
$$ \vec{BA} \times \vec{BC} = |BA||BC| \sin(\angle ABC)$$
$$ \vec{BA} \cdot \vec{BC} = |BA||BC| \cos(\angle ABC)$$
<div id="box8" class="jxgbox" style="width:300px; height:200px;"></div>
<script type="text/javascript">
{
var board = JXG.JSXGraph.initBoard('box8', {boundingbox: [-5, 5, 10, -5]});
var A = [1, 1];
var B = [8, 3];
var C = [1, 4];
var D = [6, -2];
board.create('point', A, {name:'B', strokecolor:'red'});
board.create('point', B, {name:'A', strokecolor:'red'});
board.create('point', C, {name:'C', strokecolor:'red'});
board.create('point', D, {name:'C', strokecolor:'red'});
var l = board.create('segment', [A,B], {lastArrow:true, strokeColor:'green'});
var l2 = board.create('segment', [A,C], {lastArrow:true});
var l3 = board.create('segment', [A,D], {lastArrow:true, strokeColor:'red'});
var a1 = board.create('angle', [B,A,C], {strokeColor:'blue', fillColor:'blue', radius:1, withLabel:false});
var a2 = board.create('angle', [D,A,B], {strokeColor:'red', fillColor:'red', radius:1, withLabel:false});
}
</script>
</section>
<section>
<h3> 判斷方向 </h3>
$$\mathrm{ori}(P_1, P_2, P_3) =
\mathrm{sign}(\vec{P_1P_2} \times \vec{P_1P_3})$$
<p>
<ul>
<li class="fragment"> $1$ : $P_3$ 在 $\vec{P_1P_2}$ <font color="blue">左</font>邊</li>
<li class="fragment"> $-1$ : $P_3$ 在 $\vec{P_1P_2}$ <font color="red">右</font>邊</li>
<li class="fragment"> $0$ : $P_1, P_2, P_3$ 三點共線</li>
</ul>
</p>
<div id="box9" class="jxgbox" style="width:300px; height:200px;"></div>
<script type="text/javascript">
{
var board = JXG.JSXGraph.initBoard('box9', {boundingbox: [-5, 5, 10, -5]});
var A = [1, 1];
var B = [8, 3];
var C = [1, 4];
var D = [6, -2];
board.create('point', A, {name:'P1', strokecolor:'red'});
board.create('point', B, {name:'P2', strokecolor:'red'});
board.create('point', C, {name:'P3', strokecolor:'red'});
board.create('point', D, {name:'P3', strokecolor:'red'});
var l = board.create('segment', [A,B], {lastArrow:true, strokeColor:'green'});
var l2 = board.create('segment', [A,C], {lastArrow:true});
var l3 = board.create('segment', [A,D], {lastArrow:true, strokeColor:'red'});
var a1 = board.create('angle', [B,A,C], {strokeColor:'blue', fillColor:'blue', radius:1, withLabel:false});
var a2 = board.create('angle', [D,A,B], {strokeColor:'red', fillColor:'red', radius:1, withLabel:false});
}
</script>
</section>
<section>
<h3> 有向面積 </h3>
<p> 三角形的<b>有向</b>面積</p>
$$ \Delta ABC = \frac{1}{2} \vec{BA} \times \vec{BC}$$
<div id="box10" class="jxgbox" style="width:300px; height:200px;"></div>
<script type="text/javascript">
{
var board = JXG.JSXGraph.initBoard('box10', {boundingbox: [-5, 5, 10, -5]});
var A = [1, -3];
var B = [8, 3];
var C = [1, 4];
board.create('point', A, {name:'B', strokecolor:'red'});
board.create('point', B, {name:'A', strokecolor:'red'});
board.create('point', C, {name:'C', strokecolor:'red'});
var l = board.create('segment', [A,B], {lastArrow:true});
var l2 = board.create('segment', [A,C], {lastArrow:true});
var l3 = board.create('segment', [B,C], {});
var poly = board.create('polygon', [A,B,C], {fillColor:'blue'});
var t = board.create('text', [3, 3, '+'], {fontSize: 36});
}
</script>
</section>
<section>
<h3> 有向面積 </h3>
<p> <b>凸多邊形</b>的有向面積</p>
<p> 切割成$N$個三角形!</p>
$$ \mathrm{Area}(P_1P_2\cdots P_N) = \Delta P_1AP_2 + \Delta P_2AP_3
+ \cdots + \Delta P_NAP_1$$
<div id="box11" class="jxgbox" style="width:300px; height:300px;"></div>
<script type="text/javascript">
{
var board = JXG.JSXGraph.initBoard('box11', {boundingbox: [-5, 5, 5, -5]});
var A = [0, 0];
var poly = [[3, 0], [2, 3], [-1, 3], [-3, -1], [0, -2]];
board.create('point', A, {name:'A'});
board.create('polygon', poly);
for(i=0; i<5; i++)
{
board.create('point', poly[i], {name:'P'+(i+1)});
board.create('segment', [poly[i],poly[(i+1)%5]], {lastArrow:true});
board.create('segment', [A, poly[i]], {lastArrow:true});
}
}
</script>
</section>
<section>
<h3> 有向面積 </h3>
<p> <b>凹</b>多邊形的有向面積</p>
<p> 照切!</p>
<p> <b>有向</b>面積的好處 : 正負會抵消掉</p>
<div id="box12" class="jxgbox" style="width:300px; height:300px;"></div>
<script type="text/javascript">
{
var board = JXG.JSXGraph.initBoard('box12', {boundingbox: [-5, 5, 5, -5]});
var A = [0, 0];
var poly = [[3, 0], [2, 3], [-1, 3], [-3, -1], [0, -2]];
board.create('point', A, {name:'A'});
board.create('polygon', poly);
for(i=0; i<5; i++)
{
board.create('point', poly[i], {name:'P'+(i+1)});
board.create('segment', [poly[i],poly[(i+1)%5]], {lastArrow:true});
board.create('segment', [A, poly[i]], {lastArrow:true});
}
}
</script>
</section>
<section>
<h3> 有向面積 </h3>
<p> 任意多邊形的有向面積</p>
<p> 乾脆把中心點$A$選成原點,那麼$\vec{AP_i}$就變成$P_i$位置向量</p>
$$ \mathrm{Area}(P_1P_2\cdots P_N) = \sum_{i=1}^{N} \Delta P_iOP_{i+1}
= \frac{1}{2} \sum_{i=1}^{N} P_i \times P_{i+1}$$
<p>(定義$P_{N+1} = P_1$)</p>
<p>時間複雜度 : $O(N)$</p>
</section>
<section>
<p> 真的嗎...^_^;;</p>
<div id="box13" class="jxgbox" style="width:300px; height:300px;"></div>
<script type="text/javascript">
{
var board = JXG.JSXGraph.initBoard('box13', {boundingbox: [-5, 5, 5, -5]});
var poly2 = [];
for(i=0; i<20; i++)
{
var x = Math.random()*9-4.5;
var y = Math.random()*9-4.5;
poly2.push([x, y]);
}
board.create('polygon', poly2, {fillColor: 'yellow'});
}
</script>
</section>
<section>
<h3> 簡單多邊形 </h3>
<ul>
<li class="fragment"> 邊不相交 </li>
</ul>
<br>
<div id="box14" class="jxgbox" style="width:300px; height:300px;"></div>
<script type="text/javascript">
{
var board = JXG.JSXGraph.initBoard('box14', {boundingbox: [-5, 5, 5, -5]});
var poly = [[3, 0], [0, 3], [1, 1], [-1, 3], [-1, -2], [0, 1], [1, -4], [1, 0]];
board.create('polygon', poly);
}
</script>
</section>
<section>
<h3> 有向面積 </h3>
<p> <b>簡單多邊形</b>的有向面積</p>
$$ \mathrm{Area}(P_1P_2\cdots P_N) = \sum_{i=1}^{N} \Delta P_iOP_{i+1}
= \frac{1}{2} \sum_{i=1}^{N} P_i \times P_{i+1}$$
<p>(定義$P_{N+1} = P_1$)</p>
<p>時間複雜度 : $O(N)$</p>
</section>
</section>
<section>
<section>
<h3> 線段相交 </h3>
<div class="ques">
給定兩條線段 $\overline{P_1P_2}$ 和 $\overline{P_3P_4}$,請問他們有交點嗎?
</div>
<div id="box15" class="jxgbox" style="width:300px; height:300px;"></div>
<script type="text/javascript">
{
var board = JXG.JSXGraph.initBoard('box15', {boundingbox: [-5, 5, 5, -5]});
var P1 = board.create('point', [-2, -1], {name: 'P1'});
var P2 = board.create('point', [2, 3], {name: 'P2'});
var P3 = board.create('point', [-3, 2], {name: 'P3'});
var P4 = board.create('point', [2, -2], {name: 'P4'});
board.create('segment', [P1, P2]);
board.create('segment', [P3, P4]);
}
</script>
</section>
<section>
<h3> 直線相交 </h3>
先考慮這個問題:
<div class="ques">
給定兩條<b>直線</b> $\overline{P_1P_2}$ 和 $\overline{P_3P_4}$,請問他們有交點嗎?
</div>
<div id="box16" class="jxgbox" style="width:300px; height:300px;"></div>
<script type="text/javascript">
{
var board = JXG.JSXGraph.initBoard('box16', {boundingbox: [-5, 5, 5, -5]});
var P1 = board.create('point', [-2, -1], {name: 'P1'});
var P2 = board.create('point', [2, 3], {name: 'P2'});
var P3 = board.create('point', [-3, 2], {name: 'P3'});
var P4 = board.create('point', [2, -2], {name: 'P4'});
board.create('line', [P1, P2], {strokeColor:'lightgreen'});
board.create('line', [P3, P4], {strokeColor:'lightgreen'});
board.create('segment', [P1, P2]);
board.create('segment', [P3, P4]);
}
</script>
</section>
<section>
<h3> 直線相交 </h3>
<p> 好像還蠻簡單的... </p>
<ul>
<li class="fragment"> 兩直線不平行 : 肯定有交點! </li>
<li class="fragment"> 兩直線平行 : 不相交 or 完全重合</li>
</ul>
</section>
<section>
<h3> 直線求交點 </h3>
<ul style="line-height: 150%">
<li class="fragment"> $ \vec{d_1} = \vec{P_1P_2}, \vec{d_2} = \vec{P_3P_4}$ </li>
<li class="fragment"> 交點 $ x = P_1 + t\vec{d_1} = P_3 + s\vec{d_2}$ </li>
<li class="fragment"> $ t\vec{d_1} = \vec{P_1P_3} + s\vec{d_2}$ </li>
<li class="fragment"> $ t\vec{d_1} \times \vec{d_2} = \vec{P_1P_3} \times \vec{d_2}$ </li>
<li class="fragment"> $\displaystyle t = \frac{\vec{P_1P_3} \times \vec{d_2}}{\vec{d_1} \times \vec{d_2}}$ </li>
<br>
<li class="fragment"> $\vec{d_1} \times \vec{d_2} \neq 0$ (為什麼?)</li>
</ul>
<div id="box16.5" class="jxgbox" style="width:300px; height:300px;"></div>
<script type="text/javascript">
{
var board = JXG.JSXGraph.initBoard('box16.5', {boundingbox: [-5, 5, 5, -5]});
var P1 = board.create('point', [-2, -1], {name: 'P1'});
var P2 = board.create('point', [2, 3], {name: 'P2'});
var P3 = board.create('point', [-3, 2], {name: 'P3'});
var P4 = board.create('point', [2, -2], {name: 'P4'});
var ln1 = board.create('segment', [P1, P2], {lastArrow:true, withLabel:true, label:{position:'top'}, name:'d1'});
var ln2 = board.create('segment', [P3, P4], {lastArrow:true, withLabel:true, label:{position:'top'}, name:'d2'});
var Pp = board.create('intersection', [ln1, ln2], {name:'x', strokeColor:'green'});
}
</script>
</section>
<section>
<h3> 線段求交點 </h3>
<ul>
<li class="fragment"> 類似的方法可以求出 $s$ </li>
<li class="fragment"> $0 \le t \le 1$ and $0 \le s \le 1$ : 兩<b>線段</b>有交點!</li>
</ul>
</section>
<section>
<h3> 另一種方法 </h3>
<ul>
<li class="fragment"> 如果 $\overline{P_1P_2}$ 和 $\overline{P_3P_4}$ 有交點的話 ... </li>
<li class="fragment"> $P_3$ 和 $P_4$ 在 $\vec{P_1P_2}$ 的<b>異側</b> </li>
<li class="fragment"> $P_1$ 和 $P_2$ 在 $\vec{P_3P_4}$ 的<b>異側</b> </li>
<li class="fragment"> $\mathrm{ori}(P_1, P_2, P_3) \cdot \mathrm{ori}(P_1, P_2, P_4) < 0$ </li>
<li class="fragment"> $\mathrm{ori}(P_3, P_4, P_1) \cdot \mathrm{ori}(P_3, P_4, P_2) < 0$ </li>
</ul>
<div id="box17" class="jxgbox" style="width:300px; height:300px;"></div>
<script type="text/javascript">
{
var board = JXG.JSXGraph.initBoard('box17', {boundingbox: [-5, 5, 5, -5]});
var P1 = board.create('point', [-2, -1], {name: 'P1'});
var P2 = board.create('point', [2, 3], {name: 'P2'});
var P3 = board.create('point', [-3, 2], {name: 'P3'});
var P4 = board.create('point', [2, -2], {name: 'P4'});
board.create('segment', [P1, P2]);
board.create('segment', [P3, P4]);
}
</script>
</section>
<section>
<h3> 慘了了 </h3>
<div id="box18" class="jxgbox" style="width:300px; height:300px;"></div>
<script type="text/javascript">
{
var board = JXG.JSXGraph.initBoard('box18', {boundingbox: [-5, 5, 5, -5]});
var P1 = board.create('point', [-2, -1], {name: 'P1'});
var P2 = board.create('point', [2, 3], {name: 'P2'});
var P3 = board.create('point', [-1, 0], {name: 'P3'});
var P4 = board.create('point', [2, -2], {name: 'P4'});
board.create('segment', [P1, P2]);
board.create('segment', [P3, P4]);
}
</script>
</section>
<section>
<h3> 線段相交 </h3>
<ul>
<li class="fragment"> 兩線段無交點 $\Leftrightarrow P_3, P_4$ 在 $\vec{P_1P_2}$ 同側
<font color="red">或</font> $P_1, P_2$ 在 $\vec{P_3P_4}$ 同側</li>
<li class="fragment"> 萬一兩線段平行呢?</li>
<li class="fragment"> 延伸的直線不重合 : 無交點</li>
<li class="fragment"> 延伸的直線重合 : 一維的判斷交點</li>
</ul>
<div id="box19" class="jxgbox" style="width:300px; height:300px;"></div>
<script type="text/javascript">
{
var board = JXG.JSXGraph.initBoard('box19', {boundingbox: [-5, 5, 5, -5]});
var P1 = board.create('point', [-2, 0], {name: 'P1'});
var P2 = board.create('point', [0, 0], {name: 'P2'});
var P3 = board.create('point', [1, 0], {name: 'P3'});
var P4 = board.create('point', [4, 0], {name: 'P4'});
board.create('segment', [P1, P2]);
board.create('segment', [P3, P4]);
}
</script>
</section>
</section>
<section>
<section>
<h3> 多邊形內外判斷</h3>
<div class="ques">
給定一個<b>簡單多邊形</b>和一個點 $A$,請問 $A$ 在該多邊形的內部還是外部?
</div>
<div id="box20" class="jxgbox" style="width:300px; height:300px;"></div>
<script type="text/javascript">
{
var board = JXG.JSXGraph.initBoard('box20', {boundingbox: [-5, 5, 5, -5]});
var A = [-0.5, 2];
var poly = [[3, 0], [0, 3], [1, 1], [-1, 3], [-1, -2], [0, 1], [1, -4], [1, 0]];
board.create('point', A, {name:'A', strokeColor:'green'});
board.create('polygon', poly);
}
</script>
</section>
<section>
<h3> 多邊形內外判斷</h3>
<ul>
<li class="fragment"> 想辦法往外走,不能越過多邊形邊界,走得到外面就是外部 </li>
<li class="fragment"> 時間複雜度 : $O(?)$ </li>
</ul>
</section>
<section>
<h3> 多邊形內外判斷</h3>
<ul>
<li class="fragment"> 開外掛穿牆(?) </li>
<li class="fragment"> 往一個方向一直走 </li>
<li class="fragment"> 每穿過一次牆,內部變外部,外部變內部 </li>
<li class="fragment"> 走到無限遠處... 這下總是外部了吧! </li>
<li class="fragment"> 計算總共穿幾次牆 </li>
<li class="fragment"> 偶數次 : 原本的點在<b>外部</b> </li>
<li class="fragment"> 奇數次 : 原本的點在<b>內部</b> </li>
</ul>
<div id="box21" class="jxgbox" style="width:300px; height:300px;"></div>
<script type="text/javascript">
{
var board = JXG.JSXGraph.initBoard('box21', {boundingbox: [-5, 5, 5, -5]});
var A = [-0.5, 2];
var B = [6, 3];
var poly = [[3, 0], [0, 3], [1, 1], [-1, 3], [-1, -2], [0, 1], [1, -4], [1, 0]];
board.create('point', A, {name:'A', strokeColor:'green'});
board.create('polygon', poly);
board.create('segment', [A,B]);
}
</script>
</section>
<section>
<h3> 多邊形內外判斷</h3>
<ul>
<li class="fragment"> 要怎麼計算穿牆次數呢? </li>
<li class="fragment"> 線段相交! </li>
<li class="fragment"> 把多邊形的 $N$ 條邊都檢查一次</li>
<li class="fragment"> 時間複雜度 : $O(N)$</li>
</ul>
</section>
<section>
<h3> 慘了了</h3>
<ul>
<li class="fragment"> 萬一剛好交到邊、頂點上呢? </li>
<li class="fragment"> 可能會重複算到次數 </li>
<li class="fragment"> 隨機取一個方向射線,交到邊、頂點的機率很低 (為什麼?) </li>
<li class="fragment"> 座標都是整數(範圍 $W$ )的話,外面的點取 $(W, W+1)$,肯定不會交到邊點 </li>
</ul>
<div id="box22" class="jxgbox" style="width:300px; height:300px;"></div>
<script type="text/javascript">
{
var board = JXG.JSXGraph.initBoard('box22', {boundingbox: [-5, 5, 5, -5]});
var A = [-0.5, 2];
var B = [2, 7];
var poly = [[3, 0], [0, 3], [1, 1], [-1, 3], [-1, -2], [0, 1], [1, -4], [1, 0]];
board.create('point', A, {name:'A', strokeColor:'green'});
board.create('polygon', poly);
board.create('segment', [A,B]);
}
</script>
</section>
<section>
<h3> 多邊形內外判斷</h3>
<ul>
<li class="fragment"> 如果是<b>凸</b>多邊形呢? </li>
<li class="fragment"> 中間找一個點 $A$,連線到每個頂點 </li>
<li class="fragment"> 把一圈分成 $N$ 個角度區間 </li>
<li class="fragment"> 查詢時,先二分搜出在哪個區間,再檢查是否在三角形內</li>
<li class="fragment"> 時間複雜度 : 預處理 $O(N)$,查詢$O(\log N)$</li>
</ul>
<div id="box23" class="jxgbox" style="width:300px; height:300px;"></div>
<script type="text/javascript">
{
var board = JXG.JSXGraph.initBoard('box23', {boundingbox: [-5, 5, 5, -5]});
var A = [0, 0];
var poly = [[3, 0], [2, 3], [-1, 3], [-3, -1], [0, -2]];
board.create('point', A, {name:'A'});
board.create('point', [2,1], {name:'P'});
board.create('polygon', poly);
for(i=0; i<5; i++)
{
board.create('point', poly[i], {name:'P'+(i+1)});
board.create('segment', [A, poly[i]], {lastArrow:true});
}
board.create('segment', [A, 'P'], {lastArrow:true, strokeColor:'yellow'});
board.create('polygon', [A,poly[0],poly[1]], {fillColor:'blue'});
}
</script>
</section>
</section>
<section>
<section>
<h3> 凸包 </h3>
<div class="ques">
給定平面上一個點集,請找出包含這個點集的最小面積<b>凸</b>多邊形。
</div>
<div id="box24" class="jxgbox" style="width:300px; height:300px;"></div>
<script type="text/javascript">
{
var board = JXG.JSXGraph.initBoard('box24', {boundingbox: [-5, 5, 5, -5]});
var poly = [[3, 0], [1, 0], [1, 2], [-1, 2], [-1, 0], [-3, 0], [-3, -2], [3, -2]];
var poly2 = [[3, 0], [1, 2], [-1, 2], [-3, 0], [-3, -2], [3, -2]];
board.create('polygon', poly);
board.create('polygon', poly2, {fillColor: 'yellow'});
}
</script>
</section>
<section>
<h3> <b>"凸"</b>包 </h3>
<p class="fragment">字不要寫錯啊!</p>
<p class="fragment">不然就會變這樣...</p>
<img src="sanp15.png" class="fragment"/>
</section>
<section>
<h3> "凸"多邊形? </h3>
<ul>
<li class="fragment"> 內角皆小於 $180^\circ$ </li>
<li class="fragment"> 兩點連線包含在多邊形內 </li>
</ul>
<div id="box25" class="jxgbox" style="width:300px; height:300px;"></div>
<script type="text/javascript">
{
var board = JXG.JSXGraph.initBoard('box25', {boundingbox: [-5, 5, 5, -5]});
var poly2 = [[3, 0], [1, 2], [-1, 2], [-3, 0], [-3, -2], [3, -2]];
board.create('polygon', poly2, {fillColor: 'yellow'});
var A = board.create('point', [-2, -1]);
var B = board.create('point', [-1, 1]);
var l = board.create('segment', [A, B]);
}
</script>
</section>
<section>
<h3> 拿條橡皮筋綁起來! </h3>
<div style="float:right">
<ul>
<li class="fragment"> 先找一個必定在凸包上的點 <br>(最左下角的點一定是!)</li>
<li class="fragment"> 每次找最靠右邊的,加入凸包</li>
<li class="fragment"> 時間複雜度 : $O(N^2)$</li>
</ul>
</div>
<div id="box26" style="width:300px; height:300px; float:left">
<img src="https://upload.wikimedia.org/wikipedia/commons/b/bc/ConvexHull.png">
</div>
</section>
<section>
<h3> Monotone Chain </h3>
<ul>
<li class="fragment"> 每次都重新排序,太辛苦了!</li>
<li class="fragment"> 一次從左到右排序好,一個一個點慢慢加</li>
<li class="fragment"> 每多加一個點時,檢查看會不會影響凸包</li>
</ul>
</section>
<section>
<h3> Monotone Chain </h3>
<p> 檢查新增的點與目前凸包最後2個點的夾角:</p>
<ul>
<li class="fragment"> $< 180^\circ$ : 加入凸包</li>
<li class="fragment"> $\ge 180^\circ$ : 中間那個點刪掉</li>
<li class="fragment"> 用外積判斷夾角</li>
<li class="fragment"> 凸包根本沒有2個點? 那直接加入</li>
</ul>
<div id="box27" class="jxgbox" style="width:300px; height:300px;"></div>
<script type="text/javascript">
{
var board = JXG.JSXGraph.initBoard('box27', {boundingbox: [-5, 5, 5, -5]});
var A = board.create('point', [-4, 1], {name:'A'});
var B = board.create('point', [-3, 0], {name:'B'});
var C = board.create('point', [-1, 1], {name:'C'});
board.create('segment', [A,B]);
board.create('segment', [B,C], {dash:true});
var D = board.create('point', [0, -1], {name:'A'});
var E = board.create('point', [1, 0], {name:'B'});
var F = board.create('point', [3, -2], {name:'C'});
board.create('segment', [D,E]);
board.create('segment', [E,F], {dash:true});
board.create('line', [[-0.5,-6],[-0.5,6]], {dash:true, strokeColor:'lightgreen'});
}
</script>
</section>
<section>
<h3> Monotone Chain </h3>
<img src="http://www.csie.ntnu.edu.tw/~u91029/Andrew'sMonotoneChain1.png">
</section>
<section>
<h3> Monotone Chain </h3>
<ul>
<li class="fragment"> 用 stack 維護目前凸包 </li>
<li class="fragment"> 每個點會被加入 1 次</li>
<li class="fragment"> 每個點<b>最多</b>被刪除 1 次</li>
<li class="fragment"> 時間複雜度 : $O(N \log N) + O(N)$</li>
<li class="fragment"> 這樣找到的是<b>下</b>凸包</li>
<li class="fragment"> 從右到左排序再做一次,接起來才是完整的凸包</li>
</ul>
</section>
<section>
<h3> 加權平均 </h3>
<ul style:"float:right">
<li class="fragment"> 兩個點 $P_1, P_2$ 的 <b>加權平均</b> 長怎樣?</li>
<li class="fragment"> $P = \color{red}t \cdot P_1 + \color{red}{(1-t)} \cdot P_2, \color{red}{0 \le t \le 1}$</li>
<li class="fragment"> 落在線段 $P_1 P_2$ !</li>
<li class="fragment"> 三個點 $P_1, P_2$ 的 <b>加權平均</b> 長怎樣?</li>
<li class="fragment"> $P = \color{green}{\alpha} \cdot P_1 + \color{blue}{\beta} \cdot P_2 + \color{purple}{\gamma}\cdot P_3$<br>
$\color{green}{\alpha}, \color{blue}{\beta}, \color{purple}{\gamma} \ge 0, \color{green}{\alpha} + \color{blue}{\beta} + \color{purple}{\gamma} = 1$</li>
<li class="fragment"> 落在三角形 $\Delta P_1 P_2 P_3$ !</li>
</ul>
<div id="box101" class="jxgbox" style="width:300px; height:300px; float:left"></div>
<script type="text/javascript">
{
var board = JXG.JSXGraph.initBoard('box101', {boundingbox: [-5, 5, 5, -5]});
var P1 = board.create('point', [-2, 3], {name: 'P1'});
var P2 = board.create('point', [2, 3], {name: 'P2'});
var P3 = board.create('point', [-2, 0], {name: 'P1'});
var P4 = board.create('point', [2, 0], {name: 'P2'});
var P5 = board.create('point', [1, -3], {name: 'P3'});
board.create('segment', [P1, P2]);
board.create('polygon', [P3, P4, P5]);
}
</script>
</section>
<section>
<h3> 加權平均 </h3>
<ul style:"float:right">
<li class="fragment"> $N$ 個點 $P_1, P_2$ 的 <b>加權平均</b> 長怎樣?</li>
<li class="fragment"> $P = \sum_{i=1}^N \color{green}{\alpha_i} \cdot P_i$ <br>
$\color{green}{\alpha_i} \ge 0, \sum_{i=1}^N \color{green}{\alpha_i} = 1$</li>
<li class="fragment"> 落在 $P_1 \sim P_N$ 的<b>凸包</b> !</li>
<li class="fragment"> 事實上凸包有一個等價定義:<br>$N$ 個點的所有加權平均形成的集合</li>
<li class="fragment"> 這種加權平均又稱為 <br> <b>凸組合 (Convex combination)</b></li>
</ul>
<div id="box102" class="jxgbox" style="width:300px; height:300px; float:left"></div>
<script type="text/javascript">
{
var board = JXG.JSXGraph.initBoard('box102', {boundingbox: [-5, 5, 5, -5]});
var P1 = board.create('point', [-2, 3], {name:'P1'});
var P2 = board.create('point', [4, 0], {name:'P2'});
var P3 = board.create('point', [3, -1], {name:'P3'});
var P4 = board.create('point', [-1, 4], {name:'P4'});
var P5 = board.create('point', [1, 2], {name:'P5'});
var P6 = board.create('point', [1, 1], {name:'P6'});
var P7 = board.create('point', [-2, -2], {name:'P7'});
board.create('polygon', [P1, P7, P3, P2, P4]);
}
</script>
</section>
<section>
<h3> 另一種定義 </h3>
<ul style:"float:right">
<li class="fragment"> 如此一來可以定義更高維度的凸包!<br>
$P = \sum_{i=1}^N \color{green}{\alpha_i} \cdot P_i$ <br>
$\color{green}{\alpha_i} \ge 0, \sum_{i=1}^N \color{green}{\alpha_i} = 1$</li>
<li class="fragment"> 回過頭來看剛才說過的<b>凸多邊形</b>條件:<br>
<i>任兩點連線段,還在多邊形內部</i><br>
可以推廣成:<br><b>任取一些點的凸組合(加權平均)都還在內部</b>
<br> 或者說 <br>
<b>凸包還是自己!!</b>
</li>
<li class="fragment">凸多邊形的很多好性質可以從這裡推導!</li>
</ul>
<div id="box103" class="jxgbox" style="width:300px; height:300px; float:left">
<img src="https://i.stack.imgur.com/PFkhV.png">
</div>
</section>
</section>
<section>
<section>
<h3> 掃描線 </h3>
<p>拿一條垂直線,從左掃到右</p>
<p class="fragment"> 實作上就只是把點照 $x$ 座標排序 </p>
<div id="box29" class="jxgbox" style="width:300px; height:300px;"></div>
<script type="text/javascript">
{
var board = JXG.JSXGraph.initBoard('box29', {boundingbox: [-5, 5, 5, -5]});
var poly2 = [];
for(i=0; i<20; i++)
{
var x = Math.random()*9-4.5;
var y = Math.random()*9-4.5;
poly2.push([x, y]);
board.create('point', poly2[i], {withLabel:false});
}
var P = board.create('point', [-5,-6]);
var Q = board.create('point', [-5,6]);
var li = board.create('line', [P, Q]);
var f = function(){
P.moveTo([5, -6], 2000);
Q.moveTo([5, 6], 2000);
P.moveTo([-5, -6], 1);
Q.moveTo([-5, 6], 1);
setTimeout(f, 3000);
};
f();
}
</script>
</section>
<section>
<h3> 矩形覆蓋次數 </h3>
<div class="prob">
給定平面上 $N$ 個矩形和 $M$ 個點,問每個點被包含在<b>幾個</b>矩形中?
</div>
<div id="box30" class="jxgbox" style="width:300px; height:300px;"></div>
<script type="text/javascript">
{
var board = JXG.JSXGraph.initBoard('box30', {boundingbox: [-5, 5, 5, -5]});
var poly2 = [];
for(i=0; i<10; i++)
{
var x = Math.random()*9-4.5;
var y = Math.random()*9-4.5;
poly2.push([x, y]);
board.create('point', poly2[i], {withLabel:false});
}
for(i=0;i<5;i++)
{
var x1 = Math.random()*9-4.5;
var y1 = Math.random()*9-4.5;
var x2 = Math.random()*9-4.5;
var y2 = Math.random()*9-4.5;
board.create('polygon', [[x1,y1],[x1,y2],[x2,y2],[x2,y1]], {vertices:{visible:false}});
}
}
</script>
</section>
<section>
<h3> 矩形覆蓋次數 </h3>
<p>從左掃到右,會經歷一些<b>事件</b></p>
<ul>
<li class="fragment"> 矩形的左邊界</li>
<li class="fragment"> 矩形的右邊界</li>
<li class="fragment"> 查詢點</li>
</ul>
<p class="fragment">兩個事件之間,什麼都不會發生!</p>
<div id="box31" class="jxgbox" style="width:300px; height:300px;"></div>
<script type="text/javascript">
{
var board = JXG.JSXGraph.initBoard('box31', {boundingbox: [-5, 5, 5, -5]});
var poly2 = [];
var xco = [];
for(i=0; i<10; i++)
{
var x = Math.random()*9-4.5;
var y = Math.random()*9-4.5;
poly2.push([x, y]);
xco.push(x);
board.create('point', poly2[i], {withLabel:false});
}
for(i=0;i<5;i++)
{
var x1 = Math.random()*9-4.5;
var y1 = Math.random()*9-4.5;
var x2 = Math.random()*9-4.5;
var y2 = Math.random()*9-4.5;
xco.push(x1);
xco.push(x2);
board.create('polygon', [[x1,y1],[x1,y2],[x2,y2],[x2,y1]], {vertices:{visible:false}});
}
xco.sort(function(x, y){return x-y;});
console.log(xco);
sz = xco.length;
var pos = sz-1;
var AA = board.create('point', [-5, -6]);
var AB = board.create('point', [-5, 6]);
var li = board.create('line', [AA, AB]);
setInterval(function(){
pos = (pos + 1) % sz;
AA.moveTo([xco[pos], -6]);
AB.moveTo([xco[pos], 6]);
}, 500);
}
</script>
</section>
<section>
<h3> 矩形覆蓋次數 </h3>
<ul>
<li class="fragment"> 掃描線 : 1 維 </li>
<li class="fragment"> 維護每個 $y$ 座標被多少矩形覆蓋</li>
<li class="fragment"> 線段樹! </li>
<li class="fragment"> 左邊界 : $y$ 區間 +1 </li>
<li class="fragment"> 右邊界 : $y$ 區間 -1 </li>