This is de Casteljau’s algorithm in Luau, which is used for linear interpolation at Bézier curves. How can I improve it further in terms of efficiency/performance?
Original code:
local function getCF(points:{CFrame}, ratio:number):CFrame
repeat
local ntb:{CFrame} = {}
for i, v in ipairs(points) do
if i ~= 1 then
ntb[i-1] = points[i-1]:Lerp(v, ratio)
end
end
points = ntb
until #points == 1
return points[1]
end
Already improved a bit:
local function getCF(points:{CFrame}, t:number):CFrame
local copy = {unpack(points)}
repeat
for i, v in ipairs(copy) do
if i ~= 1 then
copy[i-1] = copy[i-1]:Lerp(v, t)
end
end
if #copy ~= 1 then
copy[#copy] = nil
end
until #copy == 1
return copy[1]
end