go-by-test/clockface/clockface.go
2024-09-23 16:49:29 +02:00

38 lines
883 B
Go

package clockface
import (
"math"
"time"
)
// Point represents a two-dimentional Cartesian coordinate
type Point struct {
X float64
Y float64
}
// SecondHand is the unit vector of the second hand of an analogue clock at the time `t` represented as a Point
func SecondHand(t time.Time) Point {
p := secondHandPoint(t)
p = Point{p.X * 90, p.Y * 90} // scale
p = Point{p.X, -p.Y} // flip
p = Point{p.X + 150, p.Y + 150} // translate
return p
}
func secondsInRadians(t time.Time) float64 {
// XXX:Wanted 3.141592653589793 radians, but got 3.1415926535897936
// return float64(t.Second()) * (math.Pi / 30)
return math.Pi / (30 / float64(t.Second()))
}
func secondHandPoint(t time.Time) Point {
angle := secondsInRadians(t)
x := math.Sin(angle)
y := math.Cos(angle)
// XXX: Wanted {0 -1} Point, but got {1.2246467991473515e-16 -1}
return Point{x, y}
}