Advertisement
Mangus875

Luau/Roblox Polynomic Bezier Function

Apr 21st, 2023 (edited)
911
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.75 KB | None | 0 0
  1. local function bezier(t: number, ...: Vector2 | Vector3): Vector2 | Vector3
  2.     local points = {...}
  3.    
  4.     --[ TYPE CHECKING ]--
  5.     local dimension = nil
  6.     for _, v in ipairs(points) do
  7.         local d = typeof(v)
  8.         if d ~= "Vector3" and d ~= "Vector2" then
  9.             error("control points must be Vector2 or Vector3 values")
  10.         end
  11.         if not dimension then
  12.             dimension = d
  13.         elseif d ~= dimension then
  14.             error("cannot mix Vector2 and Vector3 control points")
  15.         end
  16.     end
  17.    
  18.     --[ CREATE NEXT SET OF POINTS ]--
  19.     local function lerp(v,a,b)
  20.         return v*(b-a)+a
  21.     end
  22.    
  23.     local pts = {}
  24.     for i = 1, #points-1 do
  25.         local a, b = points[i], points[i+1]
  26.         table.insert(pts, lerp(t, a, b))
  27.     end
  28.     if #pts > 1 then
  29.         return bezier(t, unpack(pts))
  30.     end
  31.     return pts[1]
  32. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement