Advertisement
Mangus875

Bezier Maker

Apr 20th, 2023 (edited)
981
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.25 KB | None | 0 0
  1. local Vector2 = {}
  2. local v2mt = {}
  3. function Vector2.new(X, Y)
  4.     return setmetatable({x = X or 0, y = Y or 0}, v2mt)
  5. end
  6. v2mt.__add = function(myVector, otherVector)
  7.     myVector.x = myVector.x + otherVector.x
  8.     myVector.y = myVector.y + otherVector.y
  9.     return myVector
  10. end
  11. v2mt.__sub = function(myVector, otherVector)
  12.     myVector.x = myVector.x - otherVector.x
  13.     myVector.y = myVector.y - otherVector.y
  14.     return myVector
  15. end
  16. v2mt.__mul = function(scalar, myVector)
  17.     myVector.x = myVector.x * scalar
  18.     myVector.y = myVector.y * scalar
  19.     return myVector
  20. end
  21. v2mt.__tostring = function(mytable)
  22.     return "("..mytable.x..", "..mytable.y..")"
  23. end
  24. v2mt.__call = function(mytable)
  25.     return Vector2.new(mytable.x, mytable.y)
  26. end
  27.  
  28. local function bezier(t, ...)
  29.     local points = {...}
  30.    
  31.     local function lerp(v,a,b)
  32.         return v*(b()-a())+a()
  33.     end
  34.    
  35.     local pts = {}
  36.     for i = 1, #points-1, 1 do
  37.         local a,b = points[i],points[i+1]
  38.         table.insert(pts, lerp(t, a, b))
  39.     end
  40.     if #pts > 1 then
  41.         return bezier(t, table.unpack(pts))
  42.     end
  43.     return table.unpack(pts)
  44. end
  45.  
  46. local pts = {
  47.     Vector2.new(-1,0),
  48.     Vector2.new(0,1),
  49.     Vector2.new(0,-1),
  50.     Vector2.new(1,0)
  51. }
  52. local bez = bezier(0.5, table.unpack(pts))
  53. print(bez)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement