-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathh3du-path.js
4447 lines (4365 loc) · 150 KB
/
h3du-path.js
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
/*
Any copyright to this file is released to the Public Domain.
In case this is not possible, this file is also licensed under the Unlicense: https://unlicense.org/
*/
import {BSplineCurve} from "./h3du-bspline.js";
import {Curve} from "./h3du-curve.js";
import {MathUtil} from "./h3du-math.js";
import {PiecewiseCurve} from "./h3du-piecewisecurve.js";
/** @ignore
* @private
* @constructor */
const LinkedListNode = function(item) {
this.data = item;
this.prev = null;
this.next = null;
};
/** @ignore
* @constructor */
const LinkedList = function() {
this.root = null;
this._last = null;
this.size = function() {
let k = this.root;
let ret = 0;
while(k) {
ret++;
k = k.next;
}
return ret;
};
this.first = function() {
return this.root;
};
this.last = function() {
return this._last;
};
this.front = function() {
return this.root ? this.root.data : null;
};
this.back = function() {
return this._last ? this._last.data : null;
};
this.clear = function() {
this.root = this._last = null;
};
this.spliceToBegin = function(list) {
if(list.root) {
this.root.prev = list._last;
list._last.next = this.root;
this.root = list.root;
list.clear();
}
};
this.spliceToEnd = function(list) {
if(list.root) {
this._last.next = list.root;
list.root.prev = this._last;
this._last = list._last;
list.clear();
}
};
this.spliceOneToEnd = function(list, listNode) {
list.erase(listNode);
return this.push(listNode.data);
};
this.erase = function(node) {
if(!node)return this;
if(node === this.root) {
this.root = node.next;
}
if(node === this._last) {
this._last = node.prev;
}
if(node.prev)
node.prev.next = node.next;
if(node.next)
node.next.prev = node.prev;
return this;
};
this.insertAfter = function(item, node) {
const newNode = new LinkedListNode(item);
if(node === this._last)
this._last = newNode;
const oldNext = node.next;
node.next = newNode;
newNode.prev = node;
newNode.next = oldNext;
if(oldNext) {
oldNext.prev = newNode;
}
return newNode;
};
this.push = function(item) {
if(!this.root) {
this.root = this._last = new LinkedListNode(item);
} else {
const node = new LinkedListNode(item);
this._last.next = node;
node.prev = this._last;
this._last = node;
}
return this;
};
this.reverse = function() {
let s = this.root;
const e = this._last;
if(!s)return;
const oldlast = e;
const oldroot = s;
while(s) {
const n = s.next;
const p = s.prev;
s.prev = n;
s.next = p;
s = n;
}
this.root = oldlast;
this._last = oldroot;
return this;
};
this.unshift = function(item) {
if(!this.root) {
this.root = this._last = new LinkedListNode(item);
} else {
const node = new LinkedListNode(item);
this.root.prev = node;
node.next = this.root;
this.root = node;
}
return this;
};
this.pop = function() {
if(this._last) {
if(this._last.prev)
this._last.prev.next = null;
this._last = this._last.prev;
}
return this;
};
this.shift = function() {
if(this.root) {
if(this.root.next)
this.root.next.prev = null;
this.root = this.root.next;
}
return this;
};
};
// --------------------------------------------------
/** @ignore
* @constructor */
function LineCurve(x1, y1, x2, y2) {
this.x1 = x1;
this.x2 = x2;
this.y1 = y1;
this.y2 = y2;
}
LineCurve.prototype = Object.create(Curve.prototype);
LineCurve.prototype.constructor = LineCurve;
/** @ignore */
LineCurve.prototype.evaluate = function(u) {
return [
this.x1 + (this.x2 - this.x1) * u,
this.y1 + (this.y2 - this.y1) * u, 0
];
};
/** @ignore */
LineCurve.prototype.velocity = function() {
return [
this.x2 - this.x1,
this.y2 - this.y1, 0
];
};
/** @ignore */
LineCurve.prototype.arcLength = function(u) {
const x = this.x1 + (this.x2 - this.x1) * u;
const y = this.y1 + (this.y2 - this.y1) * u;
const dx = x - this.x1;
const dy = y - this.y1;
let ret = Math.sqrt(dx * dx + dy * dy);
if(u < 0)ret = -ret;
return ret;
};
/** @ignore
* @constructor
*/
function ArcCurve(x1, y1, x2, y2, rx, ry, rot, cx, cy, theta, delta) {
this.x1 = x1;
this.x2 = x2;
this.y1 = y1;
this.y2 = y2;
this.rx = rx;
this.ry = ry;
const cr = Math.cos(rot);
const sr = rot >= 0 && rot < 6.283185307179586 ? rot <= 3.141592653589793 ? Math.sqrt(1.0 - cr * cr) : -Math.sqrt(1.0 - cr * cr) : Math.sin(rot);
this.cr = cr;
this.sr = sr;
this.cx = cx;
this.cy = cy;
this.theta = theta;
this.delta = delta;
}
ArcCurve.prototype = Object.create(Curve.prototype);
ArcCurve.prototype.constructor = ArcCurve;
/** @ignore */
ArcCurve.prototype.evaluate = function(t) {
if(t === 0)return [this.x1, this.y1, 0];
if(t === 1)return [this.x2, this.y2, 0];
const angle = this.theta + this.delta * t;
const ca = Math.cos(angle);
const sa = angle >= 0 && angle < 6.283185307179586 ? angle <= 3.141592653589793 ? Math.sqrt(1.0 - ca * ca) : -Math.sqrt(1.0 - ca * ca) : Math.sin(angle);
return [
this.cr * ca * this.rx - this.sr * sa * this.rx + this.cx,
this.sr * ca * this.rx + this.cr * sa * this.ry + this.cy, 0];
};
/** @ignore */
ArcCurve.prototype.velocity = function(t) {
const angle = this.theta + this.delta * t;
const ca = Math.cos(angle);
const sa = angle >= 0 && angle < 6.283185307179586 ? angle <= 3.141592653589793 ? Math.sqrt(1.0 - ca * ca) : -Math.sqrt(1.0 - ca * ca) : Math.sin(angle);
const caDeriv = -sa * this.delta;
const saDeriv = ca * this.delta;
return [
this.cr * caDeriv * this.rx - this.sr * saDeriv * this.rx,
this.sr * caDeriv * this.rx + this.cr * saDeriv * this.ry, 0];
};
// --------------------------------------------------
/**
* Represents a two-dimensional path.
* A path is a collection of two-dimensional line segments and/or curves. Many paths describe
* closed figures or connected sequences of lines and curves. Specifically, a path is made up
* of straight line segments, elliptical arcs, quadratic Bézier curves,
* cubic Bézier curves, or any combination of these, and
* the path can be discontinuous and/or contain closed parts.
* <h4>Creating Paths</h4>
* <p>
* There are two ways to create paths: using an SVG path string (see {@link GraphicsPath.fromString}), or by calling methods that add its segments.
* <p>A `GraphicsPath` object stores a current position and a starting position, and many methods don't have you specify a starting position, to cover the common case of drawing a series of connected lines and curves.
* _.moveTo(x, y)_ - Moves the starting position and current position.
* _.lineTo(x, y)_ - Adds a line segment from the current position to a new ending position.
* _.closePath()_ - Closes the path by drawing a line to the starting point, if needed.
* <h4>Path Segments</h4>
* Each path can include a number of line segments, Bézier curves, and elliptical arcs.
* Line segments are relatively easy to understand. The other two kinds of segments
* deserve some discussion.
* A _Bézier curve_ is a parametric curve based on a polynomial formula. In this kind of
* curve the endpoints are defined as they are, but the other points define
* the shape of the curve and generally don't cross the curve.
* A quadratic Bézier curve uses 3 points. A cubic Bézier
* curve uses 4 points.
* An _elliptical arc_ is a curve which forms part of an ellipse. There are several ways to
* parameterize an elliptical arc, as seen in the _.arc()_, _.arcTo()_, and _.arcSvgTo()_ methods
* of the `GraphicsPath` class.
* @constructor
*/
export const GraphicsPath = function() {
this.segments = [];
this.incomplete = false;
this.startPos = [0, 0];
this.endPos = [0, 0];
};
/** @ignore */
const Triangulate = {};
/** @ignore */
GraphicsPath.CLOSE = 0;
/** @ignore */
GraphicsPath.LINE = 1;
/** @ignore */
GraphicsPath.QUAD = 2;
/** @ignore */
GraphicsPath.CUBIC = 3;
/** @ignore */
GraphicsPath.ARC = 4;
/**
* Returns whether the curve path is incomplete
* because of an error in parsing the curve string.
* This flag will be reset if a moveTo command,
* closePath command, or another path segment
* is added to the path.
* @returns {boolean} Return value.
*/
GraphicsPath.prototype.isIncomplete = function() {
return this.incomplete;
};
/** @ignore */
GraphicsPath._startPoint = function(a) {
if(a[0] === GraphicsPath.CLOSE) {
return [0, 0];
} else {
return [a[1], a[2]];
}
};
/** @ignore */
GraphicsPath._endPoint = function(a) {
if(a[0] === GraphicsPath.CLOSE) {
return [0, 0];
} else if(a[0] === GraphicsPath.ARC) {
return [a[8], a[9]];
} else {
return [a[a.length - 2], a[a.length - 1]];
}
};
/** @ignore */
GraphicsPath._point = function(seg, t) {
let a;
let b;
let x;
let y;
if(seg[0] === GraphicsPath.CLOSE) {
return [0, 0];
} else if(seg[0] === GraphicsPath.LINE) {
return [
seg[1] + (seg[3] - seg[1]) * t,
seg[2] + (seg[4] - seg[2]) * t
];
} else if(seg[0] === GraphicsPath.QUAD) {
const mt = 1 - t;
const mtsq = mt * mt;
const mt2 = mt + mt;
a = seg[1] * mtsq;
b = seg[3] * mt2;
x = a + t * (b + t * seg[5]);
a = seg[2] * mtsq;
b = seg[4] * mt2;
y = a + t * (b + t * seg[6]);
return [x, y];
} else if(seg[0] === GraphicsPath.CUBIC) {
a = (seg[3] - seg[1]) * 3;
b = (seg[5] - seg[3]) * 3 - a;
let c = seg[7] - a - b - seg[1];
x = seg[1] + t * (a + t * (b + t * c));
a = (seg[4] - seg[2]) * 3;
b = (seg[6] - seg[4]) * 3 - a;
c = seg[8] - a - b - seg[2];
y = seg[2] + t * (a + t * (b + t * c));
return [x, y];
} else if(seg[0] === GraphicsPath.ARC) {
if(t === 0)return [seg[1], seg[2]];
if(t === 1)return [seg[8], seg[9]];
const rx = seg[3];
const ry = seg[4];
const cx = seg[10];
const cy = seg[11];
const theta = seg[12];
const delta = seg[13] - seg[12];
const rot = seg[5];
const angle = theta + delta * t;
const cr = Math.cos(rot);
const sr = rot >= 0 && rot < 6.283185307179586 ? rot <= 3.141592653589793 ? Math.sqrt(1.0 - cr * cr) : -Math.sqrt(1.0 - cr * cr) : Math.sin(rot);
const ca = Math.cos(angle);
const sa = angle >= 0 && angle < 6.283185307179586 ? angle <= 3.141592653589793 ? Math.sqrt(1.0 - ca * ca) : -Math.sqrt(1.0 - ca * ca) : Math.sin(angle);
return [
cr * ca * rx - sr * sa * ry + cx,
sr * ca * rx + cr * sa * ry + cy];
} else {
return [0, 0];
}
};
/** @ignore */
GraphicsPath._segToCurve = function(seg) {
if(seg[0] === GraphicsPath.LINE) {
return new LineCurve(seg[1], seg[2], seg[3], seg[4]);
} else if(seg[0] === GraphicsPath.QUAD) {
return BSplineCurve.fromBezierCurve([
[seg[1], seg[2], 0], [seg[3], seg[4], 0], [seg[5], seg[6], 0]]);
} else if(seg[0] === GraphicsPath.CUBIC) {
return BSplineCurve.fromBezierCurve([
[seg[1], seg[2], 0], [seg[3], seg[4], 0], [seg[5], seg[6], 0], [seg[7], seg[8], 0]]);
} else if(seg[0] === GraphicsPath.ARC) {
const rx = seg[3];
const ry = seg[4];
const cx = seg[10];
const cy = seg[11];
const theta = seg[12];
const delta = seg[13] - seg[12];
const rot = seg[5];
return new ArcCurve(seg[1], seg[2], seg[8], seg[9], rx, ry, rot, cx, cy, theta, delta);
} else {
throw new Error();
}
};
/** @ignore */
GraphicsPath._subdivide2 = function(a1, a2, a3, a4, a5, a6, a7, a8, t1, t2, tcut, list, flatness, mode, depth) {
const x1 = a1 + (a3 - a1) * tcut;
const x2 = a3 + (a5 - a3) * tcut;
const xc1 = x1 + (x2 - x1) * tcut;
const x3 = a5 + (a7 - a5) * tcut;
const xc2 = x2 + (x3 - x2) * tcut;
const xd = xc1 + (xc2 - xc1) * tcut;
const y1 = a2 + (a4 - a2) * tcut;
const y2 = a4 + (a6 - a4) * tcut;
const yc1 = y1 + (y2 - y1) * tcut;
const y3 = a6 + (a8 - a6) * tcut;
const yc2 = y2 + (y3 - y2) * tcut;
const yd = yc1 + (yc2 - yc1) * tcut;
const tmid = t1 + (t2 - t1) * tcut;
GraphicsPath._flattenCubic(a1, a2, x1, y1, xc1, yc1, xd, yd, t1, tmid, list, flatness, mode, depth + 1);
GraphicsPath._flattenCubic(xd, yd, xc2, yc2, x3, y3, a7, a8, tmid, t2, list, flatness, mode, depth + 1);
};
/** @ignore */
GraphicsPath._subdivide3 = function(a1, a2, a3, a4, a5, a6, a7, a8, t1, t2, tcut, tcut2, list, flatness, mode, depth) {
const x1 = a1 + (a3 - a1) * tcut;
const x2 = a3 + (a5 - a3) * tcut;
const xc1 = x1 + (x2 - x1) * tcut;
const x3 = a5 + (a7 - a5) * tcut;
const xc2 = x2 + (x3 - x2) * tcut;
const xd = xc1 + (xc2 - xc1) * tcut;
const y1 = a2 + (a4 - a2) * tcut;
const y2 = a4 + (a6 - a4) * tcut;
const yc1 = y1 + (y2 - y1) * tcut;
const y3 = a6 + (a8 - a6) * tcut;
const yc2 = y2 + (y3 - y2) * tcut;
const yd = yc1 + (yc2 - yc1) * tcut;
const tmid = t1 + (t2 - t1) * tcut;
const tcutx = (tcut2 - tmid) / (t2 - tmid);
GraphicsPath._flattenCubic(a1, a2, x1, y1, xc1, yc1, xd, yd, t1, tmid, list, flatness, mode, depth + 1);
GraphicsPath._subdivide2(xd, yd, xc2, yc2, x3, y3, a7, a8, tmid, t2, tcutx, list, flatness, mode, depth + 1);
};
/** @ignore */
GraphicsPath._flattenCubic = function(a1, a2, a3, a4, a5, a6, a7, a8, t1, t2, list, flatness, mode, depth) {
if(typeof depth === "undefined" || depth === null)depth = 0;
if(depth >= 20 || Math.abs(a1 - a3 - a3 + a5) + Math.abs(a3 - a5 - a5 + a7) +
Math.abs(a2 - a4 - a4 + a6) + Math.abs(a4 - a6 - a6 + a8) <= flatness) {
if(mode === 0) {
list.push([a1, a2, a7, a8]);
} else {
const dx = a7 - a1;
const dy = a8 - a2;
const length = Math.sqrt(dx * dx + dy * dy);
list.push(t1, t2, length);
}
} else {
GraphicsPath._subdivide2(a1, a2, a3, a4, a5, a6, a7, a8, t1, t2, 0.5, list, flatness, mode, depth);
}
};
/** @ignore */
GraphicsPath._flattenQuad = function(a1, a2, a3, a4, a5, a6, t1, t2, list, flatness, mode, depth) {
if(typeof depth === "undefined" || depth === null)depth = 0;
if(depth >= 20 || Math.abs(a1 - a3 - a3 + a5) + Math.abs(a2 - a4 - a4 + a6) <= flatness) {
if(mode === 0) {
list.push([a1, a2, a5, a6]);
} else {
const dx = a5 - a1;
const dy = a6 - a2;
const length = Math.sqrt(dx * dx + dy * dy);
list.push(t1, t2, length);
}
} else {
const x1 = (a1 + a3) * 0.5;
const x2 = (a3 + a5) * 0.5;
const xc = (x1 + x2) * 0.5;
const y1 = (a2 + a4) * 0.5;
const y2 = (a4 + a6) * 0.5;
const yc = (y1 + y2) * 0.5;
const tmid = (t1 + t2) * 0.5;
GraphicsPath._flattenQuad(a1, a2, x1, y1, xc, yc, t1, tmid, list, flatness, mode, depth + 1);
GraphicsPath._flattenQuad(xc, yc, x2, y2, a5, a6, tmid, t2, list, flatness, mode, depth + 1);
}
};
/** @ignore */
GraphicsPath._flattenArc = function(a, t1, t2, list, flatness, mode, depth) {
const rot = a[5];
const crot = Math.cos(rot);
const srot = rot >= 0 && rot < 6.283185307179586 ? rot <= 3.141592653589793 ? Math.sqrt(1.0 - crot * crot) : -Math.sqrt(1.0 - crot * crot) : Math.sin(rot);
const ellipseInfo = [a[3], a[4], a[10], a[11], crot, srot];
GraphicsPath._flattenArcInternal(ellipseInfo, a[1], a[2], a[8], a[9], a[12], a[13], t1, t2, list, flatness, mode, depth);
};
/** @ignore */
GraphicsPath._flattenArcInternal = function(ellipseInfo, x1, y1, x2, y2, theta1, theta2, t1, t2, list, flatness, mode, depth) {
if(typeof depth === "undefined" || depth === null)depth = 0;
const thetaMid = (theta1 + theta2) * 0.5;
const tmid = (t1 + t2) * 0.5;
const ca = Math.cos(thetaMid);
const sa = thetaMid >= 0 && thetaMid < 6.283185307179586 ? thetaMid <= 3.141592653589793 ? Math.sqrt(1.0 - ca * ca) : -Math.sqrt(1.0 - ca * ca) : Math.sin(thetaMid);
const xmid = ellipseInfo[4] * ca * ellipseInfo[0] - ellipseInfo[5] * sa * ellipseInfo[1] + ellipseInfo[2];
const ymid = ellipseInfo[5] * ca * ellipseInfo[0] + ellipseInfo[4] * sa * ellipseInfo[1] + ellipseInfo[3];
if(depth >= 20 || Math.abs(x1 - xmid - xmid + x2) + Math.abs(y1 - ymid - ymid + y2) <= flatness) {
if(mode === 0) {
list.push([x1, y1, xmid, ymid]);
list.push([xmid, ymid, x2, y2]);
} else {
let dx = xmid - x1;
let dy = ymid - y1;
let length = Math.sqrt(dx * dx + dy * dy);
list.push(t1, tmid, length);
dx = x2 - xmid;
dy = y2 - ymid;
length = Math.sqrt(dx * dx + dy * dy);
list.push(tmid, t2, length);
}
} else {
GraphicsPath._flattenArcInternal(ellipseInfo, x1, y1, xmid, ymid, theta1, thetaMid, t1, tmid, list, flatness, mode, depth + 1);
GraphicsPath._flattenArcInternal(ellipseInfo, xmid, ymid, x2, y2, thetaMid, theta2, tmid, t2, list, flatness, mode, depth + 1);
}
};
/** @ignore */
GraphicsPath.prototype._start = function() {
let i;
for (i = 0; i < this.segments.length; i++) {
const s = this.segments[i];
if(s[0] !== GraphicsPath.CLOSE)return GraphicsPath._startPoint(s);
}
return [0, 0];
};
/** @ignore */
GraphicsPath.prototype._end = function() {
let i;
for (i = this.segments.length - 1; i >= 0; i--) {
const s = this.segments[i];
if(s[0] !== GraphicsPath.CLOSE)return GraphicsPath._endPoint(s);
}
return [0, 0];
};
/**
* Merges the path segments in another path onto this one.
* @param {GraphicsPath} path Another graphics path.
* Can be null.
* @returns {GraphicsPath} This object.
*/
GraphicsPath.prototype.merge = function(path) {
let oldpos = null;
if(!path)return this;
const segsLength = path.segments.length;
let i;
for (i = 0; i < segsLength; i++) {
const a = path.segments[i];
if(a[0] === GraphicsPath.CLOSE) {
this.closePath();
} else {
const start = GraphicsPath._startPoint(a);
if(!oldpos || oldpos[0] !== start[0] || oldpos[1] !== start[1]) {
this.moveTo(start[0], start[1]);
}
oldpos = GraphicsPath._endPoint(a);
if(a[0] === GraphicsPath.LINE) {
this.lineTo(a[3], a[4]);
}
if(a[0] === GraphicsPath.QUAD) {
this.quadraticCurveTo(a[3], a[4], a[5], a[6]);
}
if(a[0] === GraphicsPath.CUBIC) {
this.bezierCurveTo(a[3], a[4], a[5], a[6], a[7], a[8]);
}
if(a[0] === GraphicsPath.ARC) {
const delta = a[13] - a[12];
const largeArc = Math.abs(delta) > Math.PI;
this.arcSvgTo(a[3], a[4], a[5] * GraphicsPath._toDegrees,
largeArc, delta > 0, a[8], a[9]);
}
}
}
return this;
};
/**
* Returns this path in the form of a string in SVG path format.
* See {@link GraphicsPath.fromString}.
* @returns {string} A string describing the path in the SVG path
* format.
*/
GraphicsPath.prototype.toString = function() {
let oldpos = null;
let ret = "";
let i;
for (i = 0; i < this.segments.length; i++) {
const a = this.segments[i];
if(a[0] === GraphicsPath.CLOSE) {
ret += "Z";
} else {
const start = GraphicsPath._startPoint(a);
if(!oldpos || oldpos[0] !== start[0] || oldpos[1] !== start[1]) {
ret += "M" + start[0] + "," + start[1];
}
oldpos = GraphicsPath._endPoint(a);
if(a[0] === GraphicsPath.LINE) {
ret += "L" + a[3] + "," + a[4];
}
if(a[0] === GraphicsPath.QUAD) {
ret += "Q" + a[3] + "," + a[4] + "," + a[5] + "," + a[6];
}
if(a[0] === GraphicsPath.CUBIC) {
ret += "C" + a[3] + "," + a[4] + "," + a[5] + "," + a[6] + "," + a[7] + "," + a[8];
}
if(a[0] === GraphicsPath.ARC) {
const delta = a[13] - a[12];
const largeArc = Math.abs(delta) > Math.PI;
ret += "A" + a[3] + "," + a[4] + "," + a[5] * 180 / Math.PI + "," +
(largeArc ? "1" : "0") + (delta > 0 ? "1" : "0") + a[8] + "," + a[9];
}
}
}
return ret;
};
/**
* Finds the approximate length of this path.
* @param {number} [flatness] No longer used by this method.
* @returns {number} Approximate length of this path
* in units.
*/
GraphicsPath.prototype.pathLength = function(flatness) {
if(this.segments.length === 0)return 0;
if(typeof flatness !== "undefined" && flatness !== null) {
console.warn("Unused parameter flatness is defined");
}
return this.getCurves().getLength();
};
/**
* Gets an array of line segments approximating
* the path.
* @param {number} [flatness] When curves and arcs
* are decomposed to line segments, the
* segments will be close to the true path of the curve by this
* value, given in units. If null, undefined, or omitted, default is 1.
* @returns {Array<Array<number>>} Array of line segments.
* Each line segment is an array of four numbers: the X and
* y-coordinates of the start point, respectively, then the X and
* y-coordinates of the end point, respectively.
*/
GraphicsPath.prototype.getLines = function(flatness) {
const ret = [];
if(typeof flatness === "undefined" || flatness === null)flatness = 1.0;
let i;
for (i = 0; i < this.segments.length; i++) {
const s = this.segments[i];
if(s[0] === GraphicsPath.QUAD) {
GraphicsPath._flattenQuad(s[1], s[2], s[3], s[4],
s[5], s[6], 0.0, 1.0, ret, flatness * 2, 0, 0);
} else if(s[0] === GraphicsPath.CUBIC) {
GraphicsPath._flattenCubic(s[1], s[2], s[3], s[4],
s[5], s[6], s[7], s[8], 0.0, 1.0, ret, flatness * 2, 0, 0);
} else if(s[0] === GraphicsPath.ARC) {
GraphicsPath._flattenArc(s, 0.0, 1.0, ret, flatness * 2, 0, 0);
} else if(s[0] !== GraphicsPath.CLOSE) {
ret.push([s[1], s[2], s[3], s[4]]);
}
}
return ret;
};
/**
* Creates a path in which curves and arcs are decomposed
* to line segments.
* @param {number} [flatness] When curves and arcs
* are decomposed to line segments, the
* segments will be close to the true path of the curve by this
* value, given in units. If null, undefined, or omitted, default is 1.
* @returns {GraphicsPath} A path consisting only of line
* segments and close commands.
*/
GraphicsPath.prototype.toLinePath = function(flatness) {
const ret = [];
const path = new GraphicsPath();
let last = null;
if(typeof flatness === "undefined" || flatness === null)flatness = 1.0;
let i;
for (i = 0; i < this.segments.length; i++) {
const s = this.segments[i];
if(s[0] === GraphicsPath.CLOSE) {
path.closePath();
continue;
}
let j;
const endpt = GraphicsPath._endPoint(s);
const startpt = GraphicsPath._startPoint(s);
if(!last || last[0] !== startpt[0] || last[1] !== startpt[1]) {
path.moveTo(startpt[0], startpt[1]);
}
last = endpt;
ret.splice(0, ret.length);
if(s[0] === GraphicsPath.QUAD) {
GraphicsPath._flattenQuad(s[1], s[2], s[3], s[4],
s[5], s[6], 0.0, 1.0, ret, flatness * 2, 0, 0);
for(j = 0; j < ret.length; j++) {
path.lineTo(ret[j][2], ret[j][3]);
}
} else if(s[0] === GraphicsPath.CUBIC) {
GraphicsPath._flattenCubic(s[1], s[2], s[3], s[4],
s[5], s[6], s[7], s[8], 0.0, 1.0, ret, flatness * 2, 0, 0);
for(j = 0; j < ret.length; j++) {
path.lineTo(ret[j][2], ret[j][3]);
}
} else if(s[0] === GraphicsPath.ARC) {
GraphicsPath._flattenArc(s, 0.0, 1.0, ret, flatness * 2, 0, 0);
for(j = 0; j < ret.length; j++) {
path.lineTo(ret[j][2], ret[j][3]);
}
} else if(s[0] !== GraphicsPath.CLOSE) {
path.lineTo(s[3], s[4]);
} else {
path.closePath();
}
}
return path;
};
/**
* Creates a path in which arcs are decomposed
* to cubic Bézier curves (which will approximate those arcs).
* @returns {GraphicsPath} A path consisting only of line
* segments, Bézier curves, and close commands.
*/
GraphicsPath.prototype.toCurvePath = function() {
const path = new GraphicsPath();
let last = null;
let i;
for (i = 0; i < this.segments.length; i++) {
const s = this.segments[i];
if(s[0] === GraphicsPath.CLOSE) {
path.closePath();
continue;
}
let j;
const endpt = GraphicsPath._endPoint(s);
const startpt = GraphicsPath._startPoint(s);
if(!last || last[0] !== startpt[0] || last[1] !== startpt[1]) {
path.moveTo(startpt[0], startpt[1]);
}
last = endpt;
if(s[0] === GraphicsPath.QUAD) {
path.quadraticCurveTo(s[3], s[4],
s[5], s[6]);
} else if(s[0] === GraphicsPath.CUBIC) {
path.bezierCurveTo(s[3], s[4],
s[5], s[6], s[7], s[8]);
} else if(s[0] === GraphicsPath.ARC) {
const curves = GraphicsPath._arcToBezierCurves(s[10], s[11], s[3], s[4], s[5], s[12], s[13]);
for(j = 0; j < curves.length; j++) {
path.bezierCurveTo(curves[j][2], curves[j][3], curves[j][4],
curves[j][5], curves[j][6], curves[j][7]);
}
} else if(s[0] === GraphicsPath.LINE) {
path.lineTo(s[3], s[4]);
}
}
return path;
};
/** @ignore */
GraphicsPath._accBounds = function(ret, s, t) {
if(t >= 0 && t <= 1) {
const pt = GraphicsPath._point(s, t);
ret[0] = Math.min(pt[0], ret[0]);
ret[1] = Math.min(pt[1], ret[1]);
ret[2] = Math.max(pt[0], ret[2]);
ret[3] = Math.max(pt[1], ret[3]);
}
};
/** @ignore */
GraphicsPath._accBoundsPoint = function(ret, x, y) {
ret[0] = Math.min(x, ret[0]);
ret[1] = Math.min(y, ret[1]);
ret[2] = Math.max(x, ret[2]);
ret[3] = Math.max(y, ret[3]);
};
/** @ignore */
GraphicsPath._accBoundsArc = function(ret, rx, ry, cphi, sphi, cx, cy, angle) {
const ca = Math.cos(angle);
const sa = angle >= 0 && angle < 6.283185307179586 ? angle <= 3.141592653589793 ? Math.sqrt(1.0 - ca * ca) : -Math.sqrt(1.0 - ca * ca) : Math.sin(angle);
const px = cphi * ca * rx - sphi * sa * ry + cx;
const py = sphi * ca * rx + cphi * sa * ry + cy;
ret[0] = Math.min(px, ret[0]);
ret[1] = Math.min(py, ret[1]);
ret[2] = Math.max(px, ret[2]);
ret[3] = Math.max(py, ret[3]);
};
/** @ignore */
GraphicsPath._normAngleRadians = function(angle) {
const twopi = Math.PI * 2;
let normAngle = angle;
if(normAngle >= 0) {
normAngle = normAngle < twopi ? normAngle : normAngle % twopi;
} else {
normAngle %= twopi;
normAngle += twopi;
}
return normAngle;
};
/** @ignore */
GraphicsPath._angleInRange = function(angle, startAngle, endAngle) {
const twopi = Math.PI * 2;
const diff = endAngle - startAngle;
if(Math.abs(diff) >= twopi)return true;
const normAngle = GraphicsPath._normAngleRadians(angle);
const normStart = GraphicsPath._normAngleRadians(startAngle);
const normEnd = GraphicsPath._normAngleRadians(endAngle);
if(startAngle === endAngle) {
return normAngle === normStart;
} else if(startAngle < endAngle) {
if(normStart < normEnd) {
return normAngle >= normStart && normAngle <= normEnd;
} else {
return normAngle >= normStart || normAngle <= normEnd;
}
} else if(normEnd < normStart) {
return normAngle >= normEnd && normAngle <= normStart;
} else {
return normAngle >= normEnd || normAngle <= normStart;
}
};
/**
* Calculates an axis-aligned bounding box that tightly
* fits this graphics path.
* @returns {Array<number>} An array of four numbers
* describing the bounding box. The first two are
* the lowest x- and y-coordinates, and the last two are
* the highest x- and y-coordinates. If the path is empty,
* returns the array (Infinity, Infinity, -Infinity, -Infinity).
*/
GraphicsPath.prototype.getBounds = function() {
const inf = Number.POSITIVE_INFINITY;
const ret = [inf, inf, -inf, inf];
let first = true;
let i;
for (i = 0; i < this.segments.length; i++) {
const s = this.segments[i];
let ax;
let ay;
if(s[0] === GraphicsPath.CLOSE)continue;
const endpt = GraphicsPath._endPoint(s);
const x1 = s[1];
const y1 = s[2];
let x2 = endpt[0];
let y2 = endpt[1];
let bx;
let by;
if(first) {
ret[0] = Math.min(x1, x2);
ret[1] = Math.min(y1, y2);
ret[2] = Math.max(x1, x2);
ret[3] = Math.max(y1, y2);
} else {
ret[0] = Math.min(x1, x2, ret[0]);
ret[1] = Math.min(y1, y2, ret[1]);
ret[2] = Math.max(x1, x2, ret[2]);
ret[3] = Math.max(y1, y2, ret[3]);
}
first = false;
if(s[0] === GraphicsPath.QUAD) {
x2 = s[5];
y2 = s[6];
ax = x1 - 2 * s[3] + x2;
ay = y1 - 2 * s[4] + y2;
if(ax !== 0) {
GraphicsPath._accBounds(ret, s, (x1 - s[3]) / ax);
}
if(ay !== 0) {
GraphicsPath._accBounds(ret, s, (y1 - s[4]) / ay);
}
} else if(s[0] === GraphicsPath.CUBIC) {
x2 = s[7];
y2 = s[8];
const denomX = x1 - 3 * s[3] + 3 * s[5] - x2;
const denomY = y1 - 3 * s[4] + 3 * s[6] - y2;
if(denomX !== 0 || denomY !== 0) {
ax = x1 - 2 * s[3] + s[5];
ay = y1 - 2 * s[4] + s[6];
bx = s[3] * s[3] + s[5] * s[5] - s[5] * (x1 + s[3]) + x2 * (x1 - s[3]);
by = s[4] * s[4] + s[6] * s[6] - s[6] * (y1 + s[4]) + y2 * (y1 - s[4]);
if(bx >= 0 && denomX !== 0) {
bx = Math.sqrt(bx);
GraphicsPath._accBounds(ret, s, (ax - bx) / denomX);
GraphicsPath._accBounds(ret, s, (ax + bx) / denomX);
}
if(by >= 0 && denomY !== 0) {
by = Math.sqrt(by);
GraphicsPath._accBounds(ret, s, (ay - by) / denomY);
GraphicsPath._accBounds(ret, s, (ay + by) / denomY);
}
}
} else if(s[0] === GraphicsPath.ARC) {
const rx = s[3];
const ry = s[4];
const cx = s[10];
const cy = s[11];
const theta = s[12];
const delta = s[13];
const rot = s[5]; // Rotation in radians
let cosp;
let sinp;
if(Math.abs(delta - theta) >= Math.PI * 2) {
// This arc goes around the entire ellipse, giving
// it a much simpler formula for the bounding box
let distx;
let disty;
if(rx === ry) {
// The arc forms a circle
distx = rx;
disty = ry;
} else {
cosp = Math.cos(rot);
sinp = rot >= 0 && rot < 6.283185307179586 ? rot <= 3.141592653589793 ? Math.sqrt(1.0 - cosp * cosp) : -Math.sqrt(1.0 - cosp * cosp) : Math.sin(rot);
ax = cosp * rx;
ay = sinp * rx;
bx = -sinp * ry;
by = cosp * ry;
distx = Math.sqrt(ax * ax + bx * bx);
disty = Math.sqrt(ay * ay + by * by);
}
GraphicsPath._accBoundsPoint(ret, cx + distx, cy + disty);
GraphicsPath._accBoundsPoint(ret, cx + distx, cy - disty);
GraphicsPath._accBoundsPoint(ret, cx - distx, cy + disty);
GraphicsPath._accBoundsPoint(ret, cx - distx, cy - disty);
} else if(delta !== theta) { // NOTE: Endpoints were already included in case delta==theta
cosp = Math.cos(rot);
sinp = rot >= 0 && rot < 6.283185307179586 ? rot <= 3.141592653589793 ? Math.sqrt(1.0 - cosp * cosp) : -Math.sqrt(1.0 - cosp * cosp) : Math.sin(rot);
const angles = [];
let angle;
if(cosp !== 0 && sinp !== 0) {
angle = Math.atan2(-ry * sinp / cosp, rx);
angles.push(angle, angle + Math.PI);
angle = Math.atan2(ry * cosp / sinp, rx);
angles.push(angle, angle + Math.PI);
} else {
angles.push(0, Math.PI, Math.PI * 0.5, Math.PI * 1.5);
}
let k;
for (k = 0; k < angles.length; k++) {
if(GraphicsPath._angleInRange(angles[k], theta, delta)) {
GraphicsPath._accBoundsArc(ret, rx, ry, cosp, sinp, cx, cy, angles[k]);
}
}
}
}
}
return ret;
};
/**
* Returns a path that reverses the course of this path.
* @returns {GraphicsPath} A GraphicsPath
* object with its path segments reversed.
*/
GraphicsPath.prototype.reverse = function() {
let lastptx = 0;
let lastpty = 0;
let lastClosed = false;
let pathStartX = 0;
let pathStartY = 0;
const ret = new GraphicsPath();
let i;
for (i = this.segments.length - 1; i >= 0; i--) {
const s = this.segments[i];
let startpt = GraphicsPath._startPoint(s);
let endpt = GraphicsPath._endPoint(s);
if(s[0] !== GraphicsPath.CLOSE) {
if(i === this.segments.length - 1) {
ret.moveTo(endpt[0], endpt[1]);
} else if(lastptx !== endpt[0] || lastpty !== endpt[1]) {
if(lastClosed) {
ret.closePath();
}
lastClosed = false;
ret.moveTo(endpt[0], endpt[1]);
}
lastptx = startpt[0];
lastpty = startpt[1];
}
if(s[0] === GraphicsPath.CLOSE) {
if(lastClosed) {
ret.closePath();
}
lastClosed = true;
let havePathStart = false;
let j;
for (j = i - 1; j >= 0; j--) {
if(this.segments[j][0] === GraphicsPath.CLOSE) {
break;
}
startpt = GraphicsPath._startPoint(this.segments[j]);
endpt = GraphicsPath._endPoint(this.segments[j]);
if(havePathStart) {
if(pathStartX !== endpt[0] || pathStartY !== endpt[1]) {
break;
}
}
pathStartX = startpt[0];
pathStartY = startpt[1];
havePathStart = true;
}
if(havePathStart) {
ret.moveTo(pathStartX, pathStartY);
endpt = GraphicsPath._endPoint(this.segments[i - 1]);
if(pathStartX !== endpt[0] || pathStartY !== endpt[1]) {
ret.lineTo(endpt[0], endpt[1]);
}
lastptx = endpt[0];
lastpty = endpt[1];
}
} else if(s[0] === GraphicsPath.QUAD) {
ret.quadraticCurveTo(s[3], s[4], s[1], s[2]);
} else if(s[0] === GraphicsPath.CUBIC) {