Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function love.load()
- nElapsed=0
- nLoopTime=4
- nSegments=100
- nLoopsDone=0
- nNodeSize=5
- tPoints=setmetatable({{x=0,y=0},{x=5*10,y=10*10}},{__call=function(self,x,y)
- local minlen,num=((x-self[1].x)^2+(y-self[1].y)^2)^0.5,1
- for i=1,#self do
- local len=((x-self[i].x)^2+(y-self[i].y)^2)^0.5
- if len<minlen then
- minlen=len
- num=i
- end
- end
- return num,minlen
- end})
- tCurvePts={}
- tColorLevels={}
- end
- local function curvify(tPts,bStore,level)
- level=level or 1
- local tResultantPts={}
- local delta=(nElapsed%nLoopTime)/nLoopTime
- if #tPts==2 then
- return (tPts[1].x-tPts[2].x)*delta+tPts[2].x,(tPts[1].y-tPts[2].y)*delta+tPts[2].y
- elseif #tPts<2 then
- return
- else
- for i=1,#tPts-2 do
- if i==1 then
- 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})
- end
- 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})
- if not bStore then
- if not tColorLevels[level] then
- tColorLevels[level]={math.random(100,255),math.random(100,255),math.random(100,255)}
- end
- love.graphics.setColor(unpack(tColorLevels[level]))
- love.graphics.line(
- tResultantPts[#tResultantPts-1].x,
- tResultantPts[#tResultantPts-1].y,
- tResultantPts[#tResultantPts].x,
- tResultantPts[#tResultantPts].y
- )
- end
- end
- return curvify(tResultantPts,bStore,level+1)
- end
- end
- function love.mousepressed(x,y,button)
- local nearest,nDist=tPoints(x,y)
- if button=="l" and nDist<=nNodeSize then
- nMoving=nearest
- elseif button=="l" then
- table.insert(tPoints,{x=x,y=y})
- nMoving=#tPoints
- elseif button=="r" and nMoving then
- table.insert(tPoints,nMoving,{x=x,y=y})
- nMoving=nMoving+1
- elseif button=="r" and #tPoints>1 then
- table.remove(tPoints,nearest)
- end
- end
- function love.mousereleased(x,y,button)
- if button=="l" then
- nMoving=nil
- end
- end
- function love.update(nSec)
- nElapsed=nElapsed+nSec
- if nMoving then
- tPoints[nMoving].x=love.mouse.getX()
- tPoints[nMoving].y=love.mouse.getY()
- end
- if (nElapsed-nElapsed%nLoopTime)/nLoopTime>nLoopsDone then tCurvePts={{tPoints[#tPoints].x,tPoints[#tPoints].y}} nLoopsDone=nLoopsDone+1 end
- if nElapsed%nLoopTime>=nLoopTime*(#tCurvePts+1)/nSegments then
- table.insert(tCurvePts,{curvify(tPoints,true)})
- end
- end
- function love.draw()
- love.graphics.setColor(255,255,255)
- for i=1,#tPoints-1 do
- love.graphics.line(tPoints[i].x,tPoints[i].y,tPoints[i+1].x,tPoints[i+1].y)
- end
- local x,y=curvify(tPoints)
- love.graphics.setColor(255,0,0)
- --love.graphics.circle("fill",x,y,2,20)
- for i=1,#tCurvePts-1 do
- love.graphics.line(tCurvePts[i][1],tCurvePts[i][2],tCurvePts[i+1][1],tCurvePts[i+1][2])
- end
- if not nMoving then
- love.graphics.setColor(0,0,255)
- local nearest,nDist=tPoints(love.mouse.getPosition())
- love.graphics.circle("fill",tPoints[nearest].x,tPoints[nearest].y,nDist<=nNodeSize and nNodeSize or nNodeSize/2,20)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment