Skip to content

Commit 635ac85

Browse files
committed
♻️ (clockfare): refactor with test
1 parent 26da2ce commit 635ac85

File tree

7 files changed

+136
-164
lines changed

7 files changed

+136
-164
lines changed

clockface_sample/clock.svg

Lines changed: 1 addition & 1 deletion
Loading

clockface_sample/clockface/clockface.go

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,11 @@ type Point struct {
2020
Y float64
2121
}
2222

23-
// SecondHand is the unit vector of the second hand of an analogue clock at time `t`
24-
// represented as a Point.
25-
func SecondHand(t time.Time) Point {
26-
p := secondHandPoint(t)
27-
p = Point{p.X * secondHandLength, p.Y * secondHandLength}
28-
p = Point{p.X, -p.Y}
29-
p = Point{p.X + clockCentreX, p.Y + clockCentreY} //translate
30-
return p
31-
}
32-
3323
func secondsInRadians(t time.Time) float64 {
3424
return (math.Pi / (secondsInHalfClock / (float64(t.Second()))))
3525
}
3626

37-
func secondHandPoint(t time.Time) Point {
27+
func SecondHandPoint(t time.Time) Point {
3828
return angleToPoint(secondsInRadians(t))
3929
}
4030

@@ -50,7 +40,7 @@ func angleToPoint(angle float64) Point {
5040
return Point{x, y}
5141
}
5242

53-
func minuteHandPoint(t time.Time) Point {
43+
func MinuteHandPoint(t time.Time) Point {
5444
return angleToPoint(minutesInRadians(t))
5545
}
5646

@@ -59,6 +49,6 @@ func hoursInRadians(t time.Time) float64 {
5949
(math.Pi / (hoursInHalfClock / float64(t.Hour()%hoursInClock)))
6050
}
6151

62-
func hourHandPoint(t time.Time) Point {
52+
func HourHandPoint(t time.Time) Point {
6353
return angleToPoint(hoursInRadians(t))
6454
}

clockface_sample/clockface/clockface_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func TestSecondHandPoint(t *testing.T) {
4545

4646
for _, c := range cases {
4747
t.Run(testName(c.time), func(t *testing.T) {
48-
got := secondHandPoint(c.time)
48+
got := SecondHandPoint(c.time)
4949
if !roughlyEqualPoint(got, c.point) {
5050
t.Fatalf("Wanted %v Point, but got %v", c.point, got)
5151
}
@@ -93,7 +93,7 @@ func TestMinuteHandPoint(t *testing.T) {
9393

9494
for _, c := range cases {
9595
t.Run(testName(c.time), func(t *testing.T) {
96-
got := minuteHandPoint(c.time)
96+
got := MinuteHandPoint(c.time)
9797
if !roughlyEqualPoint(got, c.point) {
9898
t.Fatalf("Wanted %v Point, but got %v", c.point, got)
9999
}
@@ -133,7 +133,7 @@ func TestHourHandPoint(t *testing.T) {
133133

134134
for _, c := range cases {
135135
t.Run(testName(c.time), func(t *testing.T) {
136-
got := hourHandPoint(c.time)
136+
got := HourHandPoint(c.time)
137137
if !roughlyEqualPoint(got, c.point) {
138138
t.Fatalf("Wanted %v Point, but got %v", c.point, got)
139139
}

clockface_sample/clockface/svgWriter.go

Lines changed: 0 additions & 59 deletions
This file was deleted.

clockface_sample/cmd/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ import (
44
"os"
55
"time"
66

7-
"github.com/leetcode-golang-classroom/learn-golang-with-tests/clockface_sample/clockface"
7+
"github.com/leetcode-golang-classroom/learn-golang-with-tests/clockface_sample/svg"
88
)
99

1010
func main() {
1111
t := time.Now()
12-
clockface.SVGWriter(os.Stdout, t)
12+
svg.Write(os.Stdout, t)
1313
}

clockface_sample/svg/svg.go

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package svg
2+
3+
import (
4+
"encoding/xml"
5+
"fmt"
6+
"io"
7+
"time"
8+
9+
"github.com/leetcode-golang-classroom/learn-golang-with-tests/clockface_sample/clockface"
10+
)
11+
12+
type SVG struct {
13+
XMLName xml.Name `xml:"svg"`
14+
Xmlns string `xml:"xmlns,attr"`
15+
Width string `xml:"width,attr"`
16+
Height string `xml:"height,attr"`
17+
ViewBox string `xml:"viewBox,attr"`
18+
Version string `xml:"version,attr"`
19+
Circle Circle `xml:"circle"`
20+
Line []Line `xml:"line"`
21+
}
22+
23+
type Circle struct {
24+
Cx float64 `xml:"cx,attr"`
25+
Cy float64 `xml:"cy,attr"`
26+
R float64 `xml:"r,attr"`
27+
}
28+
29+
type Line struct {
30+
X1 float64 `xml:"x1,attr"`
31+
Y1 float64 `xml:"y1,attr"`
32+
X2 float64 `xml:"x2,attr"`
33+
Y2 float64 `xml:"y2,attr"`
34+
}
35+
36+
const (
37+
secondHandLength = 90
38+
minuteHandLength = 80
39+
hourHandLength = 50
40+
clockCentreX = 150
41+
clockCentreY = 150
42+
)
43+
44+
// Write writes an SVG representation of an analogue clock, showing the time t, to the writer w.
45+
func Write(w io.Writer, t time.Time) {
46+
io.WriteString(w, svgStart)
47+
io.WriteString(w, bezel)
48+
secondHand(w, t)
49+
minuteHand(w, t)
50+
hourHand(w, t)
51+
io.WriteString(w, svgEnd)
52+
}
53+
54+
func secondHand(w io.Writer, t time.Time) {
55+
p := makeHand(clockface.SecondHandPoint(t), secondHandLength)
56+
fmt.Fprintf(w, `<line x1="150" y1="150" x2="%.3f" y2="%.3f" style="fill:none;stroke:#f00;stroke-width:3px;"/>`, p.X, p.Y)
57+
}
58+
59+
func minuteHand(w io.Writer, t time.Time) {
60+
p := makeHand(clockface.MinuteHandPoint(t), minuteHandLength)
61+
fmt.Fprintf(w, `<line x1="150" y1="150" x2="%.3f" y2="%.3f" style="fill:none;stroke:#000;stroke-width:3px;"/>`, p.X, p.Y)
62+
}
63+
64+
func makeHand(p clockface.Point, length float64) clockface.Point {
65+
p = clockface.Point{X: p.X * length, Y: p.Y * length} // scale
66+
p = clockface.Point{X: p.X, Y: -p.Y} // flip
67+
// transition
68+
return clockface.Point{X: p.X + clockCentreX, Y: p.Y + clockCentreY}
69+
}
70+
71+
const svgStart = `<?xml version="1.0" encoding="UTF-8" standalone="no"?>
72+
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
73+
<svg xmlns="http://www.w3.org/2000/svg"
74+
width="100%"
75+
height="100%"
76+
viewBox="0 0 300 300"
77+
version="2.0">`
78+
79+
const bezel = `<circle cx="150" cy="150" r="100" style="fill:#fff;stroke:#000;stroke-width:5px;"/>`
80+
81+
const svgEnd = `</svg>`
82+
83+
func hourHand(w io.Writer, t time.Time) {
84+
p := makeHand(clockface.HourHandPoint(t), hourHandLength)
85+
fmt.Fprintf(w, `<line x1="150" y1="150" x2="%.3f" y2="%.3f" style="fill:none;stroke:#000;stroke-width:3px;"/>`, p.X, p.Y)
86+
}

0 commit comments

Comments
 (0)