updates
This commit is contained in:
parent
469855f3de
commit
e0597a291a
33
clockface.go
33
clockface.go
@ -1 +1,34 @@
|
|||||||
package clockface
|
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}
|
||||||
|
}
|
||||||
|
@ -1,42 +1,35 @@
|
|||||||
package clockface
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
|
"git.nerdfortress.dev/mitch/clockface"
|
||||||
|
"io"
|
||||||
"math"
|
"math"
|
||||||
|
"os"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
package clockface
|
func main() {
|
||||||
|
t := time.Now()
|
||||||
import (
|
sh := clockface.SecondHand(t)
|
||||||
"git.nerdfortress.dev/mitch/gotdd/clockface"
|
io.WriteString(os.Stdout, svgStart)
|
||||||
"math"
|
io.WriteString(os.Stdout, bezel)
|
||||||
"time"
|
io.WriteString(os.Stdout, secondHandTag(sh))
|
||||||
)
|
io.WriteString(os.Stdout, svgEnd)
|
||||||
|
|
||||||
const secondHandLength = 90
|
|
||||||
const clockCentreX = 150
|
|
||||||
const clockCentreY = 150
|
|
||||||
|
|
||||||
type Point struct {
|
|
||||||
X float64
|
|
||||||
Y float64
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func SecondHand(t time.Time) Point {
|
func secondHandTag(p clockface.Point) string {
|
||||||
p := secondHandPoint(t)
|
return fmt.Sprintf(`<line x1="150" y1="150" x2="%f" y2="%f" style="fill:none;stroke:#f00;stroke-width:3px;"/>`, p.X, p.Y)
|
||||||
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 {
|
const svgStart = `<?xml version="1.0" encoding="UTF-8" standalone="no"?>
|
||||||
return math.Pi / (30 / (float64(t.Second())))
|
<!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 {
|
const bezel = `<circle cx="150" cy="150" r="100" style="fill:#fff;stroke:#000;stroke-width:5px;"/>`
|
||||||
angle := secondsInRadians(t)
|
|
||||||
x := math.Sin(angle)
|
const svgEnd = `</svg>`
|
||||||
y := math.Cos(angle)
|
|
||||||
return Point{x, y}
|
|
||||||
}
|
|
||||||
|
@ -10,6 +10,7 @@ package clockface
|
|||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
"git.nerdfortress.dev/mitch/clockface"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestSecondHandAtMidnight(t *testing.T) {
|
func TestSecondHandAtMidnight(t *testing.T) {
|
||||||
|
@ -12,6 +12,7 @@ import (
|
|||||||
"math"
|
"math"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
"git.nerdfortress.dev/mitch/clockface"
|
||||||
)
|
)
|
||||||
|
|
||||||
func simpleTime(hours, minutes, seconds int) time.Time {
|
func simpleTime(hours, minutes, seconds int) time.Time {
|
||||||
|
Loading…
Reference in New Issue
Block a user