mirror of
https://github.com/sbrl/Minetest-WorldEditAdditions.git
synced 2024-12-23 03:55:01 +00:00
Vector3: add dot product
This commit is contained in:
parent
dc5645cfeb
commit
cfa086ce46
2 changed files with 48 additions and 0 deletions
36
.tests/Vector3/dot.test.lua
Normal file
36
.tests/Vector3/dot.test.lua
Normal file
|
@ -0,0 +1,36 @@
|
|||
local Vector3 = require("worldeditadditions.utils.vector3")
|
||||
|
||||
describe("Vector3.dot", function()
|
||||
it("should work with a positive vector", function()
|
||||
local a = Vector3.new(3, 3, 3)
|
||||
local b = Vector3.new(4, 5, 6)
|
||||
assert.are.equal(
|
||||
45,
|
||||
a:dot(b)
|
||||
)
|
||||
end)
|
||||
it("should work with a negative vector", function()
|
||||
local a = Vector3.new(-4, -4, -4)
|
||||
local b = Vector3.new(4, 5, 6)
|
||||
assert.are.equal(
|
||||
-60,
|
||||
a:dot(b)
|
||||
)
|
||||
end)
|
||||
it("should work with a mixed vector", function()
|
||||
local a = Vector3.new(-3, 3, -3)
|
||||
local b = Vector3.new(7, 8, 9)
|
||||
assert.are.equal(
|
||||
-24,
|
||||
a:dot(b)
|
||||
)
|
||||
end)
|
||||
it("should work with the dot_product alias", function()
|
||||
local a = Vector3.new(-3, 3, -3)
|
||||
local b = Vector3.new(7, 8, 9)
|
||||
assert.are.equal(
|
||||
-24,
|
||||
a:dot_product(b)
|
||||
)
|
||||
end)
|
||||
end)
|
|
@ -162,6 +162,18 @@ function Vector3.length(a)
|
|||
return math.sqrt(a:length_squared())
|
||||
end
|
||||
|
||||
--- Calculates the dot product of this vector and another vector.
|
||||
-- @param a Vector3 The first vector to operate on.
|
||||
-- @param a Vector3 The second vector to operate on.
|
||||
-- @returns number The dot product of this vector as a scalar value.
|
||||
function Vector3.dot(a, b)
|
||||
return a.x * b.x + a.y * b.y + a.z * b.z;
|
||||
end
|
||||
--- Alias of Vector3.dot.
|
||||
function Vector3.dot_product(a, b)
|
||||
return Vector3.dot(a, b)
|
||||
end
|
||||
|
||||
--- Returns a new vector whose length clamped to the given length.
|
||||
-- The direction in which the vector is pointing is not changed.
|
||||
-- @param a Vector3 The vector to operate on.
|
||||
|
|
Loading…
Reference in a new issue