diff --git a/code/point.lua b/code/point.lua new file mode 100644 index 0000000..307566b --- /dev/null +++ b/code/point.lua @@ -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 +