Advertisement
qaisjp

Bezier curve (jesse)

Oct 6th, 2013
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.34 KB | None | 0 0
  1. local bezier = {}
  2. bezier.__index = bezier
  3.  
  4. function bezier:curve ( xv, yv ) -- Thx to ma homeboi Rajendro
  5.     local reductor = { __index = function ( self, ind )
  6.         return setmetatable ( { tree = self, level = ind }, { __index = function ( curves, ind )
  7.             return function ( t )
  8.                 local x1, y1 = curves.tree[curves.level-1][ind](t)
  9.                 local x2, y2 = curves.tree[curves.level-1][ind+1](t)
  10.                
  11.                 return x1 + ( x2 - x1 ) * t, y1 + ( y2 - y1 ) * t
  12.             end
  13.         end } )
  14.     end }
  15.        
  16.     local points = { }
  17.     for i = 1, #xv do
  18.         points[i] = function ( t ) return xv[i], yv[i] end
  19.     end
  20.    
  21.     return setmetatable ( { points }, reductor )[#points][1]
  22. end
  23.  
  24. -- TEST
  25.  
  26. local curve = bezier:curve ( { 50, 30, 100 }, { 90, 50, 30 } )
  27.  
  28. addEventHandler ( "onClientRender", root,
  29.     function ( )
  30.         local curves = { }
  31.        
  32.         for i = 0.01, 1, 0.1 do
  33.             local x, y = curve ( i )
  34.            
  35.             table.insert ( curves, { x, y } )
  36.         end
  37.        
  38.         for i,v in ipairs ( curves ) do
  39.             local x, y = unpack ( v )
  40.             local z = getGroundPosition ( x, y, 1000 )
  41.            
  42.             if curves[i+1] then
  43.                 local nx, ny = unpack ( curves[i+1] )
  44.                 local nz = getGroundPosition ( nx, ny, 1000 )
  45.                
  46.                 --dxDrawLine3D ( x, y, z + 0.1, nx, ny, nz + 0.1, tocolor ( 255, 0, 0, 255 ), 4 )
  47.                 dxDrawLine ( x, y, nx, ny, tocolor ( 255, 255, 255, 255 ) )
  48.             end
  49.         end
  50.        
  51.         curves = nil
  52.     end )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement