From 0ad5dc26d25055efe55029c1da91d486f04d45ae Mon Sep 17 00:00:00 2001 From: Cogitary <84321459+DoubleDataStack@users.noreply.github.com> Date: Thu, 14 Mar 2024 18:57:08 +0300 Subject: [PATCH] adds cross vec2 --- res/modules/vectors.lua | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/res/modules/vectors.lua b/res/modules/vectors.lua index 20711538..a8b4776e 100644 --- a/res/modules/vectors.lua +++ b/res/modules/vectors.lua @@ -160,7 +160,22 @@ function vector2:dist(vector) local dy = self.y - vector.y local result = vec2(dx, dy) return result:len() - end +end + +-- cross product for vec2 (in 3D space) +-- @param {vec2} v - The other vec2 +-- @return {number} -> float || integer +-- @usage +-- local v1 = vec2(10, 15) +-- local v2 = vec2(15, 10) +-- print(v1:cross(v2)) -- Output: -125 +function vector2:cross(v) + if type(v) == "number" then + print("\n(( TypeError : cross ))\nType arg cross(vec2)") + error("Invalid input argument. Expected a vec2 object.\n") + end + return self.x * v.y - self.y * v.x +end -- rotate vec2 -- @param angle {number} @@ -661,6 +676,9 @@ end -- local v2 = vec2(3, 4) -- print(v1:dot(v2)) -- Output: 11 +-- local v1 = vec2(10, 15) +-- local v2 = vec2(15, 10) +-- print(v1:cross(v2)) -- Output: -125 -- local v1 = vec2(1, 2) -- local v2 = vec2(3, 4) @@ -696,6 +714,13 @@ end -- print(v) -- Output: (2, 2, 3) + + + + + + + -- local v1 = vec3(1, 0, 0) -- local v2 = vec3(2, 0, 0) -- print(v1:isParallel(v2)) -- Output: true