From e648705e5be4bac5a438f935d163d63d32e560c7 Mon Sep 17 00:00:00 2001 From: binarycat Date: Fri, 11 Mar 2022 13:16:45 -0500 Subject: [PATCH] added Point class --- code/point.lua | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 code/point.lua 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 +