Fixed vec2 and vec3 mul/div/scale

This commit is contained in:
karai17 2016-08-14 01:29:24 -03:00
parent a5d4413cd4
commit 20f9625ae2
2 changed files with 66 additions and 23 deletions

View File

@ -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. (<cpml.vec2> expected)")
assert(type(b) == "number", "__mul: Wrong argument type for right hand operant. (<number> expected)")
return new():mul(a, b)
assert(vec2.is_vec2(b) or type(b) == "number", "__mul: Wrong argument type for right hand operant. (<cpml.vec2> or <number> 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. (<cpml.vec2> expected)")
assert(type(b) == "number", "__div: Wrong argument type for right hand operant. (<number> expected)")
return new():div(a, b)
assert(vec2.is_vec2(b) or type(b) == "number", "__div: Wrong argument type for right hand operant. (<cpml.vec2> or <number> expected)")
if vec2.is_vec2(b) then
return new():div(a, b)
end
return new():scale(a, 1 / b)
end
if status then

View File

@ -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. (<cpml.vec3> expected)")
assert(type(b) == "number", "__mul: Wrong argument type for right hand operant. (<number> expected)")
return new():mul(a, b)
assert(vec3.is_vec3(b) or type(b) == "number", "__mul: Wrong argument type for right hand operant. (<cpml.vec3> or <number> 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. (<cpml.vec3> expected)")
assert(type(b) == "number", "__div: Wrong argument type for right hand operant. (<number> expected)")
return new():div(a, b)
assert(vec3.is_vec3(b) or type(b) == "number", "__div: Wrong argument type for right hand operant. (<cpml.vec3> or <number> expected)")
if vec3.is_vec3(b) then
return new():div(a, b)
end
return new():scale(a, 1 / b)
end
if status then