-
-
Notifications
You must be signed in to change notification settings - Fork 103
/
path_intersection_test.go
1762 lines (1601 loc) · 96.9 KB
/
path_intersection_test.go
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
package canvas
import (
"fmt"
"math"
"testing"
"github.com/tdewolff/test"
)
func TestIntersectionRayCircle(t *testing.T) {
var tts = []struct {
l0, l1 Point
c Point
r float64
p0, p1 Point
}{
{Point{0.0, 0.0}, Point{0.0, 1.0}, Point{0.0, 0.0}, 2.0, Point{0.0, 2.0}, Point{0.0, -2.0}},
{Point{2.0, 0.0}, Point{2.0, 1.0}, Point{0.0, 0.0}, 2.0, Point{2.0, 0.0}, Point{2.0, 0.0}},
{Point{0.0, 2.0}, Point{1.0, 2.0}, Point{0.0, 0.0}, 2.0, Point{0.0, 2.0}, Point{0.0, 2.0}},
{Point{0.0, 3.0}, Point{1.0, 3.0}, Point{0.0, 0.0}, 2.0, Point{}, Point{}},
{Point{0.0, 1.0}, Point{0.0, 0.0}, Point{0.0, 0.0}, 2.0, Point{0.0, 2.0}, Point{0.0, -2.0}},
}
for i, tt := range tts {
t.Run(fmt.Sprint(i), func(t *testing.T) {
p0, p1, _ := intersectionRayCircle(tt.l0, tt.l1, tt.c, tt.r)
test.T(t, p0, tt.p0)
test.T(t, p1, tt.p1)
})
}
}
func TestIntersectionCircleCircle(t *testing.T) {
var tts = []struct {
c0 Point
r0 float64
c1 Point
r1 float64
p0, p1 Point
}{
{Point{0.0, 0.0}, 1.0, Point{2.0, 0.0}, 1.0, Point{1.0, 0.0}, Point{1.0, 0.0}},
{Point{0.0, 0.0}, 1.0, Point{1.0, 1.0}, 1.0, Point{1.0, 0.0}, Point{0.0, 1.0}},
{Point{0.0, 0.0}, 1.0, Point{3.0, 0.0}, 1.0, Point{}, Point{}},
{Point{0.0, 0.0}, 1.0, Point{0.0, 0.0}, 1.0, Point{}, Point{}},
{Point{0.0, 0.0}, 1.0, Point{0.5, 0.0}, 2.0, Point{}, Point{}},
}
for i, tt := range tts {
t.Run(fmt.Sprint(i), func(t *testing.T) {
p0, p1, _ := intersectionCircleCircle(tt.c0, tt.r0, tt.c1, tt.r1)
test.T(t, p0, tt.p0)
test.T(t, p1, tt.p1)
})
}
}
func TestIntersectionLineLine(t *testing.T) {
var tts = []struct {
line1, line2 string
zs Intersections
}{
// secant
{"M2 0L2 3", "M1 2L3 2", Intersections{
{Point{2.0, 2.0}, [2]float64{2.0 / 3.0, 0.5}, [2]float64{0.5 * math.Pi, 0.0}, false, false},
}},
// tangent
{"M2 0L2 3", "M2 2L3 2", Intersections{
{Point{2.0, 2.0}, [2]float64{2.0 / 3.0, 0.0}, [2]float64{0.5 * math.Pi, 0.0}, true, false},
}},
{"M2 0L2 2", "M2 2L3 2", Intersections{
{Point{2.0, 2.0}, [2]float64{1.0, 0.0}, [2]float64{0.5 * math.Pi, 0.0}, true, false},
}},
{"L2 2", "M0 4L2 2", Intersections{
{Point{2.0, 2.0}, [2]float64{1.0, 1.0}, [2]float64{0.25 * math.Pi, 1.75 * math.Pi}, true, false},
}},
{"L10 5", "M0 10L10 5", Intersections{
{Point{10.0, 5.0}, [2]float64{1.0, 1.0}, [2]float64{Point{2.0, 1.0}.Angle(), Point{2.0, -1.0}.Angle()}, true, false},
}},
{"M10 5L20 10", "M10 5L20 0", Intersections{
{Point{10.0, 5.0}, [2]float64{0.0, 0.0}, [2]float64{Point{2.0, 1.0}.Angle(), Point{2.0, -1.0}.Angle()}, true, false},
}},
// parallel
{"L2 2", "L2 2", Intersections{
{Point{0.0, 0.0}, [2]float64{0.0, 0.0}, [2]float64{0.25 * math.Pi, 0.25 * math.Pi}, true, true},
{Point{2.0, 2.0}, [2]float64{1.0, 1.0}, [2]float64{0.25 * math.Pi, 0.25 * math.Pi}, true, true},
}},
{"L2 2", "M2 2L0 0", Intersections{
{Point{0.0, 0.0}, [2]float64{0.0, 1.0}, [2]float64{0.25 * math.Pi, 1.25 * math.Pi}, true, true},
{Point{2.0, 2.0}, [2]float64{1.0, 0.0}, [2]float64{0.25 * math.Pi, 1.25 * math.Pi}, true, true},
}},
{"L2 2", "M3 3L5 5", Intersections{}},
{"L2 2", "M-1 1L1 3", Intersections{}},
{"L2 2", "M2 2L4 4", Intersections{
{Point{2.0, 2.0}, [2]float64{1.0, 0.0}, [2]float64{0.25 * math.Pi, 0.25 * math.Pi}, true, false},
}},
{"L2 2", "M-2 -2L0 0", Intersections{
{Point{0.0, 0.0}, [2]float64{0.0, 1.0}, [2]float64{0.25 * math.Pi, 0.25 * math.Pi}, true, false},
}},
{"L4 4", "M2 2L6 6", Intersections{
{Point{2.0, 2.0}, [2]float64{0.5, 0.0}, [2]float64{0.25 * math.Pi, 0.25 * math.Pi}, true, true},
{Point{4.0, 4.0}, [2]float64{1.0, 0.5}, [2]float64{0.25 * math.Pi, 0.25 * math.Pi}, true, true},
}},
{"L4 4", "M-2 -2L2 2", Intersections{
{Point{0.0, 0.0}, [2]float64{0.0, 0.5}, [2]float64{0.25 * math.Pi, 0.25 * math.Pi}, true, true},
{Point{2.0, 2.0}, [2]float64{0.5, 1.0}, [2]float64{0.25 * math.Pi, 0.25 * math.Pi}, true, true},
}},
// none
{"M2 0L2 1", "M3 0L3 1", Intersections{}},
{"M2 0L2 1", "M0 2L1 2", Intersections{}},
// bugs
{"M21.590990257669734 18.40900974233027L22.651650429449557 17.348349570550447", "M21.23743686707646 18.762563132923542L21.590990257669738 18.409009742330266", Intersections{
{Point{21.590990257669734, 18.40900974233027}, [2]float64{0.0, 1.0}, [2]float64{1.75 * math.Pi, 1.75 * math.Pi}, true, false},
}},
{"M-0.1997406229376793 296.9999999925494L-0.1997406229376793 158.88740153238177", "M-0.1997406229376793 158.88740153238177L-0.19974062293732664 158.8874019079834", Intersections{
{Point{-0.1997406229376793, 158.88740153238177}, [2]float64{1.0, 0.0}, [2]float64{1.5 * math.Pi, 89.9999462 * math.Pi / 180.0}, true, false},
//{Point{-0.1997406229376793, 158.8874019079834}, [2]float64{0.9999999973, 1.0}, [2]float64{1.5 * math.Pi, 89.9999462 * math.Pi / 180.0}, true, true},
}}, // #287
{"M-0.1997406229376793 296.9999999925494L-0.1997406229376793 158.88740153238177", "M-0.19974062293732664 158.8874019079834L-0.19999999999964735 20.77454766193328", Intersections{
{Point{-0.1997406229376793, 158.88740172019808}, [2]float64{0.9999999986, 1.359651238e-09}, [2]float64{270.0 * math.Pi / 180.0, 269.9998924 * math.Pi / 180.0}, false, false},
}}, // #287
{"M-0.1997406229376793 158.88740153238177L-0.19974062293732664 158.8874019079834", "M-0.19974062293732664 158.8874019079834L-0.19999999999964735 20.77454766193328", Intersections{
//{Point{-0.19974062293732664, 158.88740153238177}, [2]float64{0.0, 2.72e-9}, [2]float64{89.9999462 * math.Pi / 180.0, 269.9998924 * math.Pi / 180.0}, true, true},
{Point{-0.19974062293732664, 158.8874019079834}, [2]float64{1.0, 0.0}, [2]float64{89.9999462 * math.Pi / 180.0, 269.9998924 * math.Pi / 180.0}, true, false},
}}, // #287
{"M162.43449681368278 -9.999996185876771L162.43449681368278 -9.99998551284069", "M162.43449681368278 -9.999985512840682L162.2344968136828 -9.99998551284069", Intersections{
{Point{162.43449681368278, -9.99998551284069}, [2]float64{1.0, 0.0}, [2]float64{0.5 * math.Pi, math.Pi}, true, false},
}}, // #287
{"M0.7814861805182336,0.39875653588924026L0.7814851602550552,0.3987574923859699", "M0.7814852358775772,0.39875773815916654L0.7814861805182336,0.39875653588924026", Intersections{
{Point{0.7814861805182336, 0.39875653588924026}, [2]float64{0.0, 1.0}, [2]float64{136.84761026848332 * math.Pi / 180.0, 308.15722658736905 * math.Pi / 180.0}, true, false},
//{Point{0.7814852358775772, 0.39875773815916654}, [2]float64{0.07, 0.0}, [2]float64{math.Pi, 0.0}, true, true},
}},
}
for _, tt := range tts {
t.Run(fmt.Sprint(tt.line1, "x", tt.line2), func(t *testing.T) {
line1 := MustParseSVGPath(tt.line1).ReverseScanner()
line2 := MustParseSVGPath(tt.line2).ReverseScanner()
line1.Scan()
line2.Scan()
zs := intersectionLineLine(nil, line1.Start(), line1.End(), line2.Start(), line2.End())
test.T(t, zs, tt.zs)
})
}
}
func isIncreasing(a, b Point) bool {
if b.X < a.X {
return false
} else if a.X == b.X && b.Y < a.Y {
return false
}
return true
}
func TestIntersectionLineLineBentleyOttmann(t *testing.T) {
var tts = []struct {
line1, line2 string
zs []Point
}{
// secant
{"M2 0L2 3", "M1 2L3 2", []Point{{2.0, 2.0}}},
// tangent
{"M2 0L2 3", "M2 2L3 2", []Point{{2.0, 2.0}}},
{"M2 0L2 2", "M2 2L3 2", nil},
{"L2 2", "M0 4L2 2", []Point{{2.0, 2.0}}},
{"L10 5", "M0 10L10 5", []Point{{10.0, 5.0}}},
{"M10 5L20 10", "M10 5L20 0", []Point{{10.0, 5.0}}},
// parallel
{"L2 2", "L2 2", nil},
{"L2 2", "M3 3L5 5", nil},
{"L2 2", "M-3 -3L-1 -1", nil},
{"L2 2", "M-1 1L1 3", nil},
{"L2 2", "M2 2L4 4", nil},
{"L2 2", "M-2 -2L0 0", nil},
{"L4 4", "M2 2L4 4", []Point{{2.0, 2.0}}},
{"L4 4", "M2 2L6 6", []Point{{2.0, 2.0}, Point{4.0, 4.0}}},
{"L4 4", "M0 0L2 2", []Point{{2.0, 2.0}}},
{"L4 4", "M0 0L6 6", []Point{{4.0, 4.0}}},
{"L4 4", "M-2 -2L2 2", []Point{{0.0, 0.0}, {2.0, 2.0}}},
{"L4 4", "M-2 -2L4 4", []Point{{0.0, 0.0}}},
{"L4 4", "M1 1L3 3", []Point{{1.0, 1.0}, Point{3.0, 3.0}}},
{"L4 4", "M-1 -1L5 5", []Point{{0.0, 0.0}, Point{4.0, 4.0}}},
// none
{"M2 0L2 1", "M3 0L3 1", nil},
{"M2 0L2 1", "M0 2L1 2", nil},
// almost vertical
{"L2 0", "M1 -1L1.0000000000000002 2", []Point{{1.0000000000000002, 0.0}}},
{"L2 0", "M1 1L1.0000000000000002 -2", []Point{{1.0000000000000002, 0.0}}},
{"M1 -1L1.0000000000000002 2", "L2 0", []Point{{1.0000000000000002, 0.0}}},
{"M1 1L1.0000000000000002 -2", "L2 0", []Point{{1.0000000000000002, 0.0}}},
// bugs
{"M21.590990257669734 18.40900974233027L22.651650429449557 17.348349570550447", "M21.23743686707646 18.762563132923542L21.590990257669738 18.409009742330266", nil}, // almost colinear
{"M-0.1997406229376793 158.88740153238177L-0.1997406229376793 296.9999999925494", "M-0.1997406229376793 158.88740153238177L-0.19974062293732664 158.8874019079834", []Point{{-0.1997406229376793, 158.88740153238177}}}, // #287
{"M-0.1997406229376793 158.88740153238177L-0.1997406229376793 296.9999999925494", "M-0.19999999999964735 20.77454766193328L-0.19974062293732664 158.8874019079834", []Point{{-0.1997406229376793, 158.88740172019808}}}, // #287
{"M-0.1997406229376793 158.88740153238177L-0.19974062293732664 158.8874019079834", "M-0.19999999999964735 20.77454766193328L-0.19974062293732664 158.8874019079834", nil}, // almost colinear, #287
{"M162.43449681368278 -9.999996185876771L162.43449681368278 -9.99998551284069", "M162.2344968136828 -9.99998551284069L162.43449681368278 -9.999985512840682", nil}, // almost colinear, #287
{"M0.7814851602550552,0.3987574923859699L0.7814861805182336,0.39875653588924026", "M0.7814852358775772,0.39875773815916654L0.7814861805182336,0.39875653588924026", nil}, // almost colinear
{"M0.6187340865555582,7.030136875251485L0.6203785454666688,7.0296030922171955", "M0.6187340865555582,7.030136875251485L0.6189178552813777,7.03007722485377", []Point{{0.6187340865555582, 7.030136875251485}}}, // colinear
}
for _, tt := range tts {
t.Run(fmt.Sprint(tt.line1, "x", tt.line2), func(t *testing.T) {
line1 := MustParseSVGPath(tt.line1).ReverseScanner()
line2 := MustParseSVGPath(tt.line2).ReverseScanner()
line1.Scan()
line2.Scan()
if !isIncreasing(line1.Start(), line1.End()) || !isIncreasing(line2.Start(), line2.End()) {
t.Fatal("bad test: lines not increasing")
}
zs := intersectionLineLineBentleyOttmann(nil, line1.Start(), line1.End(), line2.Start(), line2.End())
test.T(t, zs, tt.zs)
})
}
}
func TestIntersectionLineQuad(t *testing.T) {
var tts = []struct {
line, quad string
zs Intersections
}{
// secant
{"M0 5L10 5", "Q10 5 0 10", Intersections{
{Point{5.0, 5.0}, [2]float64{0.5, 0.5}, [2]float64{0.0, 0.5 * math.Pi}, false, false},
}},
// tangent
{"L0 10", "Q10 5 0 10", Intersections{
{Point{0.0, 0.0}, [2]float64{0.0, 0.0}, [2]float64{0.5 * math.Pi, Point{2.0, 1.0}.Angle()}, true, false},
{Point{0.0, 10.0}, [2]float64{1.0, 1.0}, [2]float64{0.5 * math.Pi, Point{-2.0, 1.0}.Angle()}, true, false},
}},
{"M5 0L5 10", "Q10 5 0 10", Intersections{
{Point{5.0, 5.0}, [2]float64{0.5, 0.5}, [2]float64{0.5 * math.Pi, 0.5 * math.Pi}, true, false},
}},
// none
{"M-1 0L-1 10", "Q10 5 0 10", Intersections{}},
}
origEpsilon := Epsilon
for _, tt := range tts {
t.Run(fmt.Sprint(tt.line, "x", tt.quad), func(t *testing.T) {
Epsilon = origEpsilon
line := MustParseSVGPath(tt.line).ReverseScanner()
quad := MustParseSVGPath(tt.quad).ReverseScanner()
line.Scan()
quad.Scan()
zs := intersectionLineQuad(nil, line.Start(), line.End(), quad.Start(), quad.CP1(), quad.End())
Epsilon = 3.0 * origEpsilon
test.T(t, zs, tt.zs)
})
}
Epsilon = origEpsilon
}
func TestIntersectionLineCube(t *testing.T) {
var tts = []struct {
line, cube string
zs Intersections
}{
// secant
{"M0 5L10 5", "C8 0 8 10 0 10", Intersections{
{Point{6.0, 5.0}, [2]float64{0.6, 0.5}, [2]float64{0.0, 0.5 * math.Pi}, false, false},
}},
{"M0 1L1 1", "C0 2 1 0 1 2", Intersections{ // parallel at intersection
{Point{0.5, 1.0}, [2]float64{0.5, 0.5}, [2]float64{0.0, 0.0}, false, false},
}},
{"M0 1L1 1", "M0 2C0 0 1 2 1 0", Intersections{ // parallel at intersection
{Point{0.5, 1.0}, [2]float64{0.5, 0.5}, [2]float64{0.0, 0.0}, false, false},
}},
{"M0 1L1 1", "C0 3 1 -1 1 2", Intersections{ // three intersections
{Point{0.0791512117, 1.0}, [2]float64{0.0791512117, 0.1726731646}, [2]float64{0.0, 74.05460410 / 180.0 * math.Pi}, false, false},
{Point{0.5, 1.0}, [2]float64{0.5, 0.5}, [2]float64{0.0, 315 / 180.0 * math.Pi}, false, false},
{Point{0.9208487883, 1.0}, [2]float64{0.9208487883, 0.8273268354}, [2]float64{0.0, 74.05460410 / 180.0 * math.Pi}, false, false},
}},
// tangent
{"L0 10", "C8 0 8 10 0 10", Intersections{
{Point{0.0, 0.0}, [2]float64{0.0, 0.0}, [2]float64{0.5 * math.Pi, 0.0}, true, false},
{Point{0.0, 10.0}, [2]float64{1.0, 1.0}, [2]float64{0.5 * math.Pi, math.Pi}, true, false},
}},
{"M6 0L6 10", "C8 0 8 10 0 10", Intersections{
{Point{6.0, 5.0}, [2]float64{0.5, 0.5}, [2]float64{0.5 * math.Pi, 0.5 * math.Pi}, true, false},
}},
// none
{"M-1 0L-1 10", "C8 0 8 10 0 10", Intersections{}},
}
origEpsilon := Epsilon
for _, tt := range tts {
t.Run(fmt.Sprint(tt.line, "x", tt.cube), func(t *testing.T) {
Epsilon = origEpsilon
line := MustParseSVGPath(tt.line).ReverseScanner()
cube := MustParseSVGPath(tt.cube).ReverseScanner()
line.Scan()
cube.Scan()
zs := intersectionLineCube(nil, line.Start(), line.End(), cube.Start(), cube.CP1(), cube.CP2(), cube.End())
Epsilon = 3.0 * origEpsilon
test.T(t, zs, tt.zs)
})
}
Epsilon = origEpsilon
}
func TestIntersectionLineEllipse(t *testing.T) {
var tts = []struct {
line, arc string
zs Intersections
}{
// secant
{"M0 5L10 5", "A5 5 0 0 1 0 10", Intersections{
{Point{5.0, 5.0}, [2]float64{0.5, 0.5}, [2]float64{0.0, 0.5 * math.Pi}, false, false},
}},
{"M0 5L10 5", "A5 5 0 1 1 0 10", Intersections{
{Point{5.0, 5.0}, [2]float64{0.5, 0.5}, [2]float64{0.0, 0.5 * math.Pi}, false, false},
}},
{"M0 5L-10 5", "A5 5 0 0 0 0 10", Intersections{
{Point{-5.0, 5.0}, [2]float64{0.5, 0.5}, [2]float64{math.Pi, 0.5 * math.Pi}, false, false},
}},
{"M-5 0L-5 -10", "A5 5 0 0 0 -10 0", Intersections{
{Point{-5.0, -5.0}, [2]float64{0.5, 0.5}, [2]float64{1.5 * math.Pi, math.Pi}, false, false},
}},
{"M0 10L10 10", "A10 5 90 0 1 0 20", Intersections{
{Point{5.0, 10.0}, [2]float64{0.5, 0.5}, [2]float64{0.0, 0.5 * math.Pi}, false, false},
}},
// tangent
{"M-5 0L-15 0", "A5 5 0 0 0 -10 0", Intersections{
{Point{-10.0, 0.0}, [2]float64{0.5, 1.0}, [2]float64{math.Pi, 0.5 * math.Pi}, true, false},
}},
{"M-5 0L-15 0", "A5 5 0 0 1 -10 0", Intersections{
{Point{-10.0, 0.0}, [2]float64{0.5, 1.0}, [2]float64{math.Pi, 1.5 * math.Pi}, true, false},
}},
{"L0 10", "A10 5 0 0 1 0 10", Intersections{
{Point{0.0, 0.0}, [2]float64{0.0, 0.0}, [2]float64{0.5 * math.Pi, 0.0}, true, false},
{Point{0.0, 10.0}, [2]float64{1.0, 1.0}, [2]float64{0.5 * math.Pi, math.Pi}, true, false},
}},
{"M5 0L5 10", "A5 5 0 0 1 0 10", Intersections{
{Point{5.0, 5.0}, [2]float64{0.5, 0.5}, [2]float64{0.5 * math.Pi, 0.5 * math.Pi}, true, false},
}},
{"M-5 0L-5 10", "A5 5 0 0 0 0 10", Intersections{
{Point{-5.0, 5.0}, [2]float64{0.5, 0.5}, [2]float64{0.5 * math.Pi, 0.5 * math.Pi}, true, false},
}},
{"M5 0L5 20", "A10 5 90 0 1 0 20", Intersections{
{Point{5.0, 10.0}, [2]float64{0.5, 0.5}, [2]float64{0.5 * math.Pi, 0.5 * math.Pi}, true, false},
}},
{"M4 3L0 3", "M2 3A1 1 0 0 0 4 3", Intersections{
{Point{4.0, 3.0}, [2]float64{0.0, 1.0}, [2]float64{math.Pi, 1.5 * math.Pi}, true, false},
{Point{2.0, 3.0}, [2]float64{0.5, 0.0}, [2]float64{math.Pi, 0.5 * math.Pi}, true, false},
}},
// none
{"M6 0L6 10", "A5 5 0 0 1 0 10", Intersections{}},
{"M10 5L15 5", "A5 5 0 0 1 0 10", Intersections{}},
{"M6 0L6 20", "A10 5 90 0 1 0 20", Intersections{}},
// bugs
{"M0 -0.7L1 -0.7", "M-0.7 0A0.7 0.7 0 0 1 0.7 0", Intersections{
{Point{0.0, -0.7}, [2]float64{0.0, 0.5}, [2]float64{0.0, 0.0}, true, false},
}}, // #200, at intersection the arc angle is deviated towards positive angle
{"M30.23402723090112,37.620459766287226L30.170131507649785,37.66143576791836", "M30.242341004748596 37.609669236818846A0.8700000000000001 0.8700000000000001 0 0 1 28.82999999999447 36.9294", Intersections{
{Point{30.170131507649785, 37.66143576791836}, [2]float64{1.0, 0.04553266140095003}, [2]float64{2.571361371137828, 2.570702752627385}, true, false},
}}, // #280
{"M30.23402723090112,37.620459766287226L30.170131507649785,37.66143576791836", "M30.170131507649785 37.66143576791836A0.8700000000002787 0.8700000000002787 0 0 1 28.82999999999447 36.92939999999941", Intersections{
{Point{30.170131507649785, 37.66143576791836}, [2]float64{1.0, 0.0}, [2]float64{2.571361371137828, 2.570702753023132}, true, false},
}}, // #280
{"M18.28586369751671 1.9033410129748447L18.285524146153797 1.9012793871179001", "M18.285524146153797 1.9012793871179001A0.09877777777777778 0.09877777777777778 0 0 0 18.188037109374996 1.8184178602430556", Intersections{
{Point{18.285524146153797, 1.9012793871179001}, [2]float64{1.0, 0}, [2]float64{4.549153676432458, 4.550551548951796}, true, false},
}}, // in preview
{"M32761.5,32383.691L32761.52,32383.691", "M31511.49999999751,33633.691 A1250 1250 0 0 1 32761.50000000074,32383.691", Intersections{
{Point{32761.5, 32383.691}, [2]float64{0.0, 1.0}, [2]float64{0.0, 0.0}, true, false},
}}, // #293
{"M73643.30051730774,34889.01290159931L73639.44503270132,34889.13797316706", "M73639.44503270132,34889.13797316706A1250 1250 0 0 1 73599.5139303418 34889.76998615123", Intersections{
{Point{73639.44503270132, 34889.13797316706}, [2]float64{1.0, 0.0}, [2]float64{3.109164117290831, 3.1097912676269237}, true, false},
}}, // #293
}
origEpsilon := Epsilon
for _, tt := range tts {
t.Run(fmt.Sprint(tt.line, "x", tt.arc), func(t *testing.T) {
Epsilon = origEpsilon
line := MustParseSVGPath(tt.line).ReverseScanner()
arc := MustParseSVGPath(tt.arc).ReverseScanner()
line.Scan()
arc.Scan()
rx, ry, rot, large, sweep := arc.Arc()
phi := rot * math.Pi / 180.0
cx, cy, theta0, theta1 := ellipseToCenter(arc.Start().X, arc.Start().Y, rx, ry, phi, large, sweep, arc.End().X, arc.End().Y)
zs := intersectionLineEllipse(nil, line.Start(), line.End(), Point{cx, cy}, Point{rx, ry}, phi, theta0, theta1)
Epsilon = 3.0 * origEpsilon
test.T(t, zs, tt.zs)
})
}
Epsilon = origEpsilon
}
func TestIntersectionEllipseEllipse(t *testing.T) {
var tts = []struct {
arc, arc2 string
zs Intersections
}{
// secant
{"M5 0A5 5 0 0 1 -5 0", "M-10 -5A10 10 0 0 1 -10 15", Intersections{
{Point{0.0, 5.0}, [2]float64{0.5, 0.5}, [2]float64{math.Pi, 0.5 * math.Pi}, false, false},
}},
// tangent
{"A5 5 0 0 1 0 10", "M10 0A5 5 0 0 0 10 10", Intersections{
{Point{5.0, 5.0}, [2]float64{0.5, 0.5}, [2]float64{0.5 * math.Pi, 0.5 * math.Pi}, true, false},
}},
// fully same
{"A5 5 0 0 1 0 10", "A5 5 0 0 1 0 10", Intersections{
{Point{0.0, 0.0}, [2]float64{0.0, 0.0}, [2]float64{0.0, 0.0}, true, true},
{Point{0.0, 10.0}, [2]float64{1.0, 1.0}, [2]float64{math.Pi, math.Pi}, true, true},
}},
{"A5 5 0 0 1 0 10", "M0 10A5 5 0 0 0 0 0", Intersections{
{Point{0.0, 0.0}, [2]float64{0.0, 1.0}, [2]float64{0.0, math.Pi}, true, true},
{Point{0.0, 10.0}, [2]float64{1.0, 0.0}, [2]float64{math.Pi, 0.0}, true, true},
}},
// partly same
{"A5 5 0 0 1 0 10", "A5 5 0 0 1 5 5", Intersections{
{Point{0.0, 0.0}, [2]float64{0.0, 0.0}, [2]float64{0.0, 0.0}, true, true},
{Point{5.0, 5.0}, [2]float64{0.5, 1.0}, [2]float64{0.5 * math.Pi, 0.5 * math.Pi}, true, true},
}},
{"A5 5 0 0 1 0 10", "M5 5A5 5 0 0 1 0 10", Intersections{
{Point{5.0, 5.0}, [2]float64{0.5, 0.0}, [2]float64{0.5 * math.Pi, 0.5 * math.Pi}, true, true},
{Point{0.0, 10.0}, [2]float64{1.0, 1.0}, [2]float64{math.Pi, math.Pi}, true, true},
}},
{"A5 5 0 0 1 0 10", "M5 5A5 5 0 0 1 -5 5", Intersections{
{Point{5.0, 5.0}, [2]float64{0.5, 0.0}, [2]float64{0.5 * math.Pi, 0.5 * math.Pi}, true, true},
{Point{0.0, 10.0}, [2]float64{1.0, 0.5}, [2]float64{math.Pi, math.Pi}, true, true},
}},
{"M30.170131507649785 37.66143576791836A0.8700000000002787 0.8700000000002787 0 0 1 28.82999999999447 36.92939999999941", "M30.242341004748596 37.609669236818846A0.8700000000000001 0.8700000000000001 0 0 1 28.82999999999447 36.9294", Intersections{
{Point{30.170131507649785, 37.66143576791836}, [2]float64{0.0, 0.0455326614}, [2]float64{2.5707027528269983, 2.5707027528269983}, true, true},
{Point{28.82999999999447, 36.92939999999941}, [2]float64{1.0, 1.0}, [2]float64{1.5 * math.Pi, 1.5 * math.Pi}, true, true},
}}, // #280
}
origEpsilon := Epsilon
for _, tt := range tts {
t.Run(fmt.Sprint(tt.arc, "x", tt.arc2), func(t *testing.T) {
Epsilon = origEpsilon
arc := MustParseSVGPath(tt.arc).ReverseScanner()
arc2 := MustParseSVGPath(tt.arc2).ReverseScanner()
arc.Scan()
arc2.Scan()
rx, ry, rot, large, sweep := arc.Arc()
phi := rot * math.Pi / 180.0
cx, cy, theta0, theta1 := ellipseToCenter(arc.Start().X, arc.Start().Y, rx, ry, phi, large, sweep, arc.End().X, arc.End().Y)
rx2, ry2, rot2, large2, sweep2 := arc2.Arc()
phi2 := rot2 * math.Pi / 180.0
cx2, cy2, theta20, theta21 := ellipseToCenter(arc2.Start().X, arc2.Start().Y, rx2, ry2, phi2, large2, sweep2, arc2.End().X, arc2.End().Y)
zs := intersectionEllipseEllipse(nil, Point{cx, cy}, Point{rx, ry}, phi, theta0, theta1, Point{cx2, cy2}, Point{rx2, ry2}, phi2, theta20, theta21)
Epsilon = 3.0 * origEpsilon
test.T(t, zs, tt.zs)
})
}
Epsilon = origEpsilon
}
/*func TestIntersections(t *testing.T) {
var tts = []struct {
p, q string
zp, zq []PathIntersection
}{
{"L10 0L5 10z", "M0 5L10 5L5 15z", []PathIntersection{
{Point{7.5, 5.0}, 2, 0.5, Point{-1.0, 2.0}.Angle(), true, false, false},
{Point{2.5, 5.0}, 3, 0.5, Point{-1.0, -2.0}.Angle(), false, false, false},
}, []PathIntersection{
{Point{7.5, 5.0}, 1, 0.75, 0.0, false, false, false},
{Point{2.5, 5.0}, 1, 0.25, 0.0, true, false, false},
}},
{"L10 0L5 10z", "M0 -5L10 -5A5 5 0 0 1 0 -5", []PathIntersection{
{Point{5.0, 0.0}, 1, 0.5, 0.0, false, true, false},
}, []PathIntersection{
{Point{5.0, 0.0}, 2, 0.5, math.Pi, false, true, false},
}},
{"M5 5L0 0", "M-5 0A5 5 0 0 0 5 0", []PathIntersection{
{Point{5.0 / math.Sqrt(2.0), 5.0 / math.Sqrt(2.0)}, 1, 0.292893219, 1.25 * math.Pi, false, false, false},
}, []PathIntersection{
{Point{5.0 / math.Sqrt(2.0), 5.0 / math.Sqrt(2.0)}, 1, 0.75, 1.75 * math.Pi, true, false, false},
}},
// intersection on one segment endpoint
{"L0 15", "M5 0L0 5L5 5", []PathIntersection{
{Point{0.0, 5.0}, 1, 1.0 / 3.0, 0.5 * math.Pi, true, true, false},
}, []PathIntersection{
{Point{0.0, 5.0}, 2, 0.0, 0.0, false, true, false},
}},
{"L0 15", "M5 0L0 5L-5 5", []PathIntersection{
{Point{0.0, 5.0}, 1, 1.0 / 3.0, 0.5 * math.Pi, false, false, false},
}, []PathIntersection{
{Point{0.0, 5.0}, 2, 0.0, math.Pi, true, false, false},
}},
{"L0 15", "M5 5L0 5L5 0", []PathIntersection{
{Point{0.0, 5.0}, 1, 1.0 / 3.0, 0.5 * math.Pi, false, true, false},
}, []PathIntersection{
{Point{0.0, 5.0}, 2, 0.0, 1.75 * math.Pi, false, true, false},
}},
{"L0 15", "M-5 5L0 5L5 0", []PathIntersection{
{Point{0.0, 5.0}, 1, 1.0 / 3.0, 0.5 * math.Pi, true, false, false},
}, []PathIntersection{
{Point{0.0, 5.0}, 2, 0.0, 1.75 * math.Pi, false, false, false},
}},
{"M5 0L0 5L5 5", "L0 15", []PathIntersection{
{Point{0.0, 5.0}, 2, 0.0, 0.0, false, true, false},
}, []PathIntersection{
{Point{0.0, 5.0}, 1, 1.0 / 3.0, 0.5 * math.Pi, true, true, false},
}},
{"M5 0L0 5L-5 5", "L0 15", []PathIntersection{
{Point{0.0, 5.0}, 2, 0.0, math.Pi, true, false, false},
}, []PathIntersection{
{Point{0.0, 5.0}, 1, 1.0 / 3.0, 0.5 * math.Pi, false, false, false},
}},
{"M5 5L0 5L5 0", "L0 15", []PathIntersection{
{Point{0.0, 5.0}, 2, 0.0, 1.75 * math.Pi, false, true, false},
}, []PathIntersection{
{Point{0.0, 5.0}, 1, 1.0 / 3.0, 0.5 * math.Pi, false, true, false},
}},
{"M-5 5L0 5L5 0", "L0 15", []PathIntersection{
{Point{0.0, 5.0}, 2, 0.0, 1.75 * math.Pi, false, false, false},
}, []PathIntersection{
{Point{0.0, 5.0}, 1, 1.0 / 3.0, 0.5 * math.Pi, true, false, false},
}},
{"L0 10", "M5 0A5 5 0 0 0 0 5A5 5 0 0 0 5 10", []PathIntersection{
{Point{0.0, 5.0}, 1, 0.5, 0.5 * math.Pi, true, true, false},
}, []PathIntersection{
{Point{0.0, 5.0}, 2, 0.0, 0.5 * math.Pi, false, true, false},
}},
{"L0 10", "M5 10A5 5 0 0 1 0 5A5 5 0 0 1 5 0", []PathIntersection{
{Point{0.0, 5.0}, 1, 0.5, 0.5 * math.Pi, false, true, false},
}, []PathIntersection{
{Point{0.0, 5.0}, 2, 0.0, 1.5 * math.Pi, false, true, false},
}},
{"L0 5L5 5", "M5 0A5 5 0 0 0 5 10", []PathIntersection{
{Point{0.0, 5.0}, 2, 0.0, 0.0, false, false, false},
}, []PathIntersection{
{Point{0.0, 5.0}, 1, 0.5, 0.5 * math.Pi, true, false, false},
}},
{"L0 5L5 5", "M5 10A5 5 0 0 1 5 0", []PathIntersection{
{Point{0.0, 5.0}, 2, 0.0, 0.0, true, false, false},
}, []PathIntersection{
{Point{0.0, 5.0}, 1, 0.5, 1.5 * math.Pi, false, false, false},
}},
// intersection on two segment endpoint
{"L10 6L20 0", "M0 10L10 6L20 10", []PathIntersection{
{Point{10.0, 6.0}, 2, 0.0, Point{10.0, -6.0}.Angle(), false, true, false},
}, []PathIntersection{
{Point{10.0, 6.0}, 2, 0.0, Point{10.0, 4.0}.Angle(), true, true, false},
}},
{"L10 6L20 0", "M20 10L10 6L0 10", []PathIntersection{
{Point{10.0, 6.0}, 2, 0.0, Point{10.0, -6.0}.Angle(), true, true, false},
}, []PathIntersection{
{Point{10.0, 6.0}, 2, 0.0, Point{-10.0, 4.0}.Angle(), true, true, false},
}},
{"M20 0L10 6L0 0", "M0 10L10 6L20 10", []PathIntersection{
{Point{10.0, 6.0}, 2, 0.0, Point{-10.0, -6.0}.Angle(), false, true, false},
}, []PathIntersection{
{Point{10.0, 6.0}, 2, 0.0, Point{10.0, 4.0}.Angle(), false, true, false},
}},
{"M20 0L10 6L0 0", "M20 10L10 6L0 10", []PathIntersection{
{Point{10.0, 6.0}, 2, 0.0, Point{-10.0, -6.0}.Angle(), true, true, false},
}, []PathIntersection{
{Point{10.0, 6.0}, 2, 0.0, Point{-10.0, 4.0}.Angle(), false, true, false},
}},
{"L10 6L20 10", "M0 10L10 6L20 0", []PathIntersection{
{Point{10.0, 6.0}, 2, 0.0, Point{10.0, 4.0}.Angle(), true, false, false},
}, []PathIntersection{
{Point{10.0, 6.0}, 2, 0.0, Point{10.0, -6.0}.Angle(), false, false, false},
}},
{"L10 6L20 10", "M20 0L10 6L0 10", []PathIntersection{
{Point{10.0, 6.0}, 2, 0.0, Point{10.0, 4.0}.Angle(), false, false, false},
}, []PathIntersection{
{Point{10.0, 6.0}, 2, 0.0, Point{-10.0, 4.0}.Angle(), true, false, false},
}},
{"M20 10L10 6L0 0", "M0 10L10 6L20 0", []PathIntersection{
{Point{10.0, 6.0}, 2, 0.0, Point{-10.0, -6.0}.Angle(), false, false, false},
}, []PathIntersection{
{Point{10.0, 6.0}, 2, 0.0, Point{10.0, -6.0}.Angle(), true, false, false},
}},
{"M20 10L10 6L0 0", "M20 0L10 6L0 10", []PathIntersection{
{Point{10.0, 6.0}, 2, 0.0, Point{-10.0, -6.0}.Angle(), true, false, false},
}, []PathIntersection{
{Point{10.0, 6.0}, 2, 0.0, Point{-10.0, 4.0}.Angle(), false, false, false},
}},
{"M4 1L4 3L0 3", "M3 4L4 3L3 2", []PathIntersection{
{Point{4.0, 3.0}, 2, 0.0, math.Pi, false, false, false},
}, []PathIntersection{
{Point{4.0, 3.0}, 2, 0.0, 1.25 * math.Pi, true, false, false},
}},
{"M0 1L4 1L4 3L0 3z", MustParseSVGPath("M4 3A1 1 0 0 0 2 3A1 1 0 0 0 4 3z").Flatten(Tolerance).ToSVG(), []PathIntersection{
{Point{4.0, 3.0}, 3, 0.0, math.Pi, false, false, false},
{Point{2.0, 3.0}, 3, 0.5, math.Pi, true, false, false},
}, []PathIntersection{
{Point{4.0, 3.0}, 1, 0.0, 262.83296263 * math.Pi / 180.0, true, false, false},
{Point{2.0, 3.0}, 10, 0.0, 82.83296263 * math.Pi / 180.0, false, false, false},
}},
{"M5 1L9 1L9 5L5 5z", MustParseSVGPath("M9 5A4 4 0 0 1 1 5A4 4 0 0 1 9 5z").Flatten(Tolerance).ToSVG(), []PathIntersection{
{Point{9.0, 5.0}, 3, 0.0, math.Pi, true, false, false},
{Point{5.0, 1.00828530}, 4, 0.997928675, 1.5 * math.Pi, false, false, false},
}, []PathIntersection{
{Point{9.0, 5.0}, 1, 0.0, 93.76219714 * math.Pi / 180.0, false, false, false},
{Point{5.0, 1.00828530}, 26, 0.5, 0.0, true, false, false},
}},
// touches / same
{"L2 0L2 2L0 2z", "M2 0L4 0L4 2L2 2z", []PathIntersection{
{Point{2.0, 0.0}, 2, 0.0, 0.5 * math.Pi, false, true, true},
{Point{2.0, 2.0}, 3, 0.0, math.Pi, false, true, false},
}, []PathIntersection{
{Point{2.0, 0.0}, 1, 0.0, 0.0, false, true, false},
{Point{2.0, 2.0}, 4, 0.0, 1.5 * math.Pi, false, true, true},
}},
{"L2 0L2 2L0 2z", "M2 0L2 2L4 2L4 0z", []PathIntersection{
{Point{2.0, 0.0}, 2, 0.0, 0.5 * math.Pi, true, true, true},
{Point{2.0, 2.0}, 3, 0.0, math.Pi, true, true, false},
}, []PathIntersection{
{Point{2.0, 0.0}, 1, 0.0, 0.5 * math.Pi, false, true, true},
{Point{2.0, 2.0}, 2, 0.0, 0.0, false, true, false},
}},
{"M2 0L4 0L4 2L2 2z", "L2 0L2 2L0 2z", []PathIntersection{
{Point{2.0, 0.0}, 1, 0.0, 0.0, false, true, false},
{Point{2.0, 2.0}, 4, 0.0, 1.5 * math.Pi, false, true, true},
}, []PathIntersection{
{Point{2.0, 0.0}, 2, 0.0, 0.5 * math.Pi, false, true, true},
{Point{2.0, 2.0}, 3, 0.0, math.Pi, false, true, false},
}},
{"L2 0L2 2L0 2z", "M2 1L4 1L4 3L2 3z", []PathIntersection{
{Point{2.0, 1.0}, 2, 0.5, 0.5 * math.Pi, false, true, true},
{Point{2.0, 2.0}, 3, 0.0, math.Pi, false, true, false},
}, []PathIntersection{
{Point{2.0, 1.0}, 1, 0.0, 0.0, false, true, false},
{Point{2.0, 2.0}, 4, 0.5, 1.5 * math.Pi, false, true, true},
}},
{"L2 0L2 2L0 2z", "M2 -1L4 -1L4 1L2 1z", []PathIntersection{
{Point{2.0, 0.0}, 2, 0.0, 0.5 * math.Pi, false, true, true},
{Point{2.0, 1.0}, 2, 0.5, 0.5 * math.Pi, false, true, false},
}, []PathIntersection{
{Point{2.0, 0.0}, 4, 0.5, 1.5 * math.Pi, false, true, false},
{Point{2.0, 1.0}, 4, 0.0, 1.5 * math.Pi, false, true, true},
}},
{"L2 0L2 2L0 2z", "M2 -1L4 -1L4 3L2 3z", []PathIntersection{
{Point{2.0, 0.0}, 2, 0.0, 0.5 * math.Pi, false, true, true},
{Point{2.0, 2.0}, 3, 0.0, math.Pi, false, true, false},
}, []PathIntersection{
{Point{2.0, 0.0}, 4, 0.75, 1.5 * math.Pi, false, true, false},
{Point{2.0, 2.0}, 4, 0.25, 1.5 * math.Pi, false, true, true},
}},
{"M0 -1L2 -1L2 3L0 3z", "M2 0L4 0L4 2L2 2z", []PathIntersection{
{Point{2.0, 0.0}, 2, 0.25, 0.5 * math.Pi, false, true, true},
{Point{2.0, 2.0}, 2, 0.75, 0.5 * math.Pi, false, true, false},
}, []PathIntersection{
{Point{2.0, 0.0}, 1, 0.0, 0.0, false, true, false},
{Point{2.0, 2.0}, 4, 0.0, 1.5 * math.Pi, false, true, true},
}},
{"L1 0L1 1zM2 0L1.9 1L1.9 -1z", "L1 0L1 -1zM2 0L1.9 1L1.9 -1z", []PathIntersection{
{Point{0.0, 0.0}, 1, 0.0, 0.0, true, true, true},
{Point{1.0, 0.0}, 2, 0.0, 0.5 * math.Pi, true, true, false},
}, []PathIntersection{
{Point{0.0, 0.0}, 1, 0.0, 0.0, false, true, true},
{Point{1.0, 0.0}, 2, 0.0, 1.5 * math.Pi, false, true, false},
}},
// head-on collisions
{"M2 0L2 2L0 2", "M4 2L2 2L2 4", []PathIntersection{
{Point{2.0, 2.0}, 2, 0.0, math.Pi, true, true, false},
}, []PathIntersection{
{Point{2.0, 2.0}, 2, 0.0, 0.5 * math.Pi, false, true, false},
}},
{"M0 2Q2 4 2 2Q4 2 2 4", "M2 4L2 2L4 2", []PathIntersection{
{Point{2.0, 2.0}, 2, 0.0, 0.0, true, false, false},
{Point{2.0, 4.0}, 2, 1.0, 0.75 * math.Pi, false, true, false},
}, []PathIntersection{
{Point{2.0, 2.0}, 2, 0.0, 0.0, false, false, false},
{Point{2.0, 4.0}, 1, 0.0, 1.5 * math.Pi, false, true, false},
}},
{"M0 2C0 4 2 4 2 2C4 2 4 4 2 4", "M2 4L2 2L4 2", []PathIntersection{
{Point{2.0, 2.0}, 2, 0.0, 0.0, true, false, false},
{Point{2.0, 4.0}, 2, 1.0, math.Pi, false, true, false},
}, []PathIntersection{
{Point{2.0, 2.0}, 2, 0.0, 0.0, false, false, false},
{Point{2.0, 4.0}, 1, 0.0, 1.5 * math.Pi, false, true, false},
}},
{"M0 2A1 1 0 0 0 2 2A1 1 0 0 1 2 4", "M2 4L2 2L4 2", []PathIntersection{
{Point{2.0, 2.0}, 2, 0.0, 0.0, true, false, false},
{Point{2.0, 4.0}, 2, 1.0, math.Pi, false, true, false},
}, []PathIntersection{
{Point{2.0, 2.0}, 2, 0.0, 0.0, false, false, false},
{Point{2.0, 4.0}, 1, 0.0, 1.5 * math.Pi, false, true, false},
}},
{"M0 2A1 1 0 0 1 2 2A1 1 0 0 1 2 4", "M2 4L2 2L4 2", []PathIntersection{
{Point{2.0, 2.0}, 2, 0.0, 0.0, true, false, false},
{Point{2.0, 4.0}, 2, 1.0, math.Pi, false, true, false},
}, []PathIntersection{
{Point{2.0, 2.0}, 2, 0.0, 0.0, false, false, false},
{Point{2.0, 4.0}, 1, 0.0, 1.5 * math.Pi, false, true, false},
}},
{"M0 2A1 1 0 0 1 2 2A1 1 0 0 1 2 4", "M2 0L2 2L0 2", []PathIntersection{
{Point{0.0, 2.0}, 1, 0.0, 1.5 * math.Pi, true, true, false},
{Point{2.0, 2.0}, 2, 0.0, 0.0, false, false, false},
}, []PathIntersection{
{Point{0.0, 2.0}, 2, 1.0, math.Pi, false, true, false},
{Point{2.0, 2.0}, 2, 0.0, math.Pi, true, false, false},
}},
{"M0 1L4 1L4 3L0 3z", "M4 3A1 1 0 0 0 2 3A1 1 0 0 0 4 3z", []PathIntersection{
{Point{4.0, 3.0}, 3, 0.0, math.Pi, false, false, false},
{Point{2.0, 3.0}, 3, 0.5, math.Pi, true, false, false},
}, []PathIntersection{
{Point{4.0, 3.0}, 1, 0.0, 1.5 * math.Pi, true, false, false},
{Point{2.0, 3.0}, 2, 0.0, 0.5 * math.Pi, false, false, false},
}},
{"M1 0L3 0L3 4L1 4z", "M4 3A1 1 0 0 0 2 3A1 1 0 0 0 4 3z", []PathIntersection{
{Point{3.0, 2.0}, 2, 0.5, 0.5 * math.Pi, false, false, false},
{Point{3.0, 4.0}, 3, 0.0, math.Pi, true, false, false},
}, []PathIntersection{
{Point{3.0, 2.0}, 1, 0.5, math.Pi, true, false, false},
{Point{3.0, 4.0}, 2, 0.5, 0.0, false, false, false},
}},
{"M1 0L3 0L3 4L1 4z", "M3 0A1 1 0 0 0 1 0A1 1 0 0 0 3 0z", []PathIntersection{
{Point{1.0, 0.0}, 1, 0.0, 0.0, false, false, false},
{Point{3.0, 0.0}, 2, 0.0, 0.5 * math.Pi, true, false, false},
}, []PathIntersection{
{Point{1.0, 0.0}, 2, 0.0, 0.5 * math.Pi, true, false, false},
{Point{3.0, 0.0}, 1, 0.0, 1.5 * math.Pi, false, false, false},
}},
{"M1 0L3 0L3 4L1 4z", "M1 0A1 1 0 0 0 -1 0A1 1 0 0 0 1 0z", []PathIntersection{
{Point{1.0, 0.0}, 1, 0.0, 0.0, true, true, false},
}, []PathIntersection{
{Point{1.0, 0.0}, 1, 0.0, 1.5 * math.Pi, false, true, false},
}},
{"M1 0L3 0L3 4L1 4z", "M1 0L1 -1L0 0z", []PathIntersection{
{Point{1.0, 0.0}, 1, 0.0, 0.0, true, true, false},
}, []PathIntersection{
{Point{1.0, 0.0}, 1, 0.0, 1.5 * math.Pi, false, true, false},
}},
{"M1 0L3 0L3 4L1 4z", "M1 0L0 0L1 -1z", []PathIntersection{
{Point{1.0, 0.0}, 1, 0.0, 0.0, false, true, false},
}, []PathIntersection{
{Point{1.0, 0.0}, 1, 0.0, math.Pi, false, true, false},
}},
{"M1 0L3 0L3 4L1 4z", "M1 0L2 0L1 1z", []PathIntersection{
{Point{2.0, 0.0}, 1, 0.5, 0.0, false, true, false},
{Point{1.0, 1.0}, 4, 0.75, 1.5 * math.Pi, false, true, true},
}, []PathIntersection{
{Point{2.0, 0.0}, 2, 0.0, 0.75 * math.Pi, true, true, false},
{Point{1.0, 1.0}, 3, 0.0, 1.5 * math.Pi, true, true, true},
}},
{"M1 0L3 0L3 4L1 4z", "M1 0L1 1L2 0z", []PathIntersection{
{Point{2.0, 0.0}, 1, 0.5, 0.0, true, true, false},
{Point{1.0, 1.0}, 4, 0.75, 1.5 * math.Pi, true, true, true},
}, []PathIntersection{
{Point{2.0, 0.0}, 3, 0.0, math.Pi, true, true, true},
{Point{1.0, 1.0}, 2, 0.0, 1.75 * math.Pi, true, true, false},
}},
{"M1 0L3 0L3 4L1 4z", "M1 0L2 1L0 1z", []PathIntersection{
{Point{1.0, 0.0}, 1, 0.0, 0.0, false, false, false},
{Point{1.0, 1.0}, 4, 0.75, 1.5 * math.Pi, true, false, false},
}, []PathIntersection{
{Point{1.0, 0.0}, 1, 0.0, 0.25 * math.Pi, true, false, false},
{Point{1.0, 1.0}, 2, 0.5, math.Pi, false, false, false},
}},
// intersection with overlapping lines
{"L0 15", "M5 0L0 5L0 10L5 15", []PathIntersection{
{Point{0.0, 5.0}, 1, 1.0 / 3.0, 0.5 * math.Pi, true, true, true},
{Point{0.0, 10.0}, 1, 2.0 / 3.0, 0.5 * math.Pi, true, true, false},
}, []PathIntersection{
{Point{0.0, 5.0}, 2, 0.0, 0.5 * math.Pi, false, true, true},
{Point{0.0, 10.0}, 3, 0.0, 0.25 * math.Pi, false, true, false},
}},
{"L0 15", "M5 0L0 5L0 10L-5 15", []PathIntersection{
{Point{0.0, 5.0}, 1, 1.0 / 3.0, 0.5 * math.Pi, false, false, true},
{Point{0.0, 10.0}, 1, 2.0 / 3.0, 0.5 * math.Pi, false, false, false},
}, []PathIntersection{
{Point{0.0, 5.0}, 2, 0.0, 0.5 * math.Pi, true, false, true},
{Point{0.0, 10.0}, 3, 0.0, 0.75 * math.Pi, true, false, false},
}},
{"L0 15", "M5 15L0 10L0 5L5 0", []PathIntersection{
{Point{0.0, 5.0}, 1, 1.0 / 3.0, 0.5 * math.Pi, false, true, true},
{Point{0.0, 10.0}, 1, 2.0 / 3.0, 0.5 * math.Pi, false, true, false},
}, []PathIntersection{
{Point{0.0, 5.0}, 3, 0.0, 1.75 * math.Pi, false, true, false},
{Point{0.0, 10.0}, 2, 0.0, 1.5 * math.Pi, false, true, true},
}},
{"L0 15", "M5 15L0 10L0 5L-5 0", []PathIntersection{
{Point{0.0, 5.0}, 1, 1.0 / 3.0, 0.5 * math.Pi, false, false, true},
{Point{0.0, 10.0}, 1, 2.0 / 3.0, 0.5 * math.Pi, false, false, false},
}, []PathIntersection{
{Point{0.0, 5.0}, 3, 0.0, 1.25 * math.Pi, true, false, false},
{Point{0.0, 10.0}, 2, 0.0, 1.5 * math.Pi, true, false, true},
}},
{"L0 10L-5 15", "M5 0L0 5L0 15", []PathIntersection{
{Point{0.0, 5.0}, 1, 0.5, 0.5 * math.Pi, true, true, true},
{Point{0.0, 10.0}, 2, 0.0, 0.75 * math.Pi, true, true, false},
}, []PathIntersection{
{Point{0.0, 5.0}, 2, 0.0, 0.5 * math.Pi, false, true, true},
{Point{0.0, 10.0}, 2, 0.5, 0.5 * math.Pi, false, true, false},
}},
{"L0 10L5 15", "M5 0L0 5L0 15", []PathIntersection{
{Point{0.0, 5.0}, 1, 0.5, 0.5 * math.Pi, false, false, true},
{Point{0.0, 10.0}, 2, 0.0, 0.25 * math.Pi, false, false, false},
}, []PathIntersection{
{Point{0.0, 5.0}, 2, 0.0, 0.5 * math.Pi, true, false, true},
{Point{0.0, 10.0}, 2, 0.5, 0.5 * math.Pi, true, false, false},
}},
{"L0 10L-5 15", "M0 15L0 5L5 0", []PathIntersection{
{Point{0.0, 5.0}, 1, 0.5, 0.5 * math.Pi, false, true, true},
{Point{0.0, 10.0}, 2, 0.0, 0.75 * math.Pi, false, true, false},
}, []PathIntersection{
{Point{0.0, 5.0}, 2, 0.0, 1.75 * math.Pi, false, true, false},
{Point{0.0, 10.0}, 1, 0.5, 1.5 * math.Pi, false, true, true},
}},
{"L0 10L5 15", "M0 15L0 5L5 0", []PathIntersection{
{Point{0.0, 5.0}, 1, 0.5, 0.5 * math.Pi, true, false, true},
{Point{0.0, 10.0}, 2, 0.0, 0.25 * math.Pi, true, false, false},
}, []PathIntersection{
{Point{0.0, 5.0}, 2, 0.0, 1.75 * math.Pi, false, false, false},
{Point{0.0, 10.0}, 1, 0.5, 1.5 * math.Pi, false, false, true},
}},
{"L5 5L5 10L0 15", "M10 0L5 5L5 15", []PathIntersection{
{Point{5.0, 5.0}, 2, 0.0, 0.5 * math.Pi, true, true, true},
{Point{5.0, 10.0}, 3, 0.0, 0.75 * math.Pi, true, true, false},
}, []PathIntersection{
{Point{5.0, 5.0}, 2, 0.0, 0.5 * math.Pi, false, true, true},
{Point{5.0, 10.0}, 2, 0.5, 0.5 * math.Pi, false, true, false},
}},
{"L5 5L5 10L10 15", "M10 0L5 5L5 15", []PathIntersection{
{Point{5.0, 5.0}, 2, 0.0, 0.5 * math.Pi, false, false, true},
{Point{5.0, 10.0}, 3, 0.0, 0.25 * math.Pi, false, false, false},
}, []PathIntersection{
{Point{5.0, 5.0}, 2, 0.0, 0.5 * math.Pi, true, false, true},
{Point{5.0, 10.0}, 2, 0.5, 0.5 * math.Pi, true, false, false},
}},
{"L5 5L5 10L0 15", "M10 0L5 5L5 10L10 15", []PathIntersection{
{Point{5.0, 5.0}, 2, 0.0, 0.5 * math.Pi, true, true, true},
{Point{5.0, 10.0}, 3, 0.0, 0.75 * math.Pi, true, true, false},
}, []PathIntersection{
{Point{5.0, 5.0}, 2, 0.0, 0.5 * math.Pi, false, true, true},
{Point{5.0, 10.0}, 3, 0.0, 0.25 * math.Pi, false, true, false},
}},
{"L5 5L5 10L10 15", "M10 0L5 5L5 10L0 15", []PathIntersection{
{Point{5.0, 5.0}, 2, 0.0, 0.5 * math.Pi, false, false, true},
{Point{5.0, 10.0}, 3, 0.0, 0.25 * math.Pi, false, false, false},
}, []PathIntersection{
{Point{5.0, 5.0}, 2, 0.0, 0.5 * math.Pi, true, false, true},
{Point{5.0, 10.0}, 3, 0.0, 0.75 * math.Pi, true, false, false},
}},
{"L5 5L5 10L10 15L5 20", "M10 0L5 5L5 10L10 15L10 20", []PathIntersection{
{Point{5.0, 5.0}, 2, 0.0, 0.5 * math.Pi, true, true, true},
{Point{10.0, 15.0}, 4, 0.0, 0.75 * math.Pi, true, true, false},
}, []PathIntersection{
{Point{5.0, 5.0}, 2, 0.0, 0.5 * math.Pi, false, true, true},
{Point{10.0, 15.0}, 4, 0.0, 0.5 * math.Pi, false, true, false},
}},
{"L5 5L5 10L10 15L5 20", "M10 20L10 15L5 10L5 5L10 0", []PathIntersection{
{Point{5.0, 5.0}, 2, 0.0, 0.5 * math.Pi, false, true, true},
{Point{10.0, 15.0}, 4, 0.0, 0.75 * math.Pi, false, true, false},
}, []PathIntersection{
{Point{5.0, 5.0}, 4, 0.0, 1.75 * math.Pi, false, true, false},
{Point{10.0, 15.0}, 2, 0.0, 1.25 * math.Pi, false, true, true},
}},
{"L2 0L2 1L0 1z", "M1 0L3 0L3 1L1 1z", []PathIntersection{
{Point{1.0, 0.0}, 1, 0.5, 0.0, true, false, true},
{Point{2.0, 0.0}, 2, 0.0, 0.5 * math.Pi, true, false, false},
{Point{2.0, 1.0}, 3, 0.0, math.Pi, false, false, true},
{Point{1.0, 1.0}, 3, 0.5, math.Pi, false, false, false},
}, []PathIntersection{
{Point{1.0, 0.0}, 1, 0.0, 0.0, false, false, true},
{Point{2.0, 0.0}, 1, 0.5, 0.0, false, false, false},
{Point{2.0, 1.0}, 3, 0.5, math.Pi, true, false, true},
{Point{1.0, 1.0}, 4, 0.0, 1.5 * math.Pi, true, false, false},
}},
// bugs
{"M67.89174682452696 63.79390646055095L67.89174682452696 63.91890646055095L59.89174682452683 50.06250000000001", "M68.10825317547533 63.79390646055193L67.89174682452919 63.91890646055186M67.89174682452672 63.918906460550865L59.891746824526074 50.06250000000021", []PathIntersection{
{Point{67.89174682452696, 63.91890646055095}, 2, 0.0, 240.0 * math.Pi / 180.0, false, true, false},
{Point{67.89174682452696, 63.91890646055095}, 2, 0.0, 240.0 * math.Pi / 180.0, false, true, true},
{Point{59.89174682452683, 50.06250000000001}, 2, 1.0, 240.0 * math.Pi / 180.0, false, true, false},
}, []PathIntersection{
{Point{67.89174682452919, 63.91890646055186}, 1, 1.0, 150.0 * math.Pi / 180.0, false, true, false},
{Point{67.89174682452672, 63.918906460550865}, 3, 0.0, 240.0 * math.Pi / 180.0, false, true, true},
{Point{59.891746824526074, 50.06250000000021}, 3, 1.0, 240.0 * math.Pi / 180.0, false, true, false},
}},
}
origEpsilon := Epsilon
for _, tt := range tts {
t.Run(fmt.Sprint(tt.p, "x", tt.q), func(t *testing.T) {
Epsilon = origEpsilon
p := MustParseSVGPath(tt.p)
q := MustParseSVGPath(tt.q)
zp, zq := p.Collisions(q)
Epsilon = 3.0 * origEpsilon
test.T(t, zp, tt.zp)
test.T(t, zq, tt.zq)
})
}
Epsilon = origEpsilon
}
func TestSelfIntersections(t *testing.T) {
var tts = []struct {
p string
zs []PathIntersection
}{
{"L10 10L10 0L0 10z", []PathIntersection{
{Point{5.0, 5.0}, 1, 0.5, 0.25 * math.Pi, false, false, false},
{Point{5.0, 5.0}, 3, 0.5, 0.75 * math.Pi, true, false, false},
}},
// intersection
{"M2 1L0 0L0 2L2 1L1 0L1 2z", []PathIntersection{
{Point{2.0, 1.0}, 1, 0.0, 206.5650511771 * math.Pi / 180.0, false, false, false},
{Point{1.0, 0.5}, 1, 0.5, 206.5650511771 * math.Pi / 180.0, true, false, false},
{Point{1.0, 1.5}, 3, 0.5, 333.4349488229 * math.Pi / 180.0, false, false, false},
{Point{2.0, 1.0}, 4, 0.0, 1.25 * math.Pi, true, false, false},
{Point{1.0, 0.5}, 5, 0.25, 0.5 * math.Pi, false, false, false},
{Point{1.0, 1.5}, 5, 0.75, 0.5 * math.Pi, true, false, false},
}},
// parallel segment TODO
{"L10 0L5 0L15 0", []PathIntersection{
{Point{5.0, 0.0}, 1, 0.5, 0.0, false, true, true},
{Point{10.0, 0.0}, 3, 0.5, 0.0, false, true, true},
}},
{"L10 0L0 0L15 0", []PathIntersection{
{Point{0.0, 0.0}, 1, 0.5, 0.0, false, true, true},
{Point{10.0, 0.0}, 3, 0.5, 0.0, false, true, true},
}},
{"L10 0L15 5L20 10L15 5L10 0L0 5", []PathIntersection{
{Point{10.0, 0.0}, 1, 0.0, 0.0, true, true, true},
{Point{10.0, 0.0}, 6, 0.0, 0.0, true, true, true},
}},
{"L15 0L15 10L5 10L5 0L10 0L10 5L0 5z", []PathIntersection{
{Point{5.0, 0.0}, 1, 0.5, 0.0, false, true, true},
{Point{10.0, 0.0}, 3, 0.5, 0.0, false, true, true},
}},
{"L15 0L15 10L0 10L0 0L10 0L10 5L-5 5L-5 0z", []PathIntersection{
{Point{0.0, 0.0}, 1, 0.5, 0.0, false, true, true},
{Point{10.0, 0.0}, 3, 0.5, 0.0, false, true, true},
}},
{"L15 0L15 10L0 10L0 0L10 0L10 5L0 5z", []PathIntersection{
{Point{0.0, 5.0}, 1, 0.5, 0.0, false, true, true},
{Point{10.0, 0.0}, 3, 0.5, 0.0, false, true, true},
}},
{"L-5 0A5 5 0 0 1 5 0A5 5 0 0 1 -5 0z", []PathIntersection{
{Point{5.0, 0.0}, 1, 0.5, 0.0, false, true, true},
{Point{10.0, 0.0}, 3, 0.5, 0.0, false, true, true},
}},
{"L-5 0A5 5 0 0 1 5 0A5 5 0 0 1 -5 0L0 0L0 1L1 0L0 -1z", []PathIntersection{
{Point{0.0, 0.0}, 1, 0.0, math.Pi, false, true, true},
{Point{10.0, 0.0}, 3, 0.5, 0.0, false, true, true},
}},
{"L15 0L15 5L5 0L10 0L15 -5", []PathIntersection{
{Point{5.0, 0.0}, 1, 1.0 / 3.0, 0.0, false, true, true},
{Point{10.0, 0.0}, 3, 2.0 / 3.0, 0.0, false, true, true},
}},
{"L15 0L15 5L10 0L5 0L0 5", []PathIntersection{
{Point{5.0, 0.0}, 1, 1.0 / 3.0, 0.0, false, true, true},
{Point{10.0, 0.0}, 3, 2.0 / 3.0, 0.0, false, true, true},
}},
// bugs
{"M3.512162397982181 1.239754268684486L3.3827323986701674 1.1467946944092953L3.522449858001167 1.2493787337129587A0.21166666666666667 0.21166666666666667 0 0 1 3.5121623979821806 1.2397542686844856z", []PathIntersection{}}, // #277, very small circular arc at the end of the path to the start
{"M-0.1997406229376793 296.9999999925494L-0.1997406229376793 158.88740153238177L-0.19974062293732664 158.8874019079834L-0.19999999999964735 20.77454766193328", []PathIntersection{
{Point{-0.1997406229376793, 158.88740172019808}, 1, 0.9999999986401219, 270.0 * math.Pi / 180.0, true, false, false},
{Point{-0.1997406229376793, 158.88740172019808}, 3, 1.359651237533596e-09, 269.9998923980606 * math.Pi / 180.0, false, false, false},
}}, // #287
}
for _, tt := range tts {
t.Run(fmt.Sprint(tt.p), func(t *testing.T) {
p := MustParseSVGPath(tt.p)
zs, _ := pathIntersections(p, nil, true, true)
test.T(t, zs, tt.zs)
})
}
}
func TestPathCut(t *testing.T) {
var tts = []struct {
p, q string
rs []string
}{