Skip to content

Commit 0611af8

Browse files
committed
✨ (clockface): add implementation with for unit circle
1 parent 562ed24 commit 0611af8

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

clockface/clockface.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,9 @@ func testName(t time.Time) string {
3030
}
3131

3232
func secondHandPoint(t time.Time) Point {
33-
return Point{0, -1}
33+
angle := secondsInRadians(t)
34+
x := math.Sin(angle)
35+
y := math.Cos(angle)
36+
37+
return Point{x, y}
3438
}

clockface/clockface_test.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,25 @@ func TestSecondHandPoint(t *testing.T) {
3333
point Point
3434
}{
3535
{simpleTime(0, 0, 30), Point{0, -1}},
36+
{simpleTime(0, 0, 45), Point{-1, 0}},
3637
}
3738

3839
for _, c := range cases {
3940
t.Run(testName(c.time), func(t *testing.T) {
4041
got := secondHandPoint(c.time)
41-
if got != c.point {
42+
if !roughlyEqualPoint(got, c.point) {
4243
t.Fatalf("Wanted %v Point, but got %v", c.point, got)
4344
}
4445
})
4546
}
4647
}
48+
49+
func roughlyEqualFloat64(a, b float64) bool {
50+
const equalityThreshold = 1e-7
51+
return math.Abs(a-b) < equalityThreshold
52+
}
53+
54+
func roughlyEqualPoint(a, b Point) bool {
55+
return roughlyEqualFloat64(a.X, b.X) &&
56+
roughlyEqualFloat64(a.Y, b.Y)
57+
}

0 commit comments

Comments
 (0)