-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathvector.go
110 lines (89 loc) · 3.13 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package collision2d
import (
"fmt"
"math"
)
//Vector is a simple 2D vector/point struct.
type Vector struct {
X, Y float64
}
func (vector Vector) String() string {
return fmt.Sprintf("{X:%f, Y:%f}\n", vector.X, vector.Y)
}
//NewVector create a new vector with the values of x and y
func NewVector(x, y float64) Vector {
return Vector{X: x, Y: y}
}
//Copy the value of another vector to a new one.
func (vector Vector) Copy(another Vector) Vector {
return Vector{another.X, another.Y}
}
//Clone this vector coordinates to a new vector with the same coordinates as this one.
func (vector Vector) Clone() Vector {
return Vector{}.Copy(vector)
}
//Perp returns a new vector perpendicular from this one.
func (vector Vector) Perp() Vector {
return Vector{vector.Y, -vector.X}
}
//Rotate returns a new vector rotated counter-clockwise by the specified number of radians.
func (vector Vector) Rotate(angle float64) Vector {
return Vector{
vector.X*math.Cos(angle) - vector.Y*math.Sin(angle),
vector.X*math.Sin(angle) + vector.Y*math.Cos(angle)}
}
//Reverse returns a new vector that is reversed from this one.
func (vector Vector) Reverse() Vector {
return Vector{-vector.X, -vector.Y}
}
//Normalize returns a new unit-length vector.
func (vector Vector) Normalize() Vector {
d := vector.Len()
return Vector{vector.X / d, vector.Y / d}
}
//Add returns a new vector with the result of adding another vector to this one.
func (vector Vector) Add(another Vector) Vector {
return Vector{vector.X + another.X, vector.Y + another.Y}
}
//Sub returns a new vector that is the result of subtracting another vector from this one.
func (vector Vector) Sub(another Vector) Vector {
return Vector{vector.X - another.X, vector.Y - another.Y}
}
//Scale returns a new vector scaled in the direction of X and Y by value x
func (vector Vector) Scale(x float64) Vector {
return Vector{vector.X * x, vector.Y * x}
}
//ScaleDifferent returns a new vector scaled in the direction of X and Y by value x and y respectively
func (vector Vector) ScaleDifferent(x, y float64) Vector {
return Vector{vector.X * x, vector.Y * y}
}
//Project this vector onto another one
func (vector Vector) Project(another Vector) Vector {
amt := vector.Dot(another) / another.Len2()
return Vector{amt * another.X, amt * another.Y}
}
//ProjectN this vector onto a unit vector
func (vector Vector) ProjectN(another Vector) Vector {
amt := vector.Dot(another)
return Vector{amt * another.X, amt * another.Y}
}
//Reflect this vector on an arbitrary axis vector
func (vector Vector) Reflect(axis Vector) Vector {
return vector.Project(axis).Scale(2).Sub(vector)
}
//ReflectN this vector on an arbitrary axis unit vector
func (vector Vector) ReflectN(axis Vector) Vector {
return vector.ProjectN(axis).Scale(2).Sub(vector)
}
//Dot returns the dot product of this vector and another
func (vector Vector) Dot(another Vector) float64 {
return vector.X*another.X + vector.Y*another.Y
}
//Len2 returns the squared length of this vector
func (vector Vector) Len2() float64 {
return vector.Dot(vector)
}
//Len returns the length of this vector
func (vector Vector) Len() float64 {
return math.Sqrt(vector.Len2())
}