From 88214aef57cc27493beb8a3c14e64dc1309cd0ad Mon Sep 17 00:00:00 2001 From: Starbeamrainbowlabs Date: Sat, 26 Jun 2021 17:02:35 +0100 Subject: [PATCH] Vector3: add min_component, max_component --- .tests/Vector3/max_component.test.lua | 39 +++++++++++++++++++++++++++ .tests/Vector3/min_component.test.lua | 39 +++++++++++++++++++++++++++ worldeditadditions/utils/vector3.lua | 16 +++++++++++ 3 files changed, 94 insertions(+) create mode 100644 .tests/Vector3/max_component.test.lua create mode 100644 .tests/Vector3/min_component.test.lua diff --git a/.tests/Vector3/max_component.test.lua b/.tests/Vector3/max_component.test.lua new file mode 100644 index 0000000..3c74bab --- /dev/null +++ b/.tests/Vector3/max_component.test.lua @@ -0,0 +1,39 @@ +local Vector3 = require("worldeditadditions.utils.vector3") + +describe("Vector3.max_component", function() + it("should work with a positive vector x", function() + local a = Vector3.new(30, 4, 5) + assert.are.equal( + 30, + a:max_component() + ) + end) + it("should work with a positive vector y", function() + local a = Vector3.new(3, 10, 5) + assert.are.equal( + 10, + a:max_component() + ) + end) + it("should work with a positive vector z", function() + local a = Vector3.new(3, 1, 50.5) + assert.are.equal( + 50.5, + a:max_component() + ) + end) + it("should work with a negative vector", function() + local a = Vector3.new(-4, -5, -1) + assert.are.equal( + -1, + a:max_component() + ) + end) + it("should work with a mixed vector", function() + local a = Vector3.new(-30, 3, -3) + assert.are.equal( + 3, + a:max_component() + ) + end) +end) diff --git a/.tests/Vector3/min_component.test.lua b/.tests/Vector3/min_component.test.lua new file mode 100644 index 0000000..d1e8fc4 --- /dev/null +++ b/.tests/Vector3/min_component.test.lua @@ -0,0 +1,39 @@ +local Vector3 = require("worldeditadditions.utils.vector3") + +describe("Vector3.min_component", function() + it("should work with a positive vector x", function() + local a = Vector3.new(3, 4, 5) + assert.are.equal( + 3, + a:min_component() + ) + end) + it("should work with a positive vector y", function() + local a = Vector3.new(3, 1, 5) + assert.are.equal( + 1, + a:min_component() + ) + end) + it("should work with a positive vector z", function() + local a = Vector3.new(3, 1, 0.5) + assert.are.equal( + 0.5, + a:min_component() + ) + end) + it("should work with a negative vector", function() + local a = Vector3.new(-4, -5, -46) + assert.are.equal( + -46, + a:min_component() + ) + end) + it("should work with a mixed vector", function() + local a = Vector3.new(-30, 3, -3) + assert.are.equal( + -30, + a:min_component() + ) + end) +end) diff --git a/worldeditadditions/utils/vector3.lua b/worldeditadditions/utils/vector3.lua index 3d4bcc5..fa6115e 100644 --- a/worldeditadditions/utils/vector3.lua +++ b/worldeditadditions/utils/vector3.lua @@ -229,6 +229,22 @@ function Vector3.move_towards(a, b, amount) return a + (b - a):limit_to(amount) end +--- Returns the value of the minimum component of the vector. +-- Returns a scalar value. +-- @param a Vector3 The vector to operate on. +-- @returns number The value of the minimum component of the vector. +function Vector3.min_component(a) + return math.min(a.x, a.y, a.z) +end + +--- Returns the value of the maximum component of the vector. +-- Returns a scalar value. +-- @param a Vector3 The vector to operate on. +-- @returns number The value of the maximum component of the vector. +function Vector3.max_component(a) + return math.max(a.x, a.y, a.z) +end + -- ██████ ██████ ███████ ██████ █████ ████████ ██████ ██████ -- ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██