github.com/pavlo67/common@v0.5.3/common/mathlib/plane/chain_projections_test.go (about) 1 package plane 2 3 import ( 4 "math" 5 "testing" 6 7 "github.com/stretchr/testify/require" 8 9 "github.com/pavlo67/common/common/mathlib" 10 ) 11 12 func TestProjectionsOnPolyChainProbe(t *testing.T) { 13 polyChain := PolyChain{{221.29971138416823, 290.62290413201606}, {238, 268.5}, {262, 252}} 14 point := Point2{237, 266} 15 16 got := ProjectionsOnPolyChain(polyChain, point, 10) 17 18 t.Logf("%#v", got) 19 } 20 21 func TestProjectionsOnPolyChain(t *testing.T) { 22 tests := []struct { 23 name string 24 polyChain PolyChain 25 p Point2 26 distanceMax float64 27 expected []ProjectionOnPolyChainDirected 28 }{ 29 { 30 name: "", 31 polyChain: PolyChain{{0, 2}, {1, 1}, {2, 2}}, // , {3, 1} 32 p: Point2{1, 0}, 33 distanceMax: 10, 34 expected: []ProjectionOnPolyChainDirected{ 35 {Distance: 1, ProjectionOnPolyChain: ProjectionOnPolyChain{N: 1, Point2: Point2{1, 1}}}}, 36 }, 37 { 38 name: "", 39 polyChain: PolyChain{{0, 2}, {1, 1}, {2, 2}, {3, 1}}, // 40 p: Point2{1, 0}, 41 distanceMax: 10, 42 expected: []ProjectionOnPolyChainDirected{ 43 {Distance: 1, ProjectionOnPolyChain: ProjectionOnPolyChain{N: 1, Point2: Point2{1, 1}}}, 44 {Distance: 3 * math.Sqrt(2) / 2, 45 ProjectionOnPolyChain: ProjectionOnPolyChain{N: 2, Position: math.Sqrt(2) / 2, Point2: Point2{2.5, 1.5}}}, 46 }, 47 }, 48 { 49 name: "", 50 polyChain: PolyChain{{X: 567.758571909734, Y: 327.33667901650387}, {X: 588, Y: 381}}, // 51 p: Point2{X: 574, Y: 355}, 52 distanceMax: 3, 53 expected: []ProjectionOnPolyChainDirected{}, 54 }, 55 { 56 name: "", 57 polyChain: PolyChain{{X: 416.5, Y: 420}, {X: 422, Y: 413}, {X: 414.5, Y: 402}}, // 58 p: Point2{X: 427, Y: 412}, 59 distanceMax: 3, 60 expected: []ProjectionOnPolyChainDirected{}, 61 }, 62 } 63 for _, tt := range tests { 64 t.Run(tt.name, func(t *testing.T) { 65 got := ProjectionsOnPolyChain(tt.polyChain, tt.p, tt.distanceMax) 66 CheckProjections(t, tt.expected, got) 67 }) 68 } 69 } 70 71 // PolyChain{Point2{X:556, Y:355}, Point2{X:559, Y:355}, Point2{X:562, Y:355}, Point2{X:565, Y:355}, Point2{X:568, 72 // Y:355}, Point2{X:571, Y:355}, Point2} / 3) 73 74 func CheckProjections(t *testing.T, expected, got []ProjectionOnPolyChainDirected) { 75 require.Equalf(t, len(expected), len(got), "expected: %v, got: %#v", expected, got) 76 for i, e := range expected { 77 g := got[i] 78 require.Equalf(t, e.N, g.N, "#d: %v vs %v", i, e, g) 79 require.Truef(t, math.Abs(e.Position-g.Position) <= mathlib.Eps, "#d: %v vs %v", i, e, g) 80 require.Truef(t, math.Abs(e.Distance-g.Distance) <= mathlib.Eps, "#d: %v vs %v", i, e, g) 81 require.Truef(t, math.Abs(e.Angle-g.Angle) <= mathlib.Eps, "#d: %v vs %v", i, e, g) 82 require.Truef(t, math.Abs(e.X-g.X) <= mathlib.Eps, "#d: %v vs %v", i, e, g) 83 require.Truef(t, math.Abs(e.Y-g.Y) <= mathlib.Eps, "#d: %v vs %v", i, e, g) 84 85 } 86 }