Skip to content

Commit 601ef0d

Browse files
committed
✨ (clockface): add minuteHand implementation
1 parent d72f6b7 commit 601ef0d

File tree

3 files changed

+41
-29
lines changed

3 files changed

+41
-29
lines changed

clockface_sample/clockface/clockface.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,5 @@ func angleToPoint(angle float64) Point {
4848
func minuteHandPoint(t time.Time) Point {
4949
return angleToPoint(minutesInRadians(t))
5050
}
51+
52+
const minuteHandLength = 80

clockface_sample/clockface/clockface_acceptance_test.go

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -101,28 +101,28 @@ func TestSecondHandAt30Seconds(t *testing.T) {
101101
}
102102
}
103103

104-
// func TestSVGWriterMinuteHand(t *testing.T) {
105-
// cases := []struct {
106-
// time time.Time
107-
// line Line
108-
// }{
109-
// {
110-
// simpleTime(0, 0, 0),
111-
// Line{150, 150, 150, 70},
112-
// },
113-
// }
114-
115-
// for _, c := range cases {
116-
// t.Run(testName(c.time), func(t *testing.T) {
117-
// b := bytes.Buffer{}
118-
// clockface.SVGWriter(&b, c.time)
119-
120-
// svg := SVG{}
121-
// xml.Unmarshal(b.Bytes(), &svg)
122-
123-
// if !containsLine(c.line, svg.Line) {
124-
// t.Errorf("Expected to find the minute hand line %+v, in the SVG lines %+v", c.line, svg.Line)
125-
// }
126-
// })
127-
// }
128-
// }
104+
func TestSVGWriterMinuteHand(t *testing.T) {
105+
cases := []struct {
106+
time time.Time
107+
line Line
108+
}{
109+
{
110+
simpleTime(0, 0, 0),
111+
Line{150, 150, 150, 70},
112+
},
113+
}
114+
115+
for _, c := range cases {
116+
t.Run(testName(c.time), func(t *testing.T) {
117+
b := bytes.Buffer{}
118+
clockface.SVGWriter(&b, c.time)
119+
120+
svg := SVG{}
121+
xml.Unmarshal(b.Bytes(), &svg)
122+
123+
if !containsLine(c.line, svg.Line) {
124+
t.Errorf("Expected to find the minute hand line %+v, in the SVG lines %+v", c.line, svg.Line)
125+
}
126+
})
127+
}
128+
}

clockface_sample/clockface/svgWriter.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,27 @@ func SVGWriter(w io.Writer, t time.Time) {
1111
io.WriteString(w, svgStart)
1212
io.WriteString(w, bezel)
1313
secondHand(w, t)
14+
minuteHand(w, t)
1415
io.WriteString(w, svgEnd)
1516
}
1617

1718
func secondHand(w io.Writer, t time.Time) {
18-
p := secondHandPoint(t)
19-
p = Point{p.X * secondHandLength, p.Y * secondHandLength}
20-
p = Point{p.X, -p.Y}
21-
p = Point{p.X + clockCentreX, p.Y + clockCentreY} //translate
19+
p := makeHand(secondHandPoint(t), secondHandLength)
2220
fmt.Fprintf(w, `<line x1="150" y1="150" x2="%.3f" y2="%.3f" style="fill:none;stroke:#f00;stroke-width:3px;"/>`, p.X, p.Y)
2321
}
2422

23+
func minuteHand(w io.Writer, t time.Time) {
24+
p := makeHand(minuteHandPoint(t), minuteHandLength)
25+
fmt.Fprintf(w, `<line x1="150" y1="150" x2="%.3f" y2="%.3f" style="fill:none;stroke:#000;stroke-width:3px;"/>`, p.X, p.Y)
26+
}
27+
28+
func makeHand(p Point, length float64) Point {
29+
p = Point{p.X * length, p.Y * length} // scale
30+
p = Point{p.X, -p.Y} // flip
31+
// transition
32+
return Point{p.X + clockCentreX, p.Y + clockCentreY}
33+
}
34+
2535
const svgStart = `<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2636
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
2737
<svg xmlns="http://www.w3.org/2000/svg"

0 commit comments

Comments
 (0)