-
Notifications
You must be signed in to change notification settings - Fork 0
/
vector.lua
53 lines (40 loc) · 1.04 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
local Vector = {}
do
local meta = {
_metatable = "Private metatable",
_DESCRIPTION = "Vectors in 2D"
}
meta.__index = meta
function meta:__add(v)
return Vector(self.i + v.i, self.j + v.j)
end
function meta:__sub(v)
return Vector(self.i - v.i, self.j - v.j)
end
function meta:__mul(v)
return self.i * v.i + self.j * v.j
end
function meta:unit()
return Vector(self.i / self:len(), self.j / self:len())
end
function meta:copy()
return Vector(self.i, self.j)
end
function meta:scalar_mul(v)
return Vector(self.i * v, self.j * v)
end
function meta:div(v)
return Vector(self.i / v, self.j / v)
end
function meta:__tostring()
return ("<%g, %g>"):format(self.i, self.j)
end
function meta:len()
return math.sqrt( self * self )
end
setmetatable( Vector, {
__call = function( V, i ,j ) return setmetatable( {i = i, j = j}, meta ) end
} )
end
Vector.__index = Vector
return Vector