-
Notifications
You must be signed in to change notification settings - Fork 0
/
vector2.go
206 lines (169 loc) · 4.65 KB
/
vector2.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package pearl
import (
"math"
"reflect"
)
// The Vector2 struct
type Vector2 struct {
X float64
Y float64
}
// Returns the magnitude of a Vector2 type
func (v *Vector2) Magnitude() float64 {
return math.Sqrt(v.X * v.X + v.Y * v.Y)
}
// Normalizes Vector2
func (v *Vector2) Normalize() {
magnitude := v.Magnitude()
if magnitude > 0 {
v.Divide(magnitude)
}
}
// Adds Vector2 and other (changes Vector2)
func (v *Vector2) Add(other interface {}) {
t := reflect.TypeOf(other).String()
if t == "pearl.Vector2" {
otherVector := other.(Vector2)
v.X += otherVector.X
v.Y += otherVector.Y
} else {
val := reflect.Indirect(reflect.ValueOf(other))
otherValue := val.Convert(reflect.TypeOf(float64(0))).Float()
v.X += otherValue
v.Y += otherValue
}
}
// Subtracts Vector2 and other (changes Vector2)
func (v *Vector2) Subtract(other interface {}) {
t := reflect.TypeOf(other).String()
if t == "pearl.Vector2" {
otherVector := other.(Vector2)
v.X -= otherVector.X
v.Y -= otherVector.Y
} else {
val := reflect.Indirect(reflect.ValueOf(other))
otherValue := val.Convert(reflect.TypeOf(float64(0))).Float()
v.X -= otherValue
v.Y -= otherValue
}
}
// Multiplies Vector2 and other (changes Vector2)
func (v *Vector2) Multiply(other interface {}) {
t := reflect.TypeOf(other).String()
if t == "pearl.Vector2" {
otherVector := other.(Vector2)
v.X *= otherVector.X
v.Y *= otherVector.Y
} else {
val := reflect.Indirect(reflect.ValueOf(other))
otherValue := val.Convert(reflect.TypeOf(float64(0))).Float()
v.X *= otherValue
v.Y *= otherValue
}
}
// Divides Vector2 and other (changes Vector2)
func (v *Vector2) Divide(other interface {}) {
t := reflect.TypeOf(other).String()
if t == "pearl.Vector2" {
otherVector := other.(Vector2)
v.X /= otherVector.X
v.Y /= otherVector.Y
} else {
val := reflect.Indirect(reflect.ValueOf(other))
otherValue := val.Convert(reflect.TypeOf(float64(0))).Float()
v.X /= otherValue
v.Y /= otherValue
}
}
// Floors Vector2 (changes Vector2)
func (v *Vector2) Vector2Floor() {
v.X = float64(int64(v.X))
v.Y = float64(int64(v.Y))
}
// Returns a copy of the Vector2 normalized (doesn't change Vector2)
func Normalized(v Vector2) Vector2 {
v.Normalize()
return v
}
// Adds b to vector. returns result
func Vector2Add(vector Vector2, other interface {}) Vector2 {
result := Vector2 { vector.X, vector.Y }
t := reflect.TypeOf(other).String()
if t == "pearl.Vector2" {
otherVector := other.(Vector2)
result.X += otherVector.X
result.Y += otherVector.Y
} else {
val := reflect.Indirect(reflect.ValueOf(other))
otherValue := val.Convert(reflect.TypeOf(float64(0))).Float()
result.X += otherValue
result.Y += otherValue
}
return result
}
// Subtracts a and b (doesn't change variables)
func Vector2Subtract(vector Vector2, other interface {}) Vector2 {
result := Vector2 { vector.X, vector.Y }
t := reflect.TypeOf(other).String()
if t == "pearl.Vector2" {
otherVector := other.(Vector2)
result.X -= otherVector.X
result.Y -= otherVector.Y
} else {
val := reflect.Indirect(reflect.ValueOf(other))
otherValue := val.Convert(reflect.TypeOf(float64(0))).Float()
result.X -= otherValue
result.Y -= otherValue
}
return result
}
// Multiplies a and b (doesn't change variables)
func Vector2Multiply(vector Vector2, other interface {}) Vector2 {
result := Vector2 { vector.X, vector.Y }
t := reflect.TypeOf(other).String()
if t == "pearl.Vector2" {
otherVector := other.(Vector2)
result.X *= otherVector.X
result.Y *= otherVector.Y
} else {
val := reflect.Indirect(reflect.ValueOf(other))
otherValue := val.Convert(reflect.TypeOf(float64(0))).Float()
result.X *= otherValue
result.Y *= otherValue
}
return result
}
// Divides a and b (doesn't change variables)
func Vector2Divide(vector Vector2, other interface {}) Vector2 {
result := Vector2 { vector.X, vector.Y }
t := reflect.TypeOf(other).String()
if t == "pearl.Vector2" {
otherVector := other.(Vector2)
result.X /= otherVector.X
result.Y /= otherVector.Y
} else {
val := reflect.Indirect(reflect.ValueOf(other))
otherValue := val.Convert(reflect.TypeOf(float64(0))).Float()
result.X /= otherValue
result.Y /= otherValue
}
return result
}
// Returns Vector2 floored (doesn't change Vector2)
func Vector2Floor(vector Vector2) Vector2 {
return Vector2 { float64(int64(vector.X)), float64(int64(vector.Y)) }
}
var (
// Shorthand for 0, 0
VZERO = Vector2 { 0, 0 }
// Shorthand for 1, 1
VONE = Vector2 { 1, 1 }
// Shorthand for 0, -1
VUP = Vector2 { 0, -1 }
// Shorthand for 0, 1
VDOWN = Vector2 { 0, 1 }
// Shorthand for -1, 0
VLEFT = Vector2 { -1, 0 }
// Shorthand for 1, 0
VRIGHT = Vector2 { 1, 0 }
)