-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvector.wren
45 lines (37 loc) · 1004 Bytes
/
vector.wren
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
class Vector {
construct new(x, y) {
_x = x
_y = y
}
x { _x }
y { _y }
x=(v) { _x = v }
y=(v) { _y = v }
- { Vector.new(-_x, -_y) }
==(other) { _x == other.x && _y == other.y }
!=(other) { _x != other.x || _y != other.y }
+(other) { Vector.new(_x + other.x, _y + other.y) }
-(other) { Vector.new(_x - other.x, _y - other.y) }
*(other) {
if (other is Vector) {
return Vector.new(_x * other.x, _y * other.y)
} else {
return Vector.new(_x * other, _y * other)
}
}
/(other) {
if (other is Vector) {
return Vector.new(_x / other.x, _y / other.y)
} else {
return Vector.new(_x / other, _y / other)
}
}
length { (_x.pow(2) + _y.pow(2)).sqrt }
unit {
if (length == 0) {
return Vector.new(0, 0)
}
return Vector.new(_x / length, _y / length)
}
toString { "Vector(%(_x), %(_y))" }
}