-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvector.go
61 lines (48 loc) · 1.14 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
package main
import "math"
type Vector2 struct {
x, y float64
}
func (v1 Vector2) Add(v2 Vector2) Vector2 {
return Vector2{v1.x + v2.x, v1.y + v2.y}
}
func (v1 Vector2) Subtract(v2 Vector2) Vector2 {
return Vector2{v1.x - v2.x, v1.y - v2.y}
}
func (v Vector2) Mult(s float64) Vector2 {
return Vector2{v.x * s, v.y * s}
}
func (v Vector2) Length() float64 {
return math.Sqrt((v.x * v.x) + (v.y * v.y))
}
func (v Vector2) Normalized() Vector2 {
f := 1.0 / v.Length()
return Vector2{v.x * f, v.y * f}
}
func (v1 Vector2) DistanceTo(v2 Vector2) float64 {
x := v2.x - v1.x
y := v2.y - v1.y
return math.Sqrt((x * x) + (y * y))
}
func (v Vector2) clampedTo(max float64) Vector2 {
v.x = clamp(v.x, -max, max)
v.y = clamp(v.y, -max, max)
return v
}
func (v Vector2) scale(f float64) Vector2 {
length := v.Length()
if length == 0 {
return v
}
vn := v.Normalized()
return vn.Mult(length * f)
}
type Vector4 struct {
x, y, z, w float32
}
func (v1 Vector4) Add(v2 Vector4) Vector4 {
return Vector4{v1.x + v2.x, v1.y + v2.y, v1.z + v2.z, v1.w + v2.w}
}
func (this Vector4) To_a() []float32 {
return []float32{this.x, this.y, this.z, this.w}
}