-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwindrose.go
89 lines (80 loc) · 1.97 KB
/
windrose.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package main
import (
"bytes"
"fmt"
"io/ioutil"
"math"
"os"
"text/template"
)
// Coordinates represents coordinates on a cartesian plane
type Coordinates struct {
X, Y int
}
// Arrow represents an SVG line object with marker
type Arrow struct {
Start, Finish Coordinates
Width float64
}
// GenWindrose takes an input angle and fills a byte buffer with SVG data
func GenWindrose(angleDeg float64, svgWindroseBuf *bytes.Buffer) error {
svgBaseTmplData, err := ioutil.ReadFile("windrose_base.svg.tmpl")
if err != nil {
return err
}
svgArrowTmplData, err := ioutil.ReadFile("windrose_arrow.svg.tmpl")
if err != nil {
return err
}
svgArrowTmpl, err := template.New("arrow").Parse(string(svgArrowTmplData))
if err != nil {
return err
}
svgBaseTmpl, err := template.New("arrow").Parse(string(svgBaseTmplData))
if err != nil {
return err
}
var (
arrow Arrow
start, finish Coordinates
)
// starting point is static
start.X = 307 // not sure why this is offset by 7 and Y is not
start.Y = 300
arrowLength := 200 // 200 = 2/3 of 1 quadrant of canvas
angleRad := (angleDeg - 90) * (math.Pi / 180)
fmt.Fprintf(os.Stderr, "angle: %f degrees, %f radians\n", angleDeg, angleRad)
finish.X = start.X + int(math.Cos(angleRad)*float64(arrowLength))
finish.Y = start.Y + int(math.Sin(angleRad)*float64(arrowLength))
arrow.Start = start
arrow.Finish = finish
arrow.Width = 3.5
svgArrowBuf := &bytes.Buffer{}
err = svgArrowTmpl.Execute(svgArrowBuf, arrow)
if err != nil {
return err
}
err = svgBaseTmpl.Execute(svgWindroseBuf, svgArrowBuf.String())
return nil
}
/*
func main() {
var (
angleDeg float64
err error
)
if len(os.Args) > 1 {
angleDeg, err = strconv.ParseFloat(os.Args[1], 64)
if err != nil {
panic(err)
}
}
svgWindroseBuf := &bytes.Buffer{}
err = GenWindrose(angleDeg, svgWindroseBuf)
// err = svgBaseTmpl.Execute(os.Stdout, svgArrowBuf.String())
if err != nil {
panic(err)
}
fmt.Printf("%s\n", svgWindroseBuf.String())
}
*/