forked from FreddieRidell/wren-vector
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvector.wren
More file actions
347 lines (291 loc) · 7.24 KB
/
vector.wren
File metadata and controls
347 lines (291 loc) · 7.24 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
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/// Multi-dimensional vector with various operators.
///
/// Copyright:
/// Copyright © 2017 Freddie Ridell
/// Copyright © 2024 Chance Snow
/// License: MIT
/// See: https://github.com/FreddieRidell/wren-vector#readme
/// See: `Vector`
class Base {
/// Number of elements in this `Vector`.
arity { _arity }
toString {
if (_arity == 2) return "[%(this[0]), %(this[1])]"
if (_arity == 3) return "[%(this[0]), %(this[1]), %(this[2])]"
if (_arity == 4) return "[%(this[0]), %(this[1]), %(this[2]), %(this[3])]"
}
magnitude {
var acc = 0
for (i in 0..._arity) {
acc = acc + (this[i] * this[i])
}
return acc.sqrt
}
/// Normalize this vector.
normalize { this / this.magnitude }
/// ditto
normalized { this.normalize }
/// ditto
normalise { this.normalize }
/// Computes the L2 (Euclidean) norm of a point.
/// Returns: Num
/// See: [Norm (mathematics): Euclidean norm](https://en.wikipedia.org/wiki/Norm_(mathematics)#Euclidean_norm) on Wikipedia
norm {
return _values.map {|n| n * n }.reduce(0) {|sum, x| sum + x }.sqrt
}
abs {
var v = Base.ofArity(_arity)
for (i in 0..._arity) {
v[i] = this[i].abs
}
return v
}
floor {
var v = Base.ofArity(_arity)
for (i in 0..._arity) {
v[i] = this[i].floor
}
return v
}
ceil {
var v = Base.ofArity(_arity)
for (i in 0..._arity) {
v[i] = this[i].ceil
}
return v
}
inverse {
var v = Base.ofArity(_arity)
for (i in 0..._arity) {
v[i] = -this[i]
}
return v
}
[ i ] {
if (i == 0) {
return _values[0]
}
if (i == 1) {
return _values[1]
}
if (i == 2 && _arity > 2) {
return _values[2]
}
if (i == 3 && _arity > 2) {
return _values[3]
}
return null
}
[ i ]=(val) {
if (i == 0) {
_values[0] = val
}
if (i == 1) {
_values[1] = val
}
if (i == 2 && _arity > 2) {
_values[2] = val
}
if (i == 3 && _arity > 2) {
_values[3] = val
}
return null
}
== (other) {
if (_arity != other.arity) {
Fiber.abort("ArgumentError: Can not compare vectors of un-equal arity.")
}
var acc = true
for (i in 0..._arity) {
acc = acc && this[i] == other[i]
}
return acc
}
+ (other) {
if (_arity != other.arity) {
Fiber.abort("ArgumentError: Can not add vectors of un-equal arity.")
}
var v = Base.ofArity(_arity)
for (i in 0..._arity) {
v[i] = this[i] + other[i]
}
return v
}
- (other) {
if (_arity != other.arity) {
Fiber.abort("ArgumentError: Can not subtract vectors of un-equal arity.")
}
var v = Base.ofArity(_arity)
for (i in 0..._arity) {
v[i] = this[i] - other[i]
}
return v
}
* (other) {
if (other is Vector) {
if (_arity != other.arity) {
Fiber.abort("ArgumentError: Can not divide vectors of un-equal arity.")
}
var v = Base.ofArity(_arity)
for (i in 0..._arity) {
v[i] = this[i] * other[i]
}
return v
}
if (other is Num) {
var v = Base.ofArity(_arity)
for (i in 0..._arity) {
v[i] = this[i] * other
}
return v
}
Fiber.abort("ArgumentError: Can only multiply a Vector by a Num or another Vector.")
}
/ (other) {
if (other is Vector) {
if (_arity != other.arity) {
Fiber.abort("ArgumentError: Can not divide vectors of un-equal arity.")
}
var v = Base.ofArity(_arity)
for (i in 0..._arity) {
v[i] = this[i] / other[i]
}
return v
}
if (other is Num) {
var v = Base.ofArity(_arity)
for (i in 0..._arity) {
v[i] = this[i] / other
}
return v
}
Fiber.abort("ArgumentError: Can only divide a Vector by a Num or another Vector.")
}
/// Calculate a dot product.
/// Params: other: Vector
/// Returns: Num
dot(other) {
if (_arity != other.arity) {
Fiber.abort("ArgumentError: Can not dot product vectors of un-equal arity.")
}
var acc = 0
for (i in 0..._arity) {
acc = acc + (this[i] * other[i] )
}
return acc
}
/// Calculate a cross product.
/// Params: other: Vector
/// Returns: Vector
cross(other) {
if (_arity != 3) {
Fiber.abort("ArgumentError: Can only cross product vectors of arity 3.")
}
var x = (this[1] * other[2]) - (this[2] * other[1])
var y = (this[2] * other[0]) - (this[0] * other[2])
var z = (this[0] * other[1]) - (this[1] * other[0])
return Vector.new(x, y, z)
}
construct new(x, y) {
_arity = 2
_values = [x, y]
}
construct new(x, y, z) {
_arity = 3
_values = [x, y, z]
}
construct new(x, y, z, w) {
_arity = 4
_values = [x, y, z, w]
}
static ofArity(n) {
if (n == 2) return Vector.new(0, 0)
if (n == 3) return Vector.new(0, 0, 0)
if (n == 4) return Vector.new(0, 0, 0, 0)
}
}
class Vector is Base {
x { this[0] }
y { this[1] }
z { this[2] }
w { this[3] }
x=(val) { this[0] = val }
y=(val) { this[1] = val }
z=(val) { this[2] = val }
w=(val) { this[3] = val }
construct new(x, y) {
super(x, y)
}
construct new(x, y, z) {
super(x, y, z)
}
construct new(x, y, z, w) {
super(x, y, z, w)
}
/// Orthogonally rotate this 2D vector clockwise.
/// Returns: Vector
///
/// Warning:
/// Descarte assumes a right-hand coordinate system.
///
/// Positive angles are counter-clockwise if z-axis points offscreen.
orthogonalRight {
if (this.arity != 2) Fiber.abort("ArgumentError: Can only orthogonally rotate vector of arity 2.")
return Vector.new(self.y, -self.x)
}
/// Orthogonally rotate this 2D vector counter-clockwise.
/// Returns: Vector
///
/// Warning:
/// Descarte assumes a right-hand coordinate system.
///
/// Positive angles are counter-clockwise if z-axis points offscreen.
orthogonalLeft {
if (this.arity != 2) Fiber.abort("ArgumentError: Can only orthogonally rotate vector of arity 2.")
return this.inverse.orthogonalRight
}
/// Section: Angles
/// Angle-related linear algebra.
/// Params: other: Vector
/// Returns: Num
angleTo(other) {
var theta = this.dot(other) / (this.norm * other.norm)
return theta.min(1.0).max(-1.0).acos
}
/// Params:
/// direction: Vector
/// other: Vector
/// Returns: Num
angleAlongTo(direction, b) {
var simpleAngle = this.angleTo(b)
var linearDirection = (b - a).normalized
if (direction.dot(linearDirection) >= 0) return simpleAngle
return 2.0 * Num.pi - simpleAngle
}
/// Params: other: Vector
/// Returns: Num
signedAngleTo(other) {
if (this.arity != 2) Fiber.abort("ArgumentError: Can only calculate a signed angle between vectors of arity 2.")
// See https://stackoverflow.com/a/2150475
var det = a.x * b.y - a.y * b.x
var dot = a.x * b.x + a.y * b.y
return det.atan(dot)
}
}
/// A 4D color with red, green, blue, and alpha components.
class Color is Base {
r { this[0] }
g { this[1] }
b { this[2] }
a { this[3] }
r=(val) { this[0] = val }
g=(val) { this[1] = val }
b=(val) { this[2] = val }
a=(val) { this[3] = val }
construct new(r, g, b) {
super(r, g, b)
}
construct new(r, g, b, a) {
super(r, g, b, a)
}
}