mirror of
https://github.com/tanema/light_world.lua.git
synced 2024-12-24 20:24:19 +00:00
forgot to add a file
This commit is contained in:
parent
7e6e3f23df
commit
5369a25a5a
46
lib/vector.lua
Normal file
46
lib/vector.lua
Normal file
@ -0,0 +1,46 @@
|
||||
local vector = {}
|
||||
vector.__index = vector
|
||||
|
||||
local function new(x, y)
|
||||
if type(x) == "table" then
|
||||
return setmetatable({
|
||||
x = x[1],
|
||||
y = y[1]
|
||||
}, vector)
|
||||
else
|
||||
return setmetatable({
|
||||
x = x or 0,
|
||||
y = y or 0
|
||||
}, vector)
|
||||
end
|
||||
end
|
||||
|
||||
function vector.__add(a, b)
|
||||
return new(a.x + b.x, a.y + b.y)
|
||||
end
|
||||
|
||||
function vector.__sub(a, b)
|
||||
return new(a.x - b.x, a.y - b.y)
|
||||
end
|
||||
|
||||
function vector.__mul(a, b)
|
||||
if type(b) == "number" then
|
||||
return new(a.x * b, a.y * b)
|
||||
else
|
||||
return a.x * b.x + a.y * b.y
|
||||
end
|
||||
end
|
||||
|
||||
function vector.__div(a, b)
|
||||
return new(a.x / b, a.y / b)
|
||||
end
|
||||
|
||||
function vector.__eq(a, b)
|
||||
return a.x == b.x and a.y == b.y
|
||||
end
|
||||
|
||||
function vector:dist(b)
|
||||
return math.sqrt(math.pow(b.x - self.x, 2) + math.pow(b.y-self.y, 2))
|
||||
end
|
||||
|
||||
return setmetatable({new = new}, {__call = function(_, ...) return new(...) end})
|
Loading…
Reference in New Issue
Block a user