I have a tempoary solution scripted in lua, but that doesn't just cut it.
My curren't solution:
Code: Select all
point = {}
function IsPoint(self,assrt)
if assrt then
assert(type(self) == "table")
assert(type(self.x) == "number")
assert(type(self.y) == "number")
assert(getmetatable(self))
for k,v in pairs(getmetatable(self)) do
assert(v == point.mt[k])
end
else
if type(self) ~= "table" or type(self.x) ~= "number" or type(self.y) ~= "number" then return false end
for k,v in pairs(getmetatable(self)) do
if v ~= self[k] then return false end
end
return true
end
end
function Point(x,y)
local obj = {x = x or 0, y = y or 0}
setmetatable(obj, point.mt)
IsPoint(obj,true)
return obj
end
point.mt = {
__index = point,
__add = function(a,b)
IsPoint(a,true) IsPoint(b,true)
return Point(a.x+b.x,a.y+b.y)
end,
__sub = function(a,b)
IsPoint(a,true) IsPoint(b,true)
return Point(a.x-b.x,a.y-b.y)
end,
__unm = function(a)
return Point(-a.x,-b.y)
end,
__mul = function(a,b)
if type(a) == "number" then
return Point(a*b.x,a*b.y)
end
if type(b) == "number" then
return Point(a.x*b,a.y*b)
end
IsPoint(a,true) IsPoint(a,true)
return Point(a.x*b.x,a.y*b.y)
end,
__div = function(a,b)
if type(a) == "number" then
return Point(a/b.x,a/b.y)
end
if type(b) == "number" then
return Point(a.x/b,a.y/b)
end
IsPoint(a,true) IsPoint(b,true)
return Point(a.x/b.x,a.y/b.y)
end,
__concat = function(a,b)
if IsPoint(a) then
if IsPoint(b) then
return "("..a.x..","..a.y..")".."("..b.x..","..b.y..")"
end
return "("..a.x..","..a.y..")"..b
end
if IsPoint(b) then
return a.."("..b.x..","..b.y..")"
end
end,
}
function point:dist(p)
p = p or Point()
IsPoint(p,true)
local res = self-p
return math.sqrt(res.x^2+res.y^2)
end
function point:normal(p)
p = p or Point()
IsPoint(p,true)
return self/self:dist()+p
end
function point:u()
return self.x,self.y
end