Move bearing into proto file
This commit is contained in:
parent
3796ee09a3
commit
4e0e55af88
10 changed files with 281 additions and 306 deletions
|
@ -1,97 +0,0 @@
|
|||
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
|
||||
}
|
||||
|
||||
// BearingFromString gets the Direction from a string
|
||||
func BearingFromString(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]
|
||||
}
|
||||
|
||||
// IsCardinal returns if this is a cardinal (NESW)
|
||||
func (d Bearing) IsCardinal() bool {
|
||||
switch d {
|
||||
case North:
|
||||
fallthrough
|
||||
case East:
|
||||
fallthrough
|
||||
case South:
|
||||
fallthrough
|
||||
case West:
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
|
@ -1,31 +0,0 @@
|
|||
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 := BearingFromString("N")
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, North, dir)
|
||||
|
||||
dir, err = BearingFromString("n")
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, North, dir)
|
||||
|
||||
dir, err = BearingFromString("north")
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, North, dir)
|
||||
|
||||
dir, err = BearingFromString("NorthWest")
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, NorthWest, dir)
|
||||
}
|
|
@ -2,6 +2,8 @@ package maths
|
|||
|
||||
import (
|
||||
"math"
|
||||
|
||||
"github.com/mdiluz/rove/proto/roveapi"
|
||||
)
|
||||
|
||||
// Vector desribes a 3D vector
|
||||
|
@ -81,3 +83,19 @@ func Min2(v1 Vector, v2 Vector) Vector {
|
|||
func Max2(v1 Vector, v2 Vector) Vector {
|
||||
return Vector{Max(v1.X, v2.X), Max(v1.Y, v2.Y)}
|
||||
}
|
||||
|
||||
// BearingToVector converts a bearing to a vector
|
||||
func BearingToVector(b roveapi.Bearing) Vector {
|
||||
switch b {
|
||||
case roveapi.Bearing_North:
|
||||
return Vector{Y: 1}
|
||||
case roveapi.Bearing_East:
|
||||
return Vector{X: 1}
|
||||
case roveapi.Bearing_South:
|
||||
return Vector{Y: -1}
|
||||
case roveapi.Bearing_West:
|
||||
return Vector{X: -1}
|
||||
}
|
||||
|
||||
return Vector{}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue