-
Notifications
You must be signed in to change notification settings - Fork 1
/
vector.go
67 lines (53 loc) · 1.22 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
//some simple vector stuff for moving the snake around
package main
import "math"
type Vector2 struct {
x, y float64
}
//add a vector to this one and return the new vector
func (v1 Vector2) Add(v2 Vector2) Vector2 {
return AddVectors(v1, v2)
}
//add two vectors and return a new one
func AddVectors(v1, v2 Vector2) Vector2 {
v1.x = v1.x + v2.x
v1.y = v1.y + v2.y
return v1
}
//uses constants in main.go
func (v Vector2) ClampToWindow() Vector2 {
switch {
//since the first argument always determines
//the sign, sometimes it is negative and
//we have to take the absolute value
case v.x < 0 || v.x > GameWidth:
v.x = math.Mod(v.x, GameWidth)
if v.x < 0 {
v.x += GameWidth + 1
} else {
v.x += -1
}
case v.y < 0 || v.y > GameHeight:
v.y = math.Mod(v.y, GameHeight)
if v.y < 0 {
v.y += GameHeight + 1
} else {
v.y += -1
}
}
return v
}
//multiplay a vector by a scale
func (v Vector2) Multiply(scale float64) Vector2 {
return MultiplyVector(v, scale)
}
//multiplay a vector by a scale
func MultiplyVector(v Vector2, scale float64) Vector2 {
v.x = v.x * scale
v.y = v.y * scale
return v
}
//reverse the vector, swapping x and y
func (v Vector2) Reverse() Vector2 {
return Vector2{v.y, v.x}
}