-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvector.go
93 lines (75 loc) · 1.46 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
92
93
package rainfall
import "math"
type vec2 struct {
X, Y float64
}
func (v *vec2) Add(other vec2) {
v.X += other.X
v.Y += other.Y
}
func (v *vec2) Mul(other vec2) {
v.X *= other.X
v.Y *= other.Y
}
func (v *vec2) Div(other vec2) {
v.X /= other.X
v.Y /= other.Y
}
func (v *vec2) AddS(scalar float64) {
v.X += scalar
v.Y += scalar
}
func (v *vec2) MulS(scalar float64) {
v.X *= scalar
v.Y *= scalar
}
func (v *vec2) DivS(scalar float64) {
v.X /= scalar
v.Y /= scalar
}
func (v *vec2) Length() float64 {
return float64(math.Sqrt(float64(v.X*v.X + v.Y*v.Y)))
}
// VECTOR 3
type vec3 struct {
X, Y, Z float64
}
func (v *vec3) Add(other vec3) {
v.X += other.X
v.Y += other.Y
v.Z += other.Z
}
// Multiple other Vec3
func (v *vec3) Mul(other vec3) {
v.X *= other.X
v.Y *= other.Y
v.Z *= other.Z
}
func (v *vec3) Div(other vec3) {
v.X /= other.X
v.Y /= other.Y
v.Z /= other.Z
}
// Multiple scalar
func (v *vec3) MulS(scalar float64) {
v.X *= scalar
v.Y *= scalar
v.Z *= scalar
}
// MulR multiplies by other vector and returns a new one
func (v vec3) MulR(other vec3) vec3 {
return vec3{v.X * other.X, v.Y * other.Y, v.Z * other.Z}
}
// AddR adds with other and returns the new one
func (v vec3) AddR(other vec3) vec3 {
return vec3{v.X + other.X, v.Y + other.Y, v.Z + other.Z}
}
func (v *vec3) Length() float64 {
return math.Sqrt(v.X*v.X + v.Y*v.Y + v.Z*v.Z)
}
func (v *vec3) Normalize() {
length := v.Length()
v.X /= length
v.Y /= length
v.Z /= length
}