Skip to content

Commit 57ef7cc

Browse files
committed
✨ (clockface): create SVGWriter and implemented it
1 parent 7fc2ffd commit 57ef7cc

File tree

4 files changed

+72
-23
lines changed

4 files changed

+72
-23
lines changed

clockface_sample/clock.svg

Lines changed: 7 additions & 0 deletions
Loading

clockface_sample/clockface/clockface_acceptance_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,41 @@
11
package clockface_test
22

33
import (
4+
"strings"
45
"testing"
56
"time"
67

78
clockface "github.com/leetcode-golang-classroom/learn-golang-with-tests/clockface_sample/clockface"
89
)
910

11+
func TestSVGWriterAtMidnight(t *testing.T) {
12+
tm := time.Date(1337, time.January, 1, 0, 0, 0, 0, time.UTC)
13+
14+
var b strings.Builder
15+
clockface.SVGWriter(&b, tm)
16+
got := b.String()
17+
18+
want := `<line x1="150" y1="150" x2="150.000" y2="60.000"`
19+
20+
if !strings.Contains(got, want) {
21+
t.Errorf("Expected to find the second hand %v, in the SVG output %v", want, got)
22+
}
23+
}
24+
25+
func TestSVGWriterAt30Seconds(t *testing.T) {
26+
tm := time.Date(1337, time.January, 1, 0, 0, 30, 0, time.UTC)
27+
28+
var b strings.Builder
29+
clockface.SVGWriter(&b, tm)
30+
got := b.String()
31+
32+
want := `<line x1="150" y1="150" x2="150.000" y2="240.000"`
33+
34+
if !strings.Contains(got, want) {
35+
t.Errorf("Expected to find the second hand %v, in the SVG output %v", want, got)
36+
}
37+
}
38+
1039
func TestSecondHandAtMidnight(t *testing.T) {
1140
tm := time.Date(1337, time.January, 1, 0, 0, 0, 0, time.UTC)
1241

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package clockface
2+
3+
import (
4+
"fmt"
5+
"io"
6+
"time"
7+
)
8+
9+
// SVGWriter writes an SVG representation of an analogue clock, showing the time t, to the writer w.
10+
func SVGWriter(w io.Writer, t time.Time) {
11+
io.WriteString(w, svgStart)
12+
io.WriteString(w, bezel)
13+
secondHand(w, t)
14+
io.WriteString(w, svgEnd)
15+
}
16+
17+
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
22+
fmt.Fprintf(w, `<line x1="150" y1="150" x2="%.3f" y2="%.3f" style="fill:none;stroke:#f00;stroke-width:3px;"/>`, p.X, p.Y)
23+
}
24+
25+
const svgStart = `<?xml version="1.0" encoding="UTF-8" standalone="no"?>
26+
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
27+
<svg xmlns="http://www.w3.org/2000/svg"
28+
width="100%"
29+
height="100%"
30+
viewBox="0 0 300 300"
31+
version="2.0">`
32+
33+
const bezel = `<circle cx="150" cy="150" r="100" style="fill:#fff;stroke:#000;stroke-width:5px;"/>`
34+
35+
const svgEnd = `</svg>`

clockface_sample/cmd/main.go

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package main
22

33
import (
4-
"fmt"
5-
"io"
64
"os"
75
"time"
86

@@ -11,25 +9,5 @@ import (
119

1210
func main() {
1311
t := time.Now()
14-
sh := clockface.SecondHand(t)
15-
io.WriteString(os.Stdout, svgStart)
16-
io.WriteString(os.Stdout, bezel)
17-
io.WriteString(os.Stdout, secondHandTag(sh))
18-
io.WriteString(os.Stdout, svgEnd)
12+
clockface.SVGWriter(os.Stdout, t)
1913
}
20-
21-
func secondHandTag(p clockface.Point) string {
22-
return fmt.Sprintf(`<line x1="150" y1="150" x2="%f" y2="%f" style="fill:none;stroke:#f00;stroke-width:3px;"/>`, p.X, p.Y)
23-
}
24-
25-
const svgStart = `<?xml version="1.0" encoding="UTF-8" standalone="no"?>
26-
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
27-
<svg xmlns="http://www.w3.org/2000/svg"
28-
width="100%"
29-
height="100%"
30-
viewBox="0 0 300 300"
31-
version="2.0">`
32-
33-
const bezel = `<circle cx="150" cy="150" r="100" style="fill:#fff;stroke:#000;stroke-width:5px;"/>`
34-
35-
const svgEnd = `</svg>`

0 commit comments

Comments
 (0)