forked from mumax/3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsvg.go
67 lines (52 loc) · 1.27 KB
/
svg.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
package draw
import (
"fmt"
"github.com/mumax/3/svgo"
"io"
"math"
)
// Renders svg image of vector data.
func SVG(out io.Writer, arr [3][][][]float32) {
h, w := len(arr[0][0]), len(arr[0][0][0])
const (
r1 = 1. / 2. // arrow half length
r2 = 1. / 4. // arrow half width
)
canvas := svg.New(out)
canvas.Start(w, h)
for slice := 0; slice < len(arr[0]); slice++ {
Mx := arr[X][slice]
My := arr[Y][slice]
Mz := arr[Z][slice]
for i := 0; i < h; i++ {
y := float64(h) - (float64(i) + 1./2.)
for j := 0; j < w; j++ {
x := float64(j) + 1./2.
mx := Mx[i][j]
my := My[i][j]
mz := Mz[i][j]
// skip zero-length vectors
if mx*mx+my*my+mz*mz == 0 {
continue
}
theta := math.Atan2(float64(my), float64(mx))
c := math.Cos(theta)
s := math.Sin(theta)
r1 := r1 * math.Cos(math.Asin(float64(mz)))
xs := []float64{(r1 * c) + x, (r2*s - r1*c) + x, (-r2*s - r1*c) + x}
ys := []float64{-(r1 * s) + y, -(-r2*c - r1*s) + y, -(r2*c - r1*s) + y}
col := HSLMap(mx, my, mz)
style := "fill:#" + hex(col.R) + hex(col.G) + hex(col.B)
canvas.Polygon(xs, ys, style)
}
}
}
canvas.End()
}
func hex(i uint8) string {
j := int(i) - 32 // make it a bit darker
if j < 0 {
j = 0
}
return fmt.Sprintf("%02X", j)
}