Skip to content

Instantly share code, notes, and snippets.

@gamefreak
Created January 24, 2010 22:02
Show Gist options
  • Save gamefreak/285474 to your computer and use it in GitHub Desktop.
Save gamefreak/285474 to your computer and use it in GitHub Desktop.
function vec(x,y)
local v = {x = x, y = y}
setmetatable(v, _vector)
return v
end
_vector = {
__add = function(a,b)
if type(b) == "table" then
return vec(a.x + b.x, a.y + b.y)
else
return vec(a.x + b, a.y + b)
end
end;
__sub = function(a,b)
if type(b) == "table" then
return vec(a.x - b.x, a.y - b.y)
else
return vec(a.x - b, a.y - b)
end
end;
__mul = function(a, b)--Dot
if type(b) == "table" then
return a.x * b.x + a.y * b.y
else
return vec(a.x * b, a.y * b)
end
end;
__div = function(a, b)
return vec(a.x / b, a.y / b)
end;
__unm = function(a)
return vec(-a.x, -a.y)
end;
__tostring = function(v) return "("..v.x..", "..v.y..")" end;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment