added Point class
This commit is contained in:
parent
3d41699d8f
commit
e648705e5b
30
code/point.lua
Normal file
30
code/point.lua
Normal file
@ -0,0 +1,30 @@
|
||||
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
|
||||
|
Loading…
Reference in New Issue
Block a user