Skip to content

Commit

Permalink
feat(angle): add new utils to wrap to 0-2Pi and create from degrees
Browse files Browse the repository at this point in the history
  • Loading branch information
tvkn committed Aug 15, 2023
1 parent b74b9e0 commit 7f6f4f9
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
16 changes: 15 additions & 1 deletion angle.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,12 @@ func FromRadians(a float64) Angle {
return Angle(a)
}

// WrapMinusPiPi wraps the current angle in the interval [-pi, pi].
// FromDegrees returns an Angle from degrees as float64.
func FromDegrees(a float64) Angle {
return Angle(a) * Degree
}

// WrapMinusPiPi wraps the current angle in the interval [-pi, pi[.
func (a Angle) WrapMinusPiPi() Angle {
b := math.Mod(a.Radians()+math.Pi, 2*math.Pi)
if b < 0 {
Expand All @@ -46,6 +51,15 @@ func (a Angle) WrapMinusPiPi() Angle {
return Angle(b - math.Pi)
}

// WrapZeroTwoPi wraps the current angle in the interval [0, 2*pi[.
func (a Angle) WrapZeroTwoPi() Angle {
b := math.Mod(a.Radians(), 2*math.Pi)
if b < 0 {
b += 2 * math.Pi
}
return Angle(b)
}

// Get returns a with the unit of as.
func (a Angle) Get(as Angle) float64 {
return float64(a) / float64(as)
Expand Down
45 changes: 45 additions & 0 deletions angle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import (
"math"
"testing"

"github.com/google/go-cmp/cmp"
"gotest.tools/v3/assert"
)

func TestAngle_FromDegrees(t *testing.T) {
assert.Equal(t, math.Pi*Radian, 180*Degree)
assert.Equal(t, FromRadians(math.Pi), FromDegrees(180))
}

func TestAngle_ToDegrees(t *testing.T) {
Expand Down Expand Up @@ -52,3 +54,46 @@ func TestAngle_WrapMinusPiPi(t *testing.T) {
})
}
}

func TestAngle_WrapZeroTwoPi(t *testing.T) {
const (
twoPi = 2 * math.Pi
epsi = 1e-5
)
type test struct {
name string
angle, want Angle
}
tests := []test{
{angle: 1.0, want: 1.0, name: "within positive"},
{angle: 4.0, want: 4.0, name: "within positive (large)"},
{angle: 8.0, want: 8.0 - twoPi, name: "double positive"},
{angle: 12.0, want: 12.0 - twoPi, name: "double positive (large)"},
{angle: 15.0, want: 15.0 - 2*twoPi, name: "triple positive"},
{angle: -1.0, want: -1.0 + twoPi, name: "negative change"},
{angle: -4.0, want: -4.0 + twoPi, name: "negative change (large)"},
{angle: -8.0, want: -8.0 + 2*twoPi, name: "double negative"},
{angle: -12.0, want: -12.0 + 2*twoPi, name: "double negative (large)"},
{angle: -15.0, want: -15.0 + 3*twoPi, name: "triple negative"},
{angle: math.Pi, want: math.Pi, name: "pi"},
{angle: -math.Pi, want: math.Pi, name: "-pi"},
{angle: twoPi, want: 0, name: "2 pi"},
{angle: 0, want: 0, name: "0"},
}
var d float64
withinEps := cmp.Comparer(func(x, y float64) bool { d = math.Abs(x - y); return d < epsi })
for _, tc := range tests {
tc := tc
t.Run(tc.name, func(t *testing.T) {
got := tc.angle.WrapZeroTwoPi()
w, g := tc.want.Radians(), got.Radians()
ok := cmp.Equal(w, g, withinEps)
// requirement check
assert.Assert(t, g < twoPi)
assert.Assert(t, g >= 0)
assert.Assert(t, math.Mod(g-tc.angle.Radians(), twoPi) < epsi)
// exact check
assert.Assert(t, ok, "got: %f, want: %f, diff: %f > %f", g, w, d, epsi)
})
}
}

0 comments on commit 7f6f4f9

Please sign in to comment.