Move bearing into maths

This commit is contained in:
Marc Di Luzio 2020-07-10 18:24:54 +01:00
parent 5b1fe61097
commit f40f7123d4
5 changed files with 17 additions and 22 deletions

82
pkg/maths/bearing.go Normal file
View file

@ -0,0 +1,82 @@
package maths
import (
"fmt"
"strings"
)
// Bearing describes a compass direction
type Bearing int
const (
// North describes a 0,1 vector
North Bearing = iota
// NorthEast describes a 1,1 vector
NorthEast
// East describes a 1,0 vector
East
// SouthEast describes a 1,-1 vector
SouthEast
// South describes a 0,-1 vector
South
// SouthWest describes a -1,-1 vector
SouthWest
// West describes a -1,0 vector
West
// NorthWest describes a -1,1 vector
NorthWest
)
// bearingString simply describes the strings associated with a direction
type bearingString struct {
Long string
Short string
}
// bearingStrings is the set of strings for each direction
var bearingStrings = []bearingString{
{"North", "N"},
{"NorthEast", "NE"},
{"East", "E"},
{"SouthEast", "SE"},
{"South", "S"},
{"SouthWest", "SW"},
{"West", "W"},
{"NorthWest", "NW"},
}
// String converts a Direction to a String
func (d Bearing) String() string {
return bearingStrings[d].Long
}
// ShortString converts a Direction to a short string version
func (d Bearing) ShortString() string {
return bearingStrings[d].Short
}
// FromString gets the Direction from a string
func FromString(s string) (Bearing, error) {
for i, d := range bearingStrings {
if strings.EqualFold(d.Long, s) || strings.EqualFold(d.Short, s) {
return Bearing(i), nil
}
}
return -1, fmt.Errorf("unknown bearing: %s", s)
}
var bearingVectors = []Vector{
{X: 0, Y: 1}, // N
{X: 1, Y: 1}, // NE
{X: 1, Y: 0}, // E
{X: 1, Y: -1}, // SE
{X: 0, Y: -1}, // S
{X: -1, Y: 1}, // SW
{X: -1, Y: 0}, // W
{X: -1, Y: 1}, // NW
}
// Vector converts a Direction to a Vector
func (d Bearing) Vector() Vector {
return bearingVectors[d]
}

31
pkg/maths/bearing_test.go Normal file
View file

@ -0,0 +1,31 @@
package maths
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestDirection(t *testing.T) {
dir := North
assert.Equal(t, "North", dir.String())
assert.Equal(t, "N", dir.ShortString())
assert.Equal(t, Vector{X: 0, Y: 1}, dir.Vector())
dir, err := FromString("N")
assert.NoError(t, err)
assert.Equal(t, North, dir)
dir, err = FromString("n")
assert.NoError(t, err)
assert.Equal(t, North, dir)
dir, err = FromString("north")
assert.NoError(t, err)
assert.Equal(t, North, dir)
dir, err = FromString("NorthWest")
assert.NoError(t, err)
assert.Equal(t, NorthWest, dir)
}