38 lines
698 B
Lua
38 lines
698 B
Lua
Point = {}
|
|
Point.__index = Point
|
|
|
|
function Point:new(x, y)
|
|
local o = { x = x, y = y }
|
|
setmetatable(o, self)
|
|
return o
|
|
end
|
|
|
|
function Point:__add(other)
|
|
return Point:new(self.x+other.x, self.y+other.y)
|
|
end
|
|
|
|
function Point:__sub(other)
|
|
return Point:new(self.x-other.x, self.y-other.y)
|
|
end
|
|
|
|
function Point:__mul(n)
|
|
return Point:new(self.x*n, self.y*n)
|
|
end
|
|
|
|
function Point:__div(n)
|
|
return Point:new(self.x/n, self.y/n)
|
|
end
|
|
|
|
-- absolute value, or the distance from the origin
|
|
function Point:abs()
|
|
return math.sqrt(self.x ^ 2 + self.y ^ 2)
|
|
end
|
|
|
|
function Point:__tostring()
|
|
return "("..self.x..","..self.y..")"
|
|
end
|
|
|
|
function Point:copy()
|
|
return Point:new(self.x, self.y)
|
|
end
|