summaryrefslogtreecommitdiff
path: root/math.lua
diff options
context:
space:
mode:
Diffstat (limited to 'math.lua')
-rw-r--r--math.lua10
1 files changed, 7 insertions, 3 deletions
diff --git a/math.lua b/math.lua
index 4a13517..dc5ba64 100644
--- a/math.lua
+++ b/math.lua
@@ -1,13 +1,17 @@
Vec2 = {}
+Vec2.__eq = function(a, b) return a.x == b.x and a.y == b.y end
Vec2.__index = Vec2
Vec2.__add = function(a, b) return vec2(a.x + b.x, a.y + b.y) end
Vec2.__unm = function(a) return vec2(-a.x, -a.y) end
Vec2.__sub = function(a, b) return a + (-b) end
Vec2.__mul = function(a, b)
if (type(b) == "number") then return vec2(a.x * b, a.y * b) end -- scalar mult
- return a.x * b.x + a.y * b.y
+ return vec2(a.x * b.x, a.y * b.y)
+end
+Vec2.__div = function(a, b)
+ if (type(b) == "number") then return vec2(a.x / b, a.y / b) end -- scalar mult
+ return vec2(a.x / b.x, a.y / b.y)
end
-Vec2.__div = function(a, b) return vec2(a.x / b.x, a.y / b.y) end
Vec2.__tostring = function(a) return "(" .. fmt(a.x) .. "," .. fmt(a.y) .. ")" end
function Vec2:magnitude()
@@ -17,7 +21,7 @@ end
function Vec2:normal()
local m = self:magnitude()
if m == 0 then return vec2(self.x, self.y) end
- return vec2(self.x / m, self.y / m)
+ return self / m
end
function Vec2:apply(f)