2022-01-20 04:56:30 +00:00
|
|
|
package clockface
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2022-01-20 06:07:29 +00:00
|
|
|
const (
|
|
|
|
hourHandLength = 50
|
|
|
|
secondHandLength = 90
|
|
|
|
minuteHandLength = 80
|
|
|
|
clockCentreX = 150
|
|
|
|
clockCentreY = 150
|
|
|
|
)
|
2022-01-20 04:56:30 +00:00
|
|
|
|
|
|
|
const svgStart = `<?xml version="1.0" encoding="UTF-8" standalone="no"?>
|
|
|
|
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
|
|
|
|
<svg xmlns="http://www.w3.org/2000/svg"
|
|
|
|
width="100%"
|
|
|
|
height="100%"
|
|
|
|
viewBox="0 0 300 300"
|
|
|
|
version="2.0">`
|
|
|
|
|
|
|
|
const bezel = `<circle cx="150" cy="150" r="100" style="fill:#fff;stroke:#000;stroke-width:5px;"/>`
|
|
|
|
|
|
|
|
const svgEnd = `</svg>`
|
|
|
|
|
|
|
|
//SVGWriter writes an SVG representation of an analog clock, showing the time t, to the writer w
|
|
|
|
func SVGWriter(w io.Writer, t time.Time) {
|
|
|
|
io.WriteString(w, svgStart)
|
|
|
|
io.WriteString(w, bezel)
|
|
|
|
SecondHand(w, t)
|
2022-01-20 05:45:27 +00:00
|
|
|
MinuteHand(w, t)
|
2022-01-20 06:07:29 +00:00
|
|
|
HourHand(w, t)
|
2022-01-20 04:56:30 +00:00
|
|
|
io.WriteString(w, svgEnd)
|
|
|
|
}
|
|
|
|
|
2022-01-20 05:45:27 +00:00
|
|
|
func makeHand(p Point, length float64) Point {
|
|
|
|
p = Point{p.X * length, p.Y * length}
|
|
|
|
p = Point{p.X, -p.Y}
|
|
|
|
return Point{p.X + clockCentreX, p.Y + clockCentreY}
|
|
|
|
}
|
|
|
|
|
2022-01-20 04:56:30 +00:00
|
|
|
func SecondHand(w io.Writer, t time.Time) {
|
2022-01-20 05:45:27 +00:00
|
|
|
p := makeHand(secondHandPoint(t), secondHandLength)
|
2022-01-20 04:56:30 +00:00
|
|
|
fmt.Fprintf(w, `<line x1="150" y1="150" x2="%.3f" y2="%.3f" style="fill:none;stroke:#f00;stroke-width:3px;"/>`, p.X, p.Y)
|
|
|
|
}
|
2022-01-20 05:45:27 +00:00
|
|
|
|
|
|
|
func MinuteHand(w io.Writer, t time.Time) {
|
|
|
|
p := makeHand(minuteHandPoint(t), minuteHandLength)
|
|
|
|
fmt.Fprintf(w, `<line x1="150" y1="150" x2="%.3f" y2="%.3f" style="fill:none;stroke:#000;stroke-width:3px;"/>`, p.X, p.Y)
|
|
|
|
}
|
2022-01-20 06:07:29 +00:00
|
|
|
|
|
|
|
func HourHand(w io.Writer, t time.Time) {
|
|
|
|
p := makeHand(hourHandPoint(t), hourHandLength)
|
|
|
|
fmt.Fprintf(w, `<line x1="150" y1="150" x2="%.3f" y2="%.3f" style="fill:none;stroke:#000;stroke-width:3px;"/>`, p.X, p.Y)
|
|
|
|
}
|