From 5369a25a5a846dbee1c33911de37c8ecd818eea0 Mon Sep 17 00:00:00 2001 From: Tim Anema Date: Sun, 21 Dec 2014 10:40:26 -0500 Subject: [PATCH] forgot to add a file --- lib/vector.lua | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 lib/vector.lua diff --git a/lib/vector.lua b/lib/vector.lua new file mode 100644 index 0000000..da10ba9 --- /dev/null +++ b/lib/vector.lua @@ -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})