This commit is contained in:
mitch 2022-01-19 23:29:46 -05:00
parent 469855f3de
commit e0597a291a
4 changed files with 59 additions and 31 deletions

View File

@ -1 +1,34 @@
package clockface
import (
"git.nerdfortress.dev/mitch/clockface"
"math"
)
const secondHandLength = 90
const clockCentreX = 150
const clockCentreY = 150
type Point struct {
X float64
Y float64
}
func SecondHand(t time.Time) Point {
p := secondHandPoint(t)
p = Point{p.X * secondHandLength, p.Y * secondHandLength} //scale
p = Point{p.X, -p.Y} //flip
p = Point{p.X + clockCentreX, p.Y + clockCentreY} //translate
return p
}
func secondsInRadians(t time.Time) float64 {
return math.Pi / (30 / (float64(t.Second())))
}
func secondHandPoint(t time.Time) Point {
angle := secondsInRadians(t)
x := math.Sin(angle)
y := math.Cos(angle)
return Point{x, y}
}

View File

@ -1,42 +1,35 @@
package clockface
package main
import (
"fmt"
"git.nerdfortress.dev/mitch/clockface"
"io"
"math"
"os"
"time"
)
package clockface
import (
"git.nerdfortress.dev/mitch/gotdd/clockface"
"math"
"time"
)
const secondHandLength = 90
const clockCentreX = 150
const clockCentreY = 150
type Point struct {
X float64
Y float64
func main() {
t := time.Now()
sh := clockface.SecondHand(t)
io.WriteString(os.Stdout, svgStart)
io.WriteString(os.Stdout, bezel)
io.WriteString(os.Stdout, secondHandTag(sh))
io.WriteString(os.Stdout, svgEnd)
}
func SecondHand(t time.Time) Point {
p := secondHandPoint(t)
p = Point{p.X * secondHandLength, p.Y * secondHandLength} //scale
p = Point{p.X, -p.Y} //flip
p = Point{p.X + clockCentreX, p.Y + clockCentreY} //translate
return p
func secondHandTag(p clockface.Point) string {
return fmt.Sprintf(`<line x1="150" y1="150" x2="%f" y2="%f" style="fill:none;stroke:#f00;stroke-width:3px;"/>`, p.X, p.Y)
}
func secondsInRadians(t time.Time) float64 {
return math.Pi / (30 / (float64(t.Second())))
}
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">`
func secondHandPoint(t time.Time) Point {
angle := secondsInRadians(t)
x := math.Sin(angle)
y := math.Cos(angle)
return Point{x, y}
}
const bezel = `<circle cx="150" cy="150" r="100" style="fill:#fff;stroke:#000;stroke-width:5px;"/>`
const svgEnd = `</svg>`

View File

@ -10,6 +10,7 @@ package clockface
import (
"testing"
"time"
"git.nerdfortress.dev/mitch/clockface"
)
func TestSecondHandAtMidnight(t *testing.T) {

View File

@ -12,6 +12,7 @@ import (
"math"
"testing"
"time"
"git.nerdfortress.dev/mitch/clockface"
)
func simpleTime(hours, minutes, seconds int) time.Time {