github.com/pavlo67/common@v0.5.3/common/mathlib/plane/lines_test.go (about) 1 package plane 2 3 import ( 4 "testing" 5 6 "github.com/stretchr/testify/require" 7 ) 8 9 type IntersectionTestCase struct { 10 S1, S2 Segment 11 Intersection *Point2 12 } 13 14 func TestLineSegmentsIntersection(t *testing.T) { 15 testCases := []IntersectionTestCase{ 16 {Segment{Point2{1, 0}, Point2{1, 1}}, 17 Segment{Point2{0, 1.5}, Point2{2, 1.5}}, nil}, 18 {Segment{Point2{1, 2}, Point2{1, 1}}, 19 Segment{Point2{1.25, 1.5}, Point2{2, 1.5}}, nil}, 20 21 {Segment{Point2{1, 1}, Point2{1, 2}}, 22 Segment{Point2{3, 0}, Point2{2, 1}}, nil}, 23 24 {Segment{Point2{2, 2}, Point2{-1, -1}}, 25 Segment{Point2{-3, 1}, Point2{3, -1}}, &Point2{0, 0}}, 26 {Segment{Point2{2, 2}, Point2{3, 3}}, 27 Segment{Point2{-3, 1}, Point2{3, -1}}, nil}, 28 {Segment{Point2{3, 3}, Point2{2, 2}}, 29 Segment{Point2{-3, 1}, Point2{3, -1}}, nil}, 30 {Segment{Point2{0, 0}, Point2{1, 1}}, 31 Segment{Point2{1, 1}, Point2{2, 2}}, nil}, 32 33 {Segment{Point2{0, 0}, Point2{0, 1}}, 34 Segment{Point2{0, 0.1}, Point2{0, 2}}, &Point2{0, 0.1}}, 35 36 {Segment{Point2{0, 0}, Point2{1, 0}}, 37 Segment{Point2{-2, 0}, Point2{-1, 0}}, nil}, 38 {Segment{Point2{1, 1}, Point2{1, 2}}, 39 Segment{Point2{1, 1}, Point2{2, 1}}, &Point2{1, 1}}, 40 {Segment{Point2{1, 1}, Point2{1, 2}}, 41 Segment{Point2{2, 1}, Point2{2, 2}}, nil}, 42 {Segment{Point2{1, 1}, Point2{1, 2}}, 43 Segment{Point2{0, 1.5}, Point2{2, 1.5}}, &Point2{1, 1.5}}, 44 } 45 46 for i, testCase := range testCases { 47 t.Log(i) 48 // t.Logf("%#v", testCase) 49 intersection := SegmentsIntersection(testCase.S1, testCase.S2) 50 if testCase.Intersection == nil { 51 require.Nil(t, intersection) 52 } else { 53 require.NotNil(t, intersection) 54 require.Equal(t, *testCase.Intersection, *intersection) 55 } 56 } 57 } 58 59 func TestLinesIntersection(t *testing.T) { 60 testCases := []IntersectionTestCase{ 61 {Segment{Point2{1, 1}, Point2{1, 2}}, 62 Segment{Point2{1, 1}, Point2{2, 1}}, &Point2{1, 1}}, 63 64 {Segment{Point2{1, 1}, Point2{1, 2}}, 65 Segment{Point2{2, 1}, Point2{2, 2}}, nil}, 66 67 {Segment{Point2{1, 1}, Point2{1, 2}}, 68 Segment{Point2{0, 1.5}, Point2{2, 1.5}}, &Point2{1, 1.5}}, 69 70 {Segment{Point2{1, 2}, Point2{1, 1}}, 71 Segment{Point2{1.25, 1.5}, Point2{2, 1.5}}, &Point2{1, 1.5}}, 72 73 {Segment{Point2{1, 1}, Point2{1, 2}}, 74 Segment{Point2{3, 0}, Point2{2, 1}}, &Point2{1, 2}}, 75 76 {Segment{Point2{2, 2}, Point2{-1, -1}}, 77 Segment{Point2{-3, 1}, Point2{3, -1}}, &Point2{0, 0}}, 78 79 {Segment{Point2{2, 2}, Point2{3, 3}}, 80 Segment{Point2{-3, 1}, Point2{3, -1}}, &Point2{0, 0}}, 81 82 {Segment{Point2{3, 3}, Point2{2, 2}}, 83 Segment{Point2{-3, 1}, Point2{3, -1}}, &Point2{0, 0}}, 84 85 {Segment{Point2{0, 0}, Point2{1, 1}}, 86 Segment{Point2{1, 1}, Point2{2, 2}}, nil}, 87 88 {Segment{Point2{0, 0}, Point2{0, 1}}, 89 Segment{Point2{0, 0.1}, Point2{0, 2}}, nil}, 90 91 {Segment{Point2{0, 0}, Point2{1, 0}}, 92 Segment{Point2{-2, 0}, Point2{-1, 0}}, nil}, 93 } 94 95 for i, testCase := range testCases { 96 t.Log(i) 97 intersection := testCase.S1.LinesIntersection(testCase.S2) 98 if testCase.Intersection == nil { 99 require.Nil(t, intersection) 100 } else { 101 require.NotNil(t, intersection) 102 require.Equal(t, *testCase.Intersection, *intersection) 103 } 104 } 105 }