forked from go-gl/mathgl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvectorf.go
169 lines (139 loc) · 3.73 KB
/
vectorf.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package mathgl
import (
"math"
)
type Vec2f [2]float32
type Vec3f [3]float32
type Vec4f [4]float32
func (v1 Vec2f) Add(v2 Vec2f) Vec2f {
return Vec2f{v1[0] + v2[0], v1[1] + v2[1]}
}
func (v1 Vec3f) Add(v2 Vec3f) Vec3f {
return Vec3f{v1[0] + v2[0], v1[1] + v2[1], v1[2] + v2[2]}
}
func (v1 Vec4f) Add(v2 Vec4f) Vec4f {
return Vec4f{v1[0] + v2[0], v1[1] + v2[1], v1[2] + v2[2], v1[3] + v2[3]}
}
func (v1 Vec2f) Sub(v2 Vec2f) Vec2f {
return Vec2f{v1[0] - v2[0], v1[1] - v2[1]}
}
func (v1 Vec3f) Sub(v2 Vec3f) Vec3f {
return Vec3f{v1[0] - v2[0], v1[1] - v2[1], v1[2] - v2[2]}
}
func (v1 Vec4f) Sub(v2 Vec4f) Vec4f {
return Vec4f{v1[0] - v2[0], v1[1] - v2[1], v1[2] - v2[2], v1[3] - v2[3]}
}
func (v1 Vec2f) Mul(c float32) Vec2f {
return Vec2f{v1[0] * c, v1[1] * c}
}
func (v1 Vec3f) Mul(c float32) Vec3f {
return Vec3f{v1[0] * c, v1[1] * c, v1[2] * c}
}
func (v1 Vec4f) Mul(c float32) Vec4f {
return Vec4f{v1[0] * c, v1[1] * c, v1[2] * c, v1[3] * c}
}
func (v1 Vec2f) Dot(v2 Vec2f) float32 {
return v1[0]*v2[0] + v1[1]*v2[1]
}
func (v1 Vec3f) Dot(v2 Vec3f) float32 {
return v1[0]*v2[0] + v1[1]*v2[1] + v1[2]*v2[2]
}
func (v1 Vec4f) Dot(v2 Vec4f) float32 {
return v1[0]*v2[0] + v1[1]*v2[1] + v1[2]*v2[2] + v1[3]*v2[3]
}
func (v1 Vec2f) Len() float32 {
return float32(math.Sqrt(float64(v1[0]*v1[0] + v1[1]*v1[1])))
}
func (v1 Vec3f) Len() float32 {
return float32(math.Sqrt(float64(v1[0]*v1[0] + v1[1]*v1[1] + v1[2]*v1[2])))
}
func (v1 Vec4f) Len() float32 {
return float32(math.Sqrt(float64(v1[0]*v1[0] + v1[1]*v1[1] + v1[2]*v1[2] + v1[3]*v1[3])))
}
func (v1 Vec2f) Normalize() Vec2f {
l := 1.0 / math.Sqrt(float64(v1[0]*v1[0]+v1[1]*v1[1]))
return Vec2f{float32(float64(v1[0]) * l), float32(float64(v1[1]) * l)}
}
func (v1 Vec3f) Normalize() Vec3f {
l := 1.0 / math.Sqrt(float64(v1[0]*v1[0]+v1[1]*v1[1]+v1[2]*v1[2]))
return Vec3f{float32(float64(v1[0]) * l), float32(float64(v1[1]) * l), float32(float64(v1[2]) * l)}
}
func (v1 Vec4f) Normalize() Vec4f {
l := 1.0 / math.Sqrt(float64(v1[0]*v1[0]+v1[1]*v1[1]+v1[2]*v1[2]+v1[3]*v1[3]))
return Vec4f{float32(float64(v1[0]) * l), float32(float64(v1[1]) * l), float32(float64(v1[2]) * l), float32(float64(v1[3]) * l)}
}
func (v1 Vec3f) Cross(v2 Vec3f) Vec3f {
return Vec3f{v1[1]*v2[2] - v1[2]*v2[1], v1[2]*v2[0] - v1[0]*v2[2], v1[0]*v2[1] - v1[1]*v2[0]}
}
func (v1 Vec2f) ApproxEqual(v2 Vec2f) bool {
for i := range v1 {
if !FloatEqual32(v1[i], v2[i]) {
return false
}
}
return true
}
func (v1 Vec3f) ApproxEqual(v2 Vec3f) bool {
for i := range v1 {
if !FloatEqual32(v1[i], v2[i]) {
return false
}
}
return true
}
func (v1 Vec4f) ApproxEqual(v2 Vec4f) bool {
for i := range v1 {
if !FloatEqual32(v1[i], v2[i]) {
return false
}
}
return true
}
func (v1 Vec2f) ApproxEqualTheshold(v2 Vec2f, threshold float32) bool {
for i := range v1 {
if !FloatEqualThreshold32(v1[i], v2[i], threshold) {
return false
}
}
return true
}
func (v1 Vec3f) ApproxEqualTheshold(v2 Vec3f, threshold float32) bool {
for i := range v1 {
if !FloatEqualThreshold32(v1[i], v2[i], threshold) {
return false
}
}
return true
}
func (v1 Vec4f) ApproxEqualTheshold(v2 Vec4f, threshold float32) bool {
for i := range v1 {
if !FloatEqualThreshold32(v1[i], v2[i], threshold) {
return false
}
}
return true
}
func (v1 Vec2f) ApproxFuncEqual(v2 Vec2f, eq func(float32, float32) bool) bool {
for i := range v1 {
if !eq(v1[i], v2[i]) {
return false
}
}
return true
}
func (v1 Vec3f) ApproxFuncEqual(v2 Vec3f, eq func(float32, float32) bool) bool {
for i := range v1 {
if !eq(v1[i], v2[i]) {
return false
}
}
return true
}
func (v1 Vec4f) ApproxFuncEqual(v2 Vec4f, eq func(float32, float32) bool) bool {
for i := range v1 {
if !eq(v1[i], v2[i]) {
return false
}
}
return true
}