-
Notifications
You must be signed in to change notification settings - Fork 0
/
vector2D.lua
71 lines (45 loc) · 966 Bytes
/
vector2D.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
---@module vector
local class={}
local sqrt=math.sqrt
function class.new (y,x)
return setmetatable({y,x},class)
end
function class:__index (i)
if i=="y" then
return self[1]
elseif i=="x" then
return self[2]
else
return class[i]
end
end
function class:__add (b)
if type(b)=="table" then
return setmetatable({self[1]+b[1],self[2]+b[2]},class)
else
return setmetatable({self[1]+b,self[2]+b},class)
end
end
function class:__sub (b)
if type(b)=="table" then
return setmetatable({self[1]-b[1],self[2]-b[2]},class)
else
return setmetatable({self[1]-b,self[2]-b},class)
end
end
function class:__mul (b)
return setmetatable({self.y*b,self.x*b},class)
end
function class:__len ()
local y,x=self[1],self[2]
return sqrt(y*y+x*x)
end
function class:__eq (b)
return self[1]==b[1] and self[2]==b[2]
end
function class:__le (b)
return self:__len() < b:__len()
end
function class:__le (b)
return self:__len() <= b:__len()
end