-
Notifications
You must be signed in to change notification settings - Fork 2
/
vector.lua
96 lines (75 loc) · 2.14 KB
/
vector.lua
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
local Vector = Object:extend()
function Vector:new(x, y, z)
self.x = x or 0
self.y = y or 0
self.z = z or 0
end
function Vector:__tostring()
return string.format("Vector(%.2f, %.2f, %.2f)", self.x, self.y, self.z)
end
function Vector:__add(other)
return Vector(self.x + other.x, self.y + other.y, self.z + other.z)
end
function Vector:__sub(other)
return Vector(self.x - other.x, self.y - other.y, self.z - other.z)
end
function Vector:__mul(other)
return Vector(self.x * other, self.y * other, self.z * other)
end
function Vector:__div(other)
return Vector(self.x / other, self.y / other, self.z / other)
end
function Vector:__unm()
return Vector(-self.x, -self.y, -self.z)
end
function Vector:__eq(other)
return self.x == other.x and self.y == other.y and self.z == other.z
end
function Vector:__lt(other)
return self.x < other.x and self.y < other.y and self.z < other.z
end
function Vector:__le(other)
return self.x <= other.x and self.y <= other.y and self.z <= other.z
end
function Vector:length()
return math.sqrt(self.x * self.x + self.y * self.y + self.z * self.z)
end
function Vector:clone()
return Vector(self.x, self.y, self.z)
end
function Vector:unpack()
return self.x, self.y, self.z
end
function Vector:dot(other)
return self.x * other.x + self.y * other.y + self.z * other.z
end
function Vector:cross(other)
return Vector(
self.y * other.z - self.z * other.y,
self.z * other.x - self.x * other.z,
self.x * other.y - self.y * other.x
)
end
function Vector:normalize()
local length = self:length()
self.x = self.x / length
self.y = self.y / length
self.z = self.z / length
end
function Vector:normalized()
local length = self:length()
if length == 0 then return Vector(0, 0, 0) end
return Vector(self.x / length, self.y / length, self.z / length)
end
function Vector:floored()
return Vector(math.floor(self.x), math.floor(self.y), math.floor(self.z))
end
function Vector:rotated(angle)
local c = math.cos(angle)
local s = math.sin(angle)
return Vector(self.x * c + self.z * s, self.y, self.x * s - self.z * c)
end
function Vector:table()
return {self.x, self.y, self.z}
end
return Vector