From 20f9625ae2eefe1966fd00d76467dd73815cec69 Mon Sep 17 00:00:00 2001 From: karai17 Date: Sun, 14 Aug 2016 01:29:24 -0300 Subject: [PATCH] Fixed vec2 and vec3 mul/div/scale --- modules/vec2.lua | 41 +++++++++++++++++++++++++++++++---------- modules/vec3.lua | 48 +++++++++++++++++++++++++++++++++++------------- 2 files changed, 66 insertions(+), 23 deletions(-) diff --git a/modules/vec2.lua b/modules/vec2.lua index 0e423af..7e94610 100644 --- a/modules/vec2.lua +++ b/modules/vec2.lua @@ -102,25 +102,25 @@ function vec2.sub(out, a, b) return out end ---- Multiply a vector by a scalar. +--- Multiply a vector by another vector. -- @tparam vec2 out Vector to store the result -- @tparam vec2 a Left hand operant -- @tparam vec2 b Right hand operant -- @treturn vec2 out function vec2.mul(out, a, b) - out.x = a.x * b - out.y = a.y * b + out.x = a.x * b.x + out.y = a.y * b.y return out end ---- Divide one vector by a scalar. +--- Divide a vector by another vector. -- @tparam vec2 out Vector to store the result -- @tparam vec2 a Left hand operant -- @tparam vec2 b Right hand operant -- @treturn vec2 out function vec2.div(out, a, b) - out.x = a.x / b - out.y = a.y / b + out.x = a.x / b.x + out.y = a.y / b.y return out end @@ -196,6 +196,17 @@ function vec2.dist2(a, b) return dx * dx + dy * dy end +--- Scale a vector by a scalar. +-- @tparam vec2 out Vector to store the result +-- @tparam vec2 a Left hand operant +-- @tparam number b Right hand operant +-- @treturn vec2 out +function vec2.scale(out, a, b) + out.x = a.x * b + out.y = a.y * b + return out +end + --- Rotate a vector. -- @tparam vec2 out Vector to store the result -- @tparam vec2 a Vector to rotate @@ -310,14 +321,24 @@ end function vec2_mt.__mul(a, b) assert(vec2.is_vec2(a), "__mul: Wrong argument type for left hand operant. ( expected)") - assert(type(b) == "number", "__mul: Wrong argument type for right hand operant. ( expected)") - return new():mul(a, b) + assert(vec2.is_vec2(b) or type(b) == "number", "__mul: Wrong argument type for right hand operant. ( or expected)") + + if vec2.is_vec2(b) then + return new():mul(a, b) + end + + return new():scale(a, b) end function vec2_mt.__div(a, b) assert(vec2.is_vec2(a), "__div: Wrong argument type for left hand operant. ( expected)") - assert(type(b) == "number", "__div: Wrong argument type for right hand operant. ( expected)") - return new():div(a, b) + assert(vec2.is_vec2(b) or type(b) == "number", "__div: Wrong argument type for right hand operant. ( or expected)") + + if vec2.is_vec2(b) then + return new():div(a, b) + end + + return new():scale(a, 1 / b) end if status then diff --git a/modules/vec3.lua b/modules/vec3.lua index ac43782..befe784 100644 --- a/modules/vec3.lua +++ b/modules/vec3.lua @@ -103,27 +103,27 @@ function vec3.sub(out, a, b) return out end ---- Multiply a vector by a scalar. +--- Multiply a vector by another vectorr. -- @tparam vec3 out Vector to store the result -- @tparam vec3 a Left hand operant --- @tparam number b Right hand operant +-- @tparam vec3 b Right hand operant -- @treturn vec3 out function vec3.mul(out, a, b) - out.x = a.x * b - out.y = a.y * b - out.z = a.z * b + out.x = a.x * b.x + out.y = a.y * b.y + out.z = a.z * b.z return out end --- Divide a vector by a scalar. -- @tparam vec3 out Vector to store the result -- @tparam vec3 a Left hand operant --- @tparam number b Right hand operant +-- @tparam vec3 b Right hand operant -- @treturn vec3 out function vec3.div(out, a, b) - out.x = a.x / b - out.y = a.y / b - out.z = a.z / b + out.x = a.x / b.x + out.y = a.y / b.y + out.z = a.z / b.z return out end @@ -206,6 +206,18 @@ function vec3.dist2(a, b) return dx * dx + dy * dy + dz * dz end +--- Scale a vector by a scalar. +-- @tparam vec3 out Vector to store the result +-- @tparam vec3 a Left hand operant +-- @tparam number b Right hand operant +-- @treturn vec3 out +function vec3.scale(out, a, b) + out.x = a.x * b + out.y = a.y * b + out.z = a.z * b + return out +end + --- Rotate vector about an axis. -- @tparam vec3 out Vector to store the result -- @tparam vec3 a Vector to rotate @@ -324,14 +336,24 @@ end function vec3_mt.__mul(a, b) assert(vec3.is_vec3(a), "__mul: Wrong argument type for left hand operant. ( expected)") - assert(type(b) == "number", "__mul: Wrong argument type for right hand operant. ( expected)") - return new():mul(a, b) + assert(vec3.is_vec3(b) or type(b) == "number", "__mul: Wrong argument type for right hand operant. ( or expected)") + + if vec3.is_vec3(b) then + return new():mul(a, b) + end + + return new():scale(a, b) end function vec3_mt.__div(a, b) assert(vec3.is_vec3(a), "__div: Wrong argument type for left hand operant. ( expected)") - assert(type(b) == "number", "__div: Wrong argument type for right hand operant. ( expected)") - return new():div(a, b) + assert(vec3.is_vec3(b) or type(b) == "number", "__div: Wrong argument type for right hand operant. ( or expected)") + + if vec3.is_vec3(b) then + return new():div(a, b) + end + + return new():scale(a, 1 / b) end if status then