From 449bf861d49387c33df2f16d38edaa9bf12b51f8 Mon Sep 17 00:00:00 2001 From: Landon Manning Date: Wed, 9 Sep 2015 23:14:33 -0300 Subject: [PATCH] Added ability to divide a number over a vector. Example: `local s = 1/scale` --- modules/vec2.lua | 10 ++++++++-- modules/vec3.lua | 10 ++++++++-- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/modules/vec2.lua b/modules/vec2.lua index 7140d0f..e21ec19 100644 --- a/modules/vec2.lua +++ b/modules/vec2.lua @@ -78,8 +78,14 @@ function vector.__mul(a,b) end function vector.__div(a,b) - assert(isvector(a) and type(b) == "number", "wrong argument types (expected / )") - return new(a.x / b, a.y / b) + if type(a) == "number" then + return new(a/b.x, a/b.y) + elseif type(b) == "number" then + return new(b/a.x, b/a.y) + else + assert(isvector(a) and isvector(b), "Div: wrong argument types ( or expected)") + return new(a.x/b.x, a.y/b.y) + end end function vector.__eq(a,b) diff --git a/modules/vec3.lua b/modules/vec3.lua index d9b9cf6..afd550b 100644 --- a/modules/vec3.lua +++ b/modules/vec3.lua @@ -88,8 +88,14 @@ function vector.__mul(a,b) end function vector.__div(a,b) - assert(isvector(a) and type(b) == "number", "wrong argument types (expected / )") - return new(a.x / b, a.y / b, a.z / b) + if type(a) == "number" then + return new(a / b.x, a / b.y, a / b.z) + elseif type(b) == "number" then + return new(a.x / b, a.y / b, a.z / b) + else + assert(isvector(a) and isvector(b), "Div: wrong argument types ( or expected)") + return new(a.x/b.x, a.y/b.y, a.z/b.z) + end end function vector.__eq(a,b)