KaoSDlanor

Bézier curve generator.love

Feb 14th, 2014
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.99 KB | None | 0 0
  1. function love.load()
  2.     nElapsed=0
  3.     nLoopTime=4
  4.     nSegments=100
  5.     nLoopsDone=0
  6.     nNodeSize=5
  7.     tPoints=setmetatable({{x=0,y=0},{x=5*10,y=10*10}},{__call=function(self,x,y)
  8.         local minlen,num=((x-self[1].x)^2+(y-self[1].y)^2)^0.5,1
  9.         for i=1,#self do
  10.             local len=((x-self[i].x)^2+(y-self[i].y)^2)^0.5
  11.             if len<minlen then
  12.                 minlen=len
  13.                 num=i
  14.             end
  15.         end
  16.         return num,minlen
  17.     end})
  18.     tCurvePts={}
  19.     tColorLevels={}
  20. end
  21.  
  22. local function curvify(tPts,bStore,level)
  23.     level=level or 1
  24.     local tResultantPts={}
  25.     local delta=(nElapsed%nLoopTime)/nLoopTime
  26.     if #tPts==2 then
  27.         return (tPts[1].x-tPts[2].x)*delta+tPts[2].x,(tPts[1].y-tPts[2].y)*delta+tPts[2].y
  28.     elseif #tPts<2 then
  29.         return
  30.     else
  31.         for i=1,#tPts-2 do
  32.             if i==1 then
  33.                 table.insert(tResultantPts,{x=(tPts[i].x-tPts[i+1].x)*delta+tPts[i+1].x,y=(tPts[i].y-tPts[i+1].y)*delta+tPts[i+1].y})
  34.             end
  35.             table.insert(tResultantPts,{x=(tPts[i+1].x-tPts[i+2].x)*delta+tPts[i+2].x,y=(tPts[i+1].y-tPts[i+2].y)*delta+tPts[i+2].y})
  36.             if not bStore then
  37.                 if not tColorLevels[level] then
  38.                     tColorLevels[level]={math.random(100,255),math.random(100,255),math.random(100,255)}
  39.                 end
  40.                 love.graphics.setColor(unpack(tColorLevels[level]))
  41.                 love.graphics.line(
  42.                     tResultantPts[#tResultantPts-1].x,
  43.                     tResultantPts[#tResultantPts-1].y,
  44.                     tResultantPts[#tResultantPts].x,
  45.                     tResultantPts[#tResultantPts].y
  46.                 )
  47.             end
  48.         end
  49.         return curvify(tResultantPts,bStore,level+1)
  50.     end
  51. end
  52.  
  53. function love.mousepressed(x,y,button)
  54.     local nearest,nDist=tPoints(x,y)
  55.     if button=="l" and nDist<=nNodeSize then
  56.         nMoving=nearest
  57.     elseif button=="l" then
  58.         table.insert(tPoints,{x=x,y=y})
  59.         nMoving=#tPoints
  60.     elseif button=="r" and nMoving then
  61.         table.insert(tPoints,nMoving,{x=x,y=y})
  62.         nMoving=nMoving+1
  63.     elseif button=="r" and #tPoints>1 then
  64.         table.remove(tPoints,nearest)
  65.     end
  66. end
  67.  
  68. function love.mousereleased(x,y,button)
  69.     if button=="l" then
  70.         nMoving=nil
  71.     end
  72. end
  73.  
  74. function love.update(nSec)
  75.     nElapsed=nElapsed+nSec
  76.  
  77.     if nMoving then
  78.         tPoints[nMoving].x=love.mouse.getX()
  79.         tPoints[nMoving].y=love.mouse.getY()
  80.     end
  81.  
  82.     if (nElapsed-nElapsed%nLoopTime)/nLoopTime>nLoopsDone then tCurvePts={{tPoints[#tPoints].x,tPoints[#tPoints].y}} nLoopsDone=nLoopsDone+1 end
  83.     if nElapsed%nLoopTime>=nLoopTime*(#tCurvePts+1)/nSegments then
  84.         table.insert(tCurvePts,{curvify(tPoints,true)})
  85.     end
  86. end
  87.  
  88. function love.draw()
  89.     love.graphics.setColor(255,255,255)
  90.     for i=1,#tPoints-1 do
  91.         love.graphics.line(tPoints[i].x,tPoints[i].y,tPoints[i+1].x,tPoints[i+1].y)
  92.     end
  93.     local x,y=curvify(tPoints)
  94.     love.graphics.setColor(255,0,0)
  95.     --love.graphics.circle("fill",x,y,2,20)
  96.     for i=1,#tCurvePts-1 do
  97.         love.graphics.line(tCurvePts[i][1],tCurvePts[i][2],tCurvePts[i+1][1],tCurvePts[i+1][2])
  98.     end
  99.     if not nMoving then
  100.         love.graphics.setColor(0,0,255)
  101.         local nearest,nDist=tPoints(love.mouse.getPosition())
  102.         love.graphics.circle("fill",tPoints[nearest].x,tPoints[nearest].y,nDist<=nNodeSize and nNodeSize or nNodeSize/2,20)
  103.     end
  104. end
Advertisement
Add Comment
Please, Sign In to add comment