-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvector.go
91 lines (78 loc) · 2.05 KB
/
vector.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
90
91
package texture
import (
"github.com/jphsd/texture/color"
"math"
)
// VectorFields produces a vector field from a slice of fields
type VectorFields struct {
Name string
Srcs []Field
}
func NewVectorFields(srcs ...Field) *VectorFields {
return &VectorFields{"VectorFields", srcs}
}
// Eval2 implements the VectorField interface.
func (v *VectorFields) Eval2(x, y float64) []float64 {
res := make([]float64, len(v.Srcs))
for i, f := range v.Srcs {
res[i] = f.Eval2(x, y)
}
return res
}
// VectorColor uses the three values from a color field to populate it.
type VectorColor struct {
Name string
Src ColorField
}
func NewVectorColor(src ColorField) *VectorColor {
return &VectorColor{"VectorColor", src}
}
// Eval2 implements the VectorField interface.
func (v *VectorColor) Eval2(x, y float64) []float64 {
c := color.NewFRGBA(v.Src.Eval2(x, y))
res := make([]float64, 3)
res[0] = c.R*2 - 1
res[1] = c.G*2 - 1
res[2] = c.B*2 - 1
res[3] = c.A*2 - 1
return res
}
// Normal provides a VectorField calculated from a Field using the finite difference method.
type Normal struct {
Name string
Src Field
SDx, SDy float64
Dx, Dy float64
}
// NewNormal returns a new instance of Normal.
func NewNormal(src Field, sx, sy, dx, dy float64) *Normal {
return &Normal{"Normal", src, sx / (2 * dx), sy / (2 * dy), dx, dy}
}
// Eval2 implements the VectorField interface.
func (n *Normal) Eval2(x, y float64) []float64 {
dx := n.Src.Eval2(x-n.Dx, y) - n.Src.Eval2(x+n.Dx, y)
dy := n.Src.Eval2(x, y-n.Dy) - n.Src.Eval2(x, y+n.Dy)
dx *= n.SDx
dy *= n.SDy
div := 1 / math.Sqrt(dx*dx+dy*dy+1)
return []float64{dx * div, dy * div, div}
}
// UnitVector provides a unit vector (i.e. magnitude = 1) version of a VectorField.
type UnitVector struct {
Name string
Src VectorField
}
// Eval2 implements the VectorField interface.
func (u *UnitVector) Eval2(x, y float64) []float64 {
v := u.Src.Eval2(x, y)
var s float64
for _, f := range v {
s += f * f
}
s = 1 / math.Sqrt(s)
res := make([]float64, len(v))
for i, f := range v {
res[i] = f * s
}
return res
}