|
- package position_test
-
- import (
- "math"
- "testing"
-
- "git.wtrh.nl/patterns/gopatterns/pkg/position"
- "git.wtrh.nl/patterns/gopatterns/pkg/vector"
- "github.com/stretchr/testify/require"
- )
-
- func TestPosition_Direction(t *testing.T) {
- t.Parallel()
-
- type testCase struct {
- A, B position.Position
- ExpA2B, ExpB2A float64
- }
-
- testCases := []testCase{
- {
- A: position.Position{
- Vector: vector.Vector{X: 0, Y: 0},
- Rotation: math.Pi / 4,
- },
- B: position.Position{
- Vector: vector.Vector{X: 2, Y: 0},
- Rotation: 3 * math.Pi / 4,
- },
- ExpA2B: -math.Pi / 4,
- ExpB2A: math.Pi / 4,
- },
- {
- A: position.Position{
- Vector: vector.Vector{X: 0, Y: 1},
- Rotation: math.Pi,
- },
- B: position.Position{
- Vector: vector.Vector{X: 1, Y: 0},
- Rotation: -math.Pi / 2,
- },
- ExpA2B: 3 * math.Pi / 4,
- ExpB2A: -3 * math.Pi / 4,
- },
- }
-
- for _, test := range testCases {
- actA2B := test.A.Direction(test.B)
- require.InDelta(t, test.ExpA2B, actA2B, 1e-12)
-
- actB2A := test.B.Direction(test.A)
- require.InDelta(t, test.ExpB2A, actB2A, 1e-12)
- }
- }
-
- func TestPosition_Add(t *testing.T) {
- t.Parallel()
-
- tests := map[string]struct {
- p, q position.Position
- want position.Position
- }{
- "straight angle": {
- p: position.Position{
- Vector: vector.Vector{X: 1, Y: 0},
- Rotation: math.Pi / 2,
- },
- q: position.Position{
- Vector: vector.Vector{X: 1, Y: 0},
- Rotation: 0,
- },
- want: position.Position{
- Vector: vector.Vector{X: 1, Y: 1},
- Rotation: math.Pi / 2,
- },
- },
- "3-4-5 triangle": {
- p: position.Position{
- Vector: vector.Vector{X: -2, Y: 0},
- Rotation: math.Atan2(3, 4),
- },
- q: position.Position{
- Vector: vector.Vector{X: 5, Y: 0},
- Rotation: -math.Atan2(3, 4),
- },
- want: position.Position{
- Vector: vector.Vector{X: 2, Y: 3},
- Rotation: 0,
- },
- },
- }
-
- for name, tt := range tests {
- t.Run(name, func(t *testing.T) {
- t.Parallel()
-
- require.Equal(t, tt.want, tt.p.Add(tt.q))
- })
- }
- }
-
- func TestPosition_Translate(t *testing.T) {
- tests := map[string]struct {
- p, q position.Position
- result float64
- }{
- "origin to 1,1": {
- p: position.Position{
- Vector: vector.Vector{
- X: 1, Y: 1,
- },
- },
- q: position.Position{
- Vector: vector.Vector{
- X: 0, Y: 0,
- },
- },
- result: math.Sqrt(2),
- },
- "0,2 to 4,5 -> 5": {
- p: position.Position{
- Vector: vector.Vector{
- X: 0, Y: 2,
- },
- },
- q: position.Position{
- Vector: vector.Vector{
- X: 4, Y: 5,
- },
- },
- result: 5,
- },
- }
-
- for name, tt := range tests {
- t.Run(name, func(t *testing.T) {
- translateResult := tt.p.Distance(tt.q)
- require.InDelta(t, tt.result, translateResult, 1e-10)
- })
- }
- }
|