diff --git a/clockface/clockface.go b/clockface/clockface.go index 06fade1..96da233 100644 --- a/clockface/clockface.go +++ b/clockface/clockface.go @@ -52,6 +52,18 @@ func SecondHand(w io.Writer, t time.Time) { ) } +func minutesInRadians(t time.Time) float64 { + return (secondsInRadians(t) / 60) + math.Pi/(30/float64(t.Minute())) +} + +func minuteHandPoint(t time.Time) Point { + angle := minutesInRadians(t) + x := math.Sin(angle) + y := math.Cos(angle) + + return Point{x, y} +} + func secondsInRadians(t time.Time) float64 { // XXX:Wanted 3.141592653589793 radians, but got 3.1415926535897936 // return float64(t.Second()) * (math.Pi / 30) diff --git a/clockface/clockface_test.go b/clockface/clockface_test.go index 3f32a48..4543800 100644 --- a/clockface/clockface_test.go +++ b/clockface/clockface_test.go @@ -6,6 +6,26 @@ import ( "time" ) +func TestMinutesInRadians(t *testing.T) { + cases := []struct { + time time.Time + angle float64 + }{ + {simpleTime(0, 30, 0), math.Pi}, + {simpleTime(0, 0, 7), 7 * (math.Pi / (30 * 60))}, + } + + for _, c := range cases { + t.Run(testName(c.time), func(t *testing.T) { + got := minutesInRadians(c.time) + + if c.angle != got { + t.Fatalf("Wanted %v radians, but got %v", c.angle, got) + } + }) + } +} + func TestSecondsInRadians(t *testing.T) { cases := []struct { time time.Time @@ -28,6 +48,25 @@ func TestSecondsInRadians(t *testing.T) { } } +func TestMinuteHandPoint(t *testing.T) { + cases := []struct { + time time.Time + point Point + }{ + {simpleTime(0, 30, 0), Point{0, -1}}, + {simpleTime(0, 45, 0), Point{-1, 0}}, + } + + for _, c := range cases { + t.Run(testName(c.time), func(t *testing.T) { + got := minuteHandPoint(c.time) + if !roughlyEqualPoint(got, c.point) { + t.Fatalf("Wanted %v Point, but got %v", c.point, got) + } + }) + } +} + func TestSecondHandPoint(t *testing.T) { cases := []struct { time time.Time