-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcp.js
More file actions
4288 lines (4288 loc) · 188 KB
/
cp.js
File metadata and controls
4288 lines (4288 loc) · 188 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
/* origami crease pattern library by Robby Kraft
* MIT open source software license
* github.com/robbykraft/Origami
* http://robbykraft.com
*/
"use strict";
var __extends = (this && this.__extends) || (function () {
var extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var EPSILON_LOW = 0.003;
var EPSILON = 0.00001;
var EPSILON_HIGH = 0.00000001;
var EPSILON_UI = 0.05;
var Tree = (function () {
function Tree(thisObject) {
this.obj = thisObject;
this.parent = undefined;
this.children = [];
}
return Tree;
}());
function isValidPoint(point) { return (point !== undefined && !isNaN(point.x) && !isNaN(point.y)); }
function isValidNumber(n) { return (n !== undefined && !isNaN(n) && !isNaN(n)); }
function pointsSimilar(a, b, epsilon) {
if (epsilon == undefined) {
epsilon = EPSILON_HIGH;
}
return epsilonEqual(a.x, b.x, epsilon) && epsilonEqual(a.y, b.y, epsilon);
}
function epsilonEqual(a, b, epsilon) {
if (epsilon === undefined) {
epsilon = EPSILON_HIGH;
}
return (Math.abs(a - b) < epsilon);
}
function wholeNumberify(num, epsilon) {
if (epsilon === undefined) {
epsilon = EPSILON_HIGH;
}
if (Math.abs(Math.round(num) - num) < epsilon) {
num = Math.round(num);
}
return num;
}
function cleanNumber(num, decimalPlaces) {
if (Math.floor(num) == num || decimalPlaces == undefined) {
return num;
}
return parseFloat(num.toFixed(decimalPlaces));
}
function clockwiseInteriorAngleRadians(a, b) {
while (a < 0) {
a += Math.PI * 2;
}
while (b < 0) {
b += Math.PI * 2;
}
var a_b = a - b;
if (a_b >= 0)
return a_b;
return Math.PI * 2 - (b - a);
}
function counterClockwiseInteriorAngleRadians(a, b) {
while (a < 0) {
a += Math.PI * 2;
}
while (b < 0) {
b += Math.PI * 2;
}
var b_a = b - a;
if (b_a >= 0)
return b_a;
return Math.PI * 2 - (a - b);
}
function clockwiseInteriorAngle(a, b) {
var dotProduct = b.x * a.x + b.y * a.y;
var determinant = b.x * a.y - b.y * a.x;
var angle = Math.atan2(determinant, dotProduct);
if (angle < 0) {
angle += Math.PI * 2;
}
return angle;
}
function counterClockwiseInteriorAngle(a, b) {
var dotProduct = a.x * b.x + a.y * b.y;
var determinant = a.x * b.y - a.y * b.x;
var angle = Math.atan2(determinant, dotProduct);
if (angle < 0) {
angle += Math.PI * 2;
}
return angle;
}
function interiorAngles(a, b) {
var interior1 = clockwiseInteriorAngle(a, b);
var interior2 = Math.PI * 2 - interior1;
if (interior1 < interior2)
return [interior1, interior2];
return [interior2, interior1];
}
function bisectVectors(a, b) {
a = a.normalize();
b = b.normalize();
return [(a.add(b)).normalize(),
new XY(-a.x + -b.x, -a.y + -b.y).normalize()];
}
function intersect_vec_func(aOrigin, aVec, bOrigin, bVec, compFunction, epsilon) {
function determinantXY(a, b) { return a.x * b.y - b.x * a.y; }
var denominator0 = determinantXY(aVec, bVec);
var denominator1 = -denominator0;
if (epsilonEqual(denominator0, 0, epsilon)) {
return undefined;
}
var numerator0 = determinantXY(bOrigin.subtract(aOrigin), bVec);
var numerator1 = determinantXY(aOrigin.subtract(bOrigin), aVec);
var t0 = numerator0 / denominator0;
var t1 = numerator1 / denominator1;
if (compFunction(t0, t1)) {
return aOrigin.add(aVec.scale(t0));
}
}
function intersectionLineLine(a, b, epsilon) {
if (epsilon === undefined) {
epsilon = EPSILON_HIGH;
}
return intersect_vec_func(new XY(a.point.x, a.point.y), new XY(a.direction.x, a.direction.y), new XY(b.point.x, b.point.y), new XY(b.direction.x, b.direction.y), function (t0, t1) { return true; }, epsilon);
}
function intersectionLineRay(line, ray, epsilon) {
if (epsilon === undefined) {
epsilon = EPSILON_HIGH;
}
return intersect_vec_func(new XY(line.point.x, line.point.y), new XY(line.direction.x, line.direction.y), new XY(ray.origin.x, ray.origin.y), new XY(ray.direction.x, ray.direction.y), function (t0, t1) { return t1 >= -epsilon; }, epsilon);
}
function intersectionLineEdge(line, edge, epsilon) {
if (epsilon === undefined) {
epsilon = EPSILON_HIGH;
}
return intersect_vec_func(new XY(line.point.x, line.point.y), new XY(line.direction.x, line.direction.y), new XY(edge.nodes[0].x, edge.nodes[0].y), new XY(edge.nodes[1].x - edge.nodes[0].x, edge.nodes[1].y - edge.nodes[0].y), function (t0, t1) { return t1 >= -epsilon && t1 <= 1 + epsilon; }, epsilon);
}
function intersectionRayRay(a, b, epsilon) {
if (epsilon === undefined) {
epsilon = EPSILON_HIGH;
}
return intersect_vec_func(new XY(a.origin.x, a.origin.y), new XY(a.direction.x, a.direction.y), new XY(b.origin.x, b.origin.y), new XY(b.direction.x, b.direction.y), function (t0, t1) { return t0 >= -epsilon && t1 >= -epsilon; }, epsilon);
}
function intersectionRayEdge(ray, edge, epsilon) {
if (epsilon === undefined) {
epsilon = EPSILON_HIGH;
}
return intersect_vec_func(new XY(ray.origin.x, ray.origin.y), new XY(ray.direction.x, ray.direction.y), new XY(edge.nodes[0].x, edge.nodes[0].y), new XY(edge.nodes[1].x - edge.nodes[0].x, edge.nodes[1].y - edge.nodes[0].y), function (t0, t1) { return t0 >= -epsilon && t1 >= -epsilon && t1 <= 1 + epsilon; }, epsilon);
}
function intersectionEdgeEdge(a, b, epsilon) {
if (epsilon === undefined) {
epsilon = EPSILON_HIGH;
}
return intersect_vec_func(new XY(a.nodes[0].x, a.nodes[0].y), new XY(a.nodes[1].x - a.nodes[0].x, a.nodes[1].y - a.nodes[0].y), new XY(b.nodes[0].x, b.nodes[0].y), new XY(b.nodes[1].x - b.nodes[0].x, b.nodes[1].y - b.nodes[0].y), function (t0, t1) { return t0 >= -epsilon && t0 <= 1 + epsilon && t1 >= -epsilon && t1 <= 1 + epsilon; }, epsilon);
}
function intersectionCircleLine(center, radius, p0, p1) {
var r_squared = Math.pow(radius, 2);
var x1 = p0.x - center.x;
var y1 = p0.y - center.y;
var x2 = p1.x - center.x;
var y2 = p1.y - center.y;
var dx = x2 - x1;
var dy = y2 - y1;
var dr_squared = dx * dx + dy * dy;
var D = x1 * y2 - x2 * y1;
function sgn(x) { if (x < 0) {
return -1;
} return 1; }
var x1 = (D * dy + sgn(dy) * dx * Math.sqrt(r_squared * dr_squared - (D * D))) / (dr_squared);
var x2 = (D * dy - sgn(dy) * dx * Math.sqrt(r_squared * dr_squared - (D * D))) / (dr_squared);
var y1 = (-D * dx + Math.abs(dy) * Math.sqrt(r_squared * dr_squared - (D * D))) / (dr_squared);
var y2 = (-D * dx - Math.abs(dy) * Math.sqrt(r_squared * dr_squared - (D * D))) / (dr_squared);
var intersections = [];
if (!isNaN(x1)) {
intersections.push(new XY(x1 + center.x, y1 + center.y));
}
if (!isNaN(x2)) {
intersections.push(new XY(x2 + center.x, y2 + center.y));
}
return intersections;
}
var Matrix = (function () {
function Matrix(a, b, c, d, tx, ty) {
this.a = (a !== undefined) ? a : 1;
this.b = (b !== undefined) ? b : 0;
this.c = (c !== undefined) ? c : 0;
this.d = (d !== undefined) ? d : 1;
this.tx = (tx !== undefined) ? tx : 0;
this.ty = (ty !== undefined) ? ty : 0;
}
Matrix.prototype.identity = function () { this.a = 1; this.b = 0; this.c = 0; this.d = 1; this.tx = 0; this.ty = 0; };
Matrix.prototype.mult = function (mat) {
var r = new Matrix();
r.a = this.a * mat.a + this.c * mat.b;
r.c = this.a * mat.c + this.c * mat.d;
r.tx = this.a * mat.tx + this.c * mat.ty + this.tx;
r.b = this.b * mat.a + this.d * mat.b;
r.d = this.b * mat.c + this.d * mat.d;
r.ty = this.b * mat.tx + this.d * mat.ty + this.ty;
return r;
};
Matrix.prototype.reflection = function (vector, offset) {
var angle = Math.atan2(vector.y, vector.x);
var cosAngle = Math.cos(angle);
var sinAngle = Math.sin(angle);
var _cosAngle = Math.cos(-angle);
var _sinAngle = Math.sin(-angle);
this.a = cosAngle * _cosAngle + sinAngle * _sinAngle;
this.b = cosAngle * -_sinAngle + sinAngle * _cosAngle;
this.c = sinAngle * _cosAngle + -cosAngle * _sinAngle;
this.d = sinAngle * -_sinAngle + -cosAngle * _cosAngle;
if (offset !== undefined) {
this.tx = offset.x + this.a * -offset.x + -offset.y * this.c;
this.ty = offset.y + this.b * -offset.x + -offset.y * this.d;
}
return this;
};
Matrix.prototype.rotation = function (angle, origin) {
this.a = Math.cos(angle);
this.c = -Math.sin(angle);
this.b = Math.sin(angle);
this.d = Math.cos(angle);
if (origin !== undefined) {
this.tx = origin.x;
this.ty = origin.y;
}
return this;
};
Matrix.prototype.copy = function () {
var m = new Matrix();
m.a = this.a;
m.c = this.c;
m.tx = this.tx;
m.b = this.b;
m.d = this.d;
m.ty = this.ty;
return m;
};
return Matrix;
}());
var XY = (function () {
function XY(x, y) {
this.x = x;
this.y = y;
}
XY.prototype.equivalent = function (point, epsilon) {
if (epsilon == undefined) {
epsilon = EPSILON_HIGH;
}
return (epsilonEqual(this.x, point.x, epsilon) && epsilonEqual(this.y, point.y, epsilon));
};
XY.prototype.normalize = function () { var m = this.magnitude(); return new XY(this.x / m, this.y / m); };
XY.prototype.dot = function (point) { return this.x * point.x + this.y * point.y; };
XY.prototype.cross = function (vector) { return this.x * vector.y - this.y * vector.x; };
XY.prototype.magnitude = function () { return Math.sqrt(this.x * this.x + this.y * this.y); };
XY.prototype.distanceTo = function (a) { return Math.sqrt(Math.pow(this.x - a.x, 2) + Math.pow(this.y - a.y, 2)); };
XY.prototype.transform = function (matrix) {
return new XY(this.x * matrix.a + this.y * matrix.c + matrix.tx, this.x * matrix.b + this.y * matrix.d + matrix.ty);
};
XY.prototype.translate = function (dx, dy) { return new XY(this.x + dx, this.y + dy); };
XY.prototype.rotate90 = function () { return new XY(-this.y, this.x); };
XY.prototype.rotate180 = function () { return new XY(-this.x, -this.y); };
XY.prototype.rotate270 = function () { return new XY(this.y, -this.x); };
XY.prototype.rotate = function (angle, origin) { return this.transform(new Matrix().rotation(angle, origin)); };
XY.prototype.lerp = function (point, pct) { var inv = 1.0 - pct; return new XY(this.x * pct + point.x * inv, this.y * pct + point.y * inv); };
XY.prototype.midpoint = function (other) { return new XY((this.x + other.x) * 0.5, (this.y + other.y) * 0.5); };
XY.prototype.reflect = function (line) {
var origin = (line.direction != undefined) ? (line.point || line.origin) : new XY(line.nodes[0].x, line.nodes[0].y);
var vector = (line.direction != undefined) ? line.direction : new XY(line.nodes[1].x, line.nodes[1].y).subtract(origin);
return this.transform(new Matrix().reflection(vector, origin));
};
XY.prototype.scale = function (magnitude) { return new XY(this.x * magnitude, this.y * magnitude); };
XY.prototype.add = function (a, b) {
if (isValidPoint(a)) {
return new XY(this.x + a.x, this.y + a.y);
}
else if (isValidNumber(b)) {
return new XY(this.x + a, this.y + b);
}
};
XY.prototype.subtract = function (point) { return new XY(this.x - point.x, this.y - point.y); };
XY.prototype.multiply = function (m) { return new XY(this.x * m.x, this.y * m.y); };
XY.prototype.abs = function () { return new XY(Math.abs(this.x), Math.abs(this.y)); };
XY.prototype.commonX = function (point, epsilon) { return epsilonEqual(this.x, point.x, epsilon); };
XY.prototype.commonY = function (point, epsilon) { return epsilonEqual(this.y, point.y, epsilon); };
return XY;
}());
var LineType = (function () {
function LineType() {
}
LineType.prototype.length = function () { };
LineType.prototype.vector = function () { };
LineType.prototype.parallel = function (line, epsilon) { };
LineType.prototype.collinear = function (point) { };
LineType.prototype.equivalent = function (line, epsilon) { };
LineType.prototype.degenrate = function (epsilon) { };
LineType.prototype.intersection = function (line, epsilon) { };
LineType.prototype.reflectionMatrix = function () { };
LineType.prototype.nearestPoint = function (point) { };
LineType.prototype.nearestPointNormalTo = function (point) { };
LineType.prototype.transform = function (matrix) { };
return LineType;
}());
var Line = (function () {
function Line(a, b, c, d) {
if (a.x !== undefined) {
this.point = new XY(a.x, a.y);
this.direction = new XY(b.x, b.y);
}
else {
this.point = new XY(a, b);
this.direction = new XY(c, d);
}
}
Line.prototype.rays = function () { var a = new Ray(this.point, this.direction); return [a, a.flip()]; };
Line.prototype.length = function () { return Infinity; };
Line.prototype.vector = function () { return this.direction; };
Line.prototype.parallel = function (line, epsilon) {
if (epsilon === undefined) {
epsilon = EPSILON_HIGH;
}
var v = (line.nodes !== undefined)
? new XY(line.nodes[1].x - line.nodes[0].x, line.nodes[1].y - line.nodes[0].y)
: line.direction;
return (v !== undefined) ? epsilonEqual(this.direction.cross(v), 0, epsilon) : undefined;
};
Line.prototype.collinear = function (point, epsilon) {
if (epsilon === undefined) {
epsilon = EPSILON_HIGH;
}
var x = [this.point.x, this.point.x + this.direction.x, point.x];
var y = [this.point.y, this.point.y + this.direction.y, point.y];
return epsilonEqual(x[0] * (y[1] - y[2]) + x[1] * (y[2] - y[0]) + x[2] * (y[0] - y[1]), 0, epsilon);
};
Line.prototype.equivalent = function (line, epsilon) {
return this.collinear(line.point, epsilon) && this.parallel(line, epsilon);
};
Line.prototype.degenrate = function (epsilon) {
if (epsilon === undefined) {
epsilon = EPSILON_HIGH;
}
return epsilonEqual(this.direction.magnitude(), 0, epsilon);
};
Line.prototype.intersection = function (line, epsilon) {
if (line instanceof Line) {
return intersectionLineLine(this, line, epsilon);
}
if (line instanceof Ray) {
return intersectionLineRay(this, line, epsilon);
}
if (line instanceof Edge) {
return intersectionLineEdge(this, line, epsilon);
}
};
Line.prototype.reflectionMatrix = function () { return new Matrix().reflection(this.direction, this.point); };
Line.prototype.nearestPoint = function (point) { return this.nearestPointNormalTo(point); };
Line.prototype.nearestPointNormalTo = function (point) {
var v = this.direction.normalize();
var u = ((point.x - this.point.x) * v.x + (point.y - this.point.y) * v.y);
return new XY(this.point.x + u * v.x, this.point.y + u * v.y);
};
Line.prototype.transform = function (matrix) {
return new Line(this.point.transform(matrix), this.direction.transform(matrix));
};
Line.prototype.bisect = function (line) {
if (this.parallel(line)) {
return [new Line(this.point.midpoint(line.point), this.direction)];
}
else {
var intersection = intersectionLineLine(this, line);
var vectors = bisectVectors(this.direction, line.direction);
vectors[1] = vectors[1].rotate90();
if (Math.abs(this.direction.cross(vectors[1])) < Math.abs(this.direction.cross(vectors[0]))) {
var swap = vectors[0];
vectors[0] = vectors[1];
vectors[1] = swap;
}
return vectors.map(function (el) { return new Line(intersection, el); }, this);
}
};
Line.prototype.subsect = function (line, count) {
var pcts = Array.apply(null, Array(count)).map(function (el, i) { return i / count; });
pcts.shift();
if (this.parallel(line)) {
return pcts.map(function (pct) { return new Line(this.point.lerp(line.point, pct), this.direction); }, this);
}
else {
var intersection = intersectionLineLine(this, line);
return [
[new Sector(intersection, [intersection.add(this.direction), intersection.add(line.direction)]),
new Sector(intersection, [intersection.add(this.direction), intersection.add(line.direction.rotate180())])
].sort(function (a, b) { return a.angle() - b.angle(); }).shift(),
[new Sector(intersection, [intersection.add(line.direction), intersection.add(this.direction)]),
new Sector(intersection, [intersection.add(line.direction), intersection.add(this.direction.rotate180())])
].sort(function (a, b) { return a.angle() - b.angle(); }).shift()
].map(function (sector) { return sector.subsect(count); }, this)
.reduce(function (prev, curr) { return prev.concat(curr); }, [])
.map(function (ray) { return new Line(ray.origin, ray.direction); }, this);
}
};
return Line;
}());
var Ray = (function () {
function Ray(a, b, c, d) {
if (a.x !== undefined) {
this.origin = new XY(a.x, a.y);
this.direction = new XY(b.x, b.y);
}
else {
this.origin = new XY(a, b);
this.direction = new XY(c, d);
}
;
}
Ray.prototype.length = function () { return Infinity; };
Ray.prototype.vector = function () { return this.direction; };
Ray.prototype.parallel = function (line, epsilon) {
if (epsilon === undefined) {
epsilon = EPSILON_HIGH;
}
var v = (line.nodes !== undefined)
? new XY(line.nodes[1].x - line.nodes[0].x, line.nodes[1].y - line.nodes[0].y)
: line.direction;
if (v === undefined) {
return undefined;
}
return epsilonEqual(this.direction.cross(v), 0, epsilon);
};
Ray.prototype.collinear = function (point, epsilon) {
if (epsilon === undefined) {
epsilon = EPSILON_HIGH;
}
var pOrigin = new XY(point.x - this.origin.x, point.y - this.origin.y);
var dot = pOrigin.dot(this.direction);
if (dot < -epsilon) {
return false;
}
var cross = pOrigin.cross(this.direction);
return epsilonEqual(cross, 0, epsilon);
};
Ray.prototype.equivalent = function (ray, epsilon) {
if (epsilon === undefined) {
epsilon = EPSILON_HIGH;
}
return (this.origin.equivalent(ray.origin, epsilon) &&
this.direction.normalize().equivalent(ray.direction.normalize(), epsilon));
};
Ray.prototype.degenrate = function (epsilon) {
if (epsilon === undefined) {
epsilon = EPSILON_HIGH;
}
return epsilonEqual(this.direction.magnitude(), 0, epsilon);
};
Ray.prototype.intersection = function (line, epsilon) {
if (line instanceof Ray) {
return intersectionRayRay(this, line, epsilon);
}
if (line instanceof Line) {
return intersectionLineRay(line, this, epsilon);
}
if (line instanceof Edge) {
return intersectionRayEdge(this, line, epsilon);
}
};
Ray.prototype.reflectionMatrix = function () { return new Matrix().reflection(this.direction, this.origin); };
Ray.prototype.nearestPoint = function (point) {
var answer = this.nearestPointNormalTo(point);
if (answer !== undefined) {
return answer;
}
return this.origin;
};
Ray.prototype.nearestPointNormalTo = function (point) {
var v = this.direction.normalize();
var u = ((point.x - this.origin.x) * v.x + (point.y - this.origin.y) * v.y);
if (u < 0) {
return undefined;
}
return new XY(this.origin.x + u * v.x, this.origin.y + u * v.y);
};
Ray.prototype.transform = function (matrix) {
return new Ray(this.origin.transform(matrix), this.direction.transform(matrix));
};
Ray.prototype.flip = function () { return new Ray(this.origin, new XY(-this.direction.x, -this.direction.y)); };
Ray.prototype.clipWithEdge = function (edge, epsilon) {
var intersect = intersectionRayEdge(this, edge, epsilon);
if (intersect === undefined) {
return undefined;
}
return new Edge(this.origin, intersect);
};
Ray.prototype.clipWithEdges = function (edges, epsilon) {
if (epsilon === undefined) {
epsilon = EPSILON_HIGH;
}
return edges
.map(function (edge) { return this.clipWithEdge(edge); }, this)
.filter(function (edge) { return edge !== undefined; })
.map(function (edge) { return { edge: edge, length: edge.length() }; })
.filter(function (el) { return el.length > epsilon; })
.sort(function (a, b) { return a.length - b.length; })
.map(function (el) { return el.edge; });
};
Ray.prototype.intersectionsWithEdges = function (edges, epsilon) {
if (epsilon === undefined) {
epsilon = EPSILON_HIGH;
}
return edges
.map(function (edge) { return intersectionRayEdge(this, edge, epsilon); }, this)
.filter(function (point) { return point !== undefined; }, this)
.map(function (point) { return { point: point, length: point.distanceTo(this.origin) }; }, this)
.sort(function (a, b) { return a.length - b.length; })
.map(function (el) { return el.point; }, this);
};
Ray.prototype.clipWithEdgesDetails = function (edges, epsilon) {
if (epsilon === undefined) {
epsilon = EPSILON_HIGH;
}
return edges
.map(function (edge) { return { 'edge': this.clipWithEdge(edge), 'intersection': edge }; }, this)
.filter(function (el) { return el.edge !== undefined; })
.map(function (el) {
return {
'edge': el.edge,
'intersection': el.intersection,
'length': el.edge.length()
};
})
.filter(function (el) { return el.length > epsilon; })
.sort(function (a, b) { return a.length - b.length; })
.map(function (el) { return { edge: el.edge, intersection: el.intersection }; });
};
return Ray;
}());
var Edge = (function () {
function Edge(a, b, c, d) {
if (a.x !== undefined) {
this.nodes = [new XY(a.x, a.y), new XY(b.x, b.y)];
}
else if (isValidNumber(d)) {
this.nodes = [new XY(a, b), new XY(c, d)];
}
else if (a.nodes !== undefined) {
this.nodes = [new XY(a.nodes[0].x, a.nodes[0].y), new XY(a.nodes[1].x, a.nodes[1].y)];
}
}
Edge.prototype.length = function () { return Math.sqrt(Math.pow(this.nodes[0].x - this.nodes[1].x, 2) + Math.pow(this.nodes[0].y - this.nodes[1].y, 2)); };
Edge.prototype.vector = function (originNode) {
if (originNode === undefined) {
return this.nodes[1].subtract(this.nodes[0]);
}
if (this.nodes[0].equivalent(originNode)) {
return this.nodes[1].subtract(this.nodes[0]);
}
return this.nodes[0].subtract(this.nodes[1]);
};
Edge.prototype.parallel = function (line, epsilon) {
if (epsilon === undefined) {
epsilon = EPSILON_HIGH;
}
var v = (line.nodes !== undefined)
? new XY(line.nodes[1].x - line.nodes[0].x, line.nodes[1].y - line.nodes[0].y)
: line.direction;
if (v === undefined) {
return undefined;
}
var u = this.nodes[1].subtract(this.nodes[0]);
return epsilonEqual(u.cross(v), 0, epsilon);
};
Edge.prototype.collinear = function (point, epsilon) {
if (epsilon === undefined) {
epsilon = EPSILON_HIGH;
}
var p0 = new Edge(point, this.nodes[0]).length();
var p1 = new Edge(point, this.nodes[1]).length();
return epsilonEqual(this.length() - p0 - p1, 0, epsilon);
};
Edge.prototype.equivalent = function (e, epsilon) {
if (epsilon === undefined) {
epsilon = EPSILON_HIGH;
}
return ((this.nodes[0].equivalent(e.nodes[0], epsilon) &&
this.nodes[1].equivalent(e.nodes[1], epsilon)) ||
(this.nodes[0].equivalent(e.nodes[1], epsilon) &&
this.nodes[1].equivalent(e.nodes[0], epsilon)));
};
Edge.prototype.degenrate = function (epsilon) {
if (epsilon === undefined) {
epsilon = EPSILON_HIGH;
}
return this.nodes[0].equivalent(this.nodes[1], epsilon);
};
Edge.prototype.intersection = function (line, epsilon) {
if (line instanceof Edge) {
return intersectionEdgeEdge(this, line, epsilon);
}
if (line instanceof Line) {
return intersectionLineEdge(line, this, epsilon);
}
if (line instanceof Ray) {
return intersectionRayEdge(line, this, epsilon);
}
};
Edge.prototype.reflectionMatrix = function () {
return new Matrix().reflection(this.nodes[1].subtract(this.nodes[0]), this.nodes[0]);
};
Edge.prototype.nearestPoint = function (point) {
var answer = this.nearestPointNormalTo(point);
if (answer !== undefined) {
return answer;
}
return this.nodes
.map(function (el) { return { point: el, distance: el.distanceTo(point) }; }, this)
.sort(function (a, b) { return a.distance - b.distance; })
.shift()
.point;
};
Edge.prototype.nearestPointNormalTo = function (point) {
var p = this.nodes[0].distanceTo(this.nodes[1]);
var u = ((point.x - this.nodes[0].x) * (this.nodes[1].x - this.nodes[0].x) + (point.y - this.nodes[0].y) * (this.nodes[1].y - this.nodes[0].y)) / (Math.pow(p, 2));
if (u < 0 || u > 1.0) {
return undefined;
}
return new XY(this.nodes[0].x + u * (this.nodes[1].x - this.nodes[0].x), this.nodes[0].y + u * (this.nodes[1].y - this.nodes[0].y));
};
Edge.prototype.transform = function (matrix) {
return new Edge(this.nodes[0].transform(matrix), this.nodes[1].transform(matrix));
};
Edge.prototype.midpoint = function () {
return new XY(0.5 * (this.nodes[0].x + this.nodes[1].x), 0.5 * (this.nodes[0].y + this.nodes[1].y));
};
Edge.prototype.perpendicularBisector = function () { return new Line(this.midpoint(), this.vector().rotate90()); };
Edge.prototype.infiniteLine = function () { return new Line(this.nodes[0], this.nodes[1].subtract(this.nodes[0])); };
return Edge;
}());
var Polyline = (function () {
function Polyline() {
this.nodes = [];
}
Polyline.prototype.edges = function () {
var result = [];
for (var i = 0; i < this.nodes.length - 1; i++) {
result.push(new Edge(this.nodes[i], this.nodes[i + 1]));
}
return result;
};
Polyline.prototype.rayReflectRepeat = function (ray, intersectable, target) {
var REFLECT_LIMIT = 666;
var clips = [];
var firstClips = ray.clipWithEdgesDetails(intersectable);
if (target !== undefined &&
epsilonEqual(ray.direction.cross(target.subtract(ray.origin)), 0, EPSILON_HIGH)) {
if (firstClips.length === 0 ||
ray.origin.distanceTo(target) < firstClips[0].edge.length()) {
this.nodes = [ray.origin, target];
return this;
}
}
clips.push(firstClips[0]);
var i = 0;
while (i < REFLECT_LIMIT) {
var prevClip = clips[clips.length - 1];
var n0 = new XY(prevClip.intersection.nodes[0].x, prevClip.intersection.nodes[0].y);
var n1 = new XY(prevClip.intersection.nodes[1].x, prevClip.intersection.nodes[1].y);
var reflection = new Matrix().reflection(n1.subtract(n0), n0);
var newRay = new Ray(prevClip.edge.nodes[1], prevClip.edge.nodes[0].transform(reflection).subtract(prevClip.edge.nodes[1]));
var newClips = newRay.clipWithEdgesDetails(intersectable);
if (target !== undefined &&
epsilonEqual(newRay.direction.cross(target.subtract(newRay.origin)), 0, EPSILON_HIGH)) {
clips.push({ edge: new Edge(newRay.origin, target), intersection: undefined });
break;
}
if (newClips.length === 0 || newClips[0] === undefined) {
break;
}
clips.push(newClips[0]);
i++;
}
this.nodes = clips.map(function (el) { return el.edge.nodes[0]; });
this.nodes.push(clips[clips.length - 1].edge.nodes[1]);
return this;
};
return Polyline;
}());
var Rect = (function () {
function Rect(x, y, width, height) {
this.origin = { 'x': x, 'y': y };
this.size = { 'width': width, 'height': height };
}
Rect.prototype.contains = function (point, epsilon) {
if (epsilon == undefined) {
epsilon = 0;
}
return point.x > this.origin.x - epsilon &&
point.y > this.origin.y - epsilon &&
point.x < this.origin.x + this.size.width + epsilon &&
point.y < this.origin.y + this.size.height + epsilon;
};
return Rect;
}());
var Triangle = (function () {
function Triangle(points, circumcenter) {
this.points = points;
this.edges = this.points.map(function (el, i) {
var nextEl = this.points[(i + 1) % this.points.length];
return new Edge(el, nextEl);
}, this);
this.sectors = this.points.map(function (el, i) {
var prevI = (i + this.points.length - 1) % this.points.length;
var nextI = (i + 1) % this.points.length;
return new Sector(el, [this.points[prevI], this.points[nextI]]);
}, this);
this.circumcenter = circumcenter;
if (circumcenter === undefined) {
}
}
Triangle.prototype.angles = function () {
return this.points.map(function (p, i) {
var prevP = this.points[(i + this.points.length - 1) % this.points.length];
var nextP = this.points[(i + 1) % this.points.length];
return clockwiseInteriorAngle(nextP.subtract(p), prevP.subtract(p));
}, this);
};
Triangle.prototype.isAcute = function () {
var a = this.angles();
for (var i = 0; i < a.length; i++) {
if (a[i] > Math.PI * 0.5) {
return false;
}
}
return true;
};
Triangle.prototype.isObtuse = function () {
var a = this.angles();
for (var i = 0; i < a.length; i++) {
if (a[i] > Math.PI * 0.5) {
return true;
}
}
return false;
};
Triangle.prototype.isRight = function () {
var a = this.angles();
for (var i = 0; i < a.length; i++) {
if (epsilonEqual(a[i], Math.PI * 0.5)) {
return true;
}
}
return false;
};
Triangle.prototype.pointInside = function (p) {
for (var i = 0; i < this.points.length; i++) {
var p0 = this.points[i];
var p1 = this.points[(i + 1) % this.points.length];
var cross = (p.y - p0.y) * (p1.x - p0.x) -
(p.x - p0.x) * (p1.y - p0.y);
if (cross < 0)
return false;
}
return true;
};
return Triangle;
}());
var Circle = (function () {
function Circle(a, b, c) {
if (c !== undefined) {
this.center = new XY(a, b);
this.radius = c;
}
else {
this.center = a;
this.radius = b;
}
}
Circle.prototype.intersection = function (line) {
if (line instanceof Line) {
return intersectionCircleLine(this.center, this.radius, line.point, line.point.add(line.direction));
}
if (line instanceof Edge) {
return intersectionCircleLine(this.center, this.radius, line.nodes[0], line.nodes[1]);
}
if (line instanceof Ray) {
return intersectionCircleLine(this.center, this.radius, line.origin, line.origin.add(line.direction));
}
};
return Circle;
}());
var Polygon = (function () {
function Polygon() {
this.nodes = [];
}
Polygon.prototype.equivalent = function (polygon) {
if (polygon.nodes.length != this.nodes.length) {
return false;
}
var iFace = undefined;
polygon.nodes.forEach(function (n, i) { if (n === this.nodes[0]) {
iFace = i;
return;
} }, this);
if (iFace == undefined) {
return false;
}
for (var i = 0; i < this.nodes.length; i++) {
var iFaceMod = (iFace + i) % this.nodes.length;
if (this.nodes[i] !== polygon.nodes[iFaceMod]) {
return false;
}
}
return true;
};
Polygon.prototype.contains = function (point) {
var isInside = false;
for (var i = 0, j = this.nodes.length - 1; i < this.nodes.length; j = i++) {
if ((this.nodes[i].y > point.y) != (this.nodes[j].y > point.y) &&
point.x < (this.nodes[j].x - this.nodes[i].x) * (point.y - this.nodes[i].y) / (this.nodes[j].y - this.nodes[i].y) + this.nodes[i].x) {
isInside = !isInside;
}
}
return isInside;
};
Polygon.prototype.signedArea = function () {
return 0.5 * this.nodes.map(function (el, i) {
var nextEl = this.nodes[(i + 1) % this.nodes.length];
return el.x * nextEl.y - nextEl.x * el.y;
}, this)
.reduce(function (prev, cur) { return prev + cur; }, 0);
};
Polygon.prototype.centroid = function () {
return this.nodes.map(function (el, i) {
var nextEl = this.nodes[(i + 1) % this.nodes.length];
var mag = el.x * nextEl.y - nextEl.x * el.y;
return new XY((el.x + nextEl.x) * mag, (el.y + nextEl.y) * mag);
}, this)
.reduce(function (prev, current) { return prev.add(current); }, new XY(0, 0))
.scale(1 / (6 * this.signedArea()));
};
Polygon.prototype.center = function () {
var xMin = Infinity, xMax = 0, yMin = Infinity, yMax = 0;
for (var i = 0; i < this.nodes.length; i++) {
if (this.nodes[i].x > xMax) {
xMax = this.nodes[i].x;
}
if (this.nodes[i].x < xMin) {
xMin = this.nodes[i].x;
}
if (this.nodes[i].y > yMax) {
yMax = this.nodes[i].y;
}
if (this.nodes[i].y < yMin) {
yMin = this.nodes[i].y;
}
}
return new XY(xMin + (xMax - xMin) * 0.5, yMin + (yMax - yMin) * 0.5);
};
Polygon.prototype.transform = function (matrix) { this.nodes.forEach(function (node) { node.transform(matrix); }, this); };
return Polygon;
}());
var ConvexPolygon = (function () {
function ConvexPolygon() {
this.edges = [];
}
ConvexPolygon.prototype.nodes = function () {
return this.edges.map(function (el, i) {
var nextEl = this.edges[(i + 1) % this.edges.length];
if (el.nodes[0].equivalent(nextEl.nodes[0]) || el.nodes[0].equivalent(nextEl.nodes[1])) {
return el.nodes[1];
}
return el.nodes[0];
}, this);
};
ConvexPolygon.prototype.signedArea = function (nodes) {
if (nodes === undefined) {
nodes = this.nodes();
}
return 0.5 * nodes.map(function (el, i) {
var nextEl = nodes[(i + 1) % nodes.length];
return el.x * nextEl.y - nextEl.x * el.y;
}, this)
.reduce(function (prev, cur) {
return prev + cur;
}, 0);
};
ConvexPolygon.prototype.centroid = function () {
var nodes = this.nodes();
return nodes.map(function (el, i) {
var nextEl = nodes[(i + 1) % nodes.length];
var mag = el.x * nextEl.y - nextEl.x * el.y;
return new XY((el.x + nextEl.x) * mag, (el.y + nextEl.y) * mag);
}, this)
.reduce(function (prev, current) {
return prev.add(current);
}, new XY(0, 0))
.scale(1 / (6 * this.signedArea(nodes)));
};
ConvexPolygon.prototype.center = function () {
var xMin = Infinity, xMax = 0, yMin = Infinity, yMax = 0;
var nodes = this.edges.map(function (el) { return el.nodes[0]; });
for (var i = 0; i < nodes.length; i++) {
if (nodes[i].x > xMax) {
xMax = nodes[i].x;
}
if (nodes[i].x < xMin) {
xMin = nodes[i].x;
}
if (nodes[i].y > yMax) {
yMax = nodes[i].y;
}
if (nodes[i].y < yMin) {
yMin = nodes[i].y;
}
}
return new XY(xMin + (xMax - xMin) * 0.5, yMin + (yMax - yMin) * 0.5);
};
ConvexPolygon.prototype.contains = function (p) {
var found = true;
for (var i = 0; i < this.edges.length; i++) {
var a = this.edges[i].nodes[1].subtract(this.edges[i].nodes[0]);
var b = new XY(p.x - this.edges[i].nodes[0].x, p.y - this.edges[i].nodes[0].y);
if (a.cross(b) < 0) {
return false;
}
}
return true;
};
ConvexPolygon.prototype.liesOnEdge = function (p) {
for (var i = 0; i < this.edges.length; i++) {
if (this.edges[i].collinear(p)) {
return true;
}
}
return false;
};
ConvexPolygon.prototype.clipEdge = function (edge) {
var intersections = this.edges
.map(function (el) { return intersectionEdgeEdge(edge, el); })
.filter(function (el) { return el !== undefined; })
.filter(function (el) {
return !el.equivalent(edge.nodes[0]) &&
!el.equivalent(edge.nodes[1]);
});
switch (intersections.length) {
case 0:
if (this.contains(edge.nodes[0])) {
return edge;
}
return undefined;
case 1:
if (this.contains(edge.nodes[0])) {
return new Edge(edge.nodes[0], intersections[0]);
}
return new Edge(edge.nodes[1], intersections[0]);
default:
for (var i = 1; i < intersections.length; i++) {
if (!intersections[0].equivalent(intersections[i])) {
return new Edge(intersections[0], intersections[i]);
}
}
}
};
ConvexPolygon.prototype.clipLine = function (line) {
var intersections = this.edges
.map(function (el) { return intersectionLineEdge(line, el); })
.filter(function (el) { return el !== undefined; });
switch (intersections.length) {
case 0: return undefined;
case 1: return new Edge(intersections[0], intersections[0]);
default:
for (var i = 1; i < intersections.length; i++) {
if (!intersections[0].equivalent(intersections[i])) {
return new Edge(intersections[0], intersections[i]);
}
}
}
};
ConvexPolygon.prototype.clipRay = function (ray) {
var intersections = this.edges
.map(function (el) { return intersectionRayEdge(ray, el); })
.filter(function (el) { return el !== undefined; });
switch (intersections.length) {
case 0: return undefined;
case 1: return new Edge(ray.origin, intersections[0]);
default:
for (var i = 1; i < intersections.length; i++) {
if (!intersections[0].equivalent(intersections[i])) {
return new Edge(intersections[0], intersections[i]);
}
}
}
};
ConvexPolygon.prototype.setEdgesFromPoints = function (points) {
this.edges = points.map(function (el, i) {