-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvector2.go
More file actions
74 lines (61 loc) · 1.61 KB
/
vector2.go
File metadata and controls
74 lines (61 loc) · 1.61 KB
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
// Basic 2d vector struct and calculations for Go language
// Author Paul Brace
// July 2024
package vector2
import "math"
// Struct for a 2d vector with associated methods
type Vector struct {
X float64
Y float64
}
// Calculate the magnitude/length of the vector v
func (v *Vector) Length() float64{
length := math.Sqrt((v.X * v.X) + (v.Y * v.Y))
return length
}
// Removes length of vector v but retains ratio between X and Y
func (v *Vector) Normalize() {
// calculate vector length
length := v.Length()
v.DivideByScalar(length)
}
// Adds vector v2 to vector v
func (v *Vector) Add(v2 Vector) {
v.X += v2.X
v.Y += v2.Y
}
// Deducts vector v2 from v
func (v *Vector) Subtract(v2 Vector) {
v.X -= v2.X
v.Y -= v2.Y
}
// Multiplies vector v by a scalar
func (v *Vector) MultiplyByScalar(scalar float64) {
v.X *= scalar
v.Y *= scalar
}
// Divides vector v by a scalar
func (v *Vector) DivideByScalar(scalar float64) {
v.X /= scalar
v.Y /= scalar
}
// Calculates the Dot value of vector v
func (v *Vector) Dot(v2 Vector) float64{
return v.X * v2.X + v.Y * v2.Y
}
// Calculates the angle between vectors v and v2
func (v *Vector) AngleTo(v2 Vector) float64 {
angle := math.Acos(v.Dot(v2) / (v.Length() * v2.Length()))
return angle
}
// Calculates the distance from vector v to v2
func (v *Vector) DistanceTo(v2 Vector) float64 {
angle := math.Sqrt(math.Pow(v2.X - v.X, 2) + math.Pow(v2.Y - v.Y, 2))
return angle
}
// Returns the rotation required for an image at vector v to "point towards" vector v2
func (v *Vector) PointTowards (v2 Vector) float64 {
dx := v.X - v2.X
dy := v.Y - v2.Y
return math.Atan2(-dx, dy)
}