From 133cae587c4385ffbb0eb7f795b77a392677469c Mon Sep 17 00:00:00 2001 From: Starbeamrainbowlabs Date: Sat, 26 Jun 2021 15:39:47 +0100 Subject: [PATCH] Vector3: add move_towards --- .tests/Vector3/move_towards.test.lua | 17 +++++++++++++++++ worldeditadditions/utils/vector3.lua | 8 ++++++++ 2 files changed, 25 insertions(+) create mode 100644 .tests/Vector3/move_towards.test.lua diff --git a/.tests/Vector3/move_towards.test.lua b/.tests/Vector3/move_towards.test.lua new file mode 100644 index 0000000..c48ba82 --- /dev/null +++ b/.tests/Vector3/move_towards.test.lua @@ -0,0 +1,17 @@ +local Vector3 = require("worldeditadditions.utils.vector3") + +-- To find these numbers, in Javascript: +-- function t(x) { return Math.sqrt((x*x)*3); } +-- for(let i = 0; i < 1000000000; i++) { let r = t(i); if(Math.floor(r) === r) console.log(`i ${i}, r ${r}`); } + + +describe("Vector3.move_towards", function() + it("should work with a positive vector", function() + local a = Vector3.new(3, 4, 5) + local b = Vector3.new(10, 10, 10) + assert.are.same( + Vector3.new(5.0022714374157439821, 5.7162326606420661435, 6.4301938838683883048), + a:move_towards(b, 3) + ) + end) +end) diff --git a/worldeditadditions/utils/vector3.lua b/worldeditadditions/utils/vector3.lua index 4526322..17d3152 100644 --- a/worldeditadditions/utils/vector3.lua +++ b/worldeditadditions/utils/vector3.lua @@ -175,6 +175,14 @@ function Vector3.limit_to(a, length) return a:clone() end +--- Return a vector that is amount distance towards b from a. +-- @param a Vector3 The vector to move from. +-- @param b Vector3 The vector to move towards. +-- @param amount number The amount to move. +function Vector3.move_towards(a, b, amount) + return a + (b - a):limit_to(amount) +end + -- ██████ ██████ ███████ ██████ █████ ████████ ██████ ██████ -- ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██