ello
I made a half working cart about bezier curves and wanted to see what people think about my code, so if you want to pls give feedback
note: you can add points by using the up and down arrow keys then move the points by using the mouse and cycle between them using X and O
--bezier curve time function clone(t)--clone a table return{unpack(t)} end function lerp(s,e,m) return s*(1-m)+e*m end function lerp2d(p1,p2,t)--lerp lx=lerp(p1[1],p2[1],t)--two 2d ly=lerp(p1[2],p2[2],t)--points return lx,ly end poke(0x5f2d,1) --devkit mouse p1={64,64} --first point ptt={ p1} --point table pts=0 --number of desired points ind=1 --index used for selected point t=.5 --time value used across lerps function lerpcheck()--lerp the points po1=lerp2d(ptt[1],ptt[2],t) l=#ptt-2 for i=0,#ptt-1,1 do if(ptt[i+2]~=nil)lx,ly=lerp2d(ptt[i+1],ptt[i+2],t) --lx stands for lerped x circ(lx,ly,2,11) if(llx and lx~=po1)line(llx,lly,lx,ly,11) llx=lx --last lerped x lly=ly l-=1 end if l>0 then lx2,ly2=lerp2d({lx,ly},{llx,lly},t) circ(lx2,ly2,2,12) line(lx,ly,lx2,ly2,12) l-=1 end end function drawp(p)--visualize points circ(p[1],p[2],3,7) if lp==nil then --last point elseif p~=p1 then line(lp[1],lp[2],p[1],p[2],7) lerpcheck(lp,p,t) end lp=p --update most recent point end function respts()--called when for i=1,pts,1do--points get added pt=clone(p1)--or removed add(ptt,pt) end end function _update60() mx=stat(32)my=stat(33) if(btnp(🅾️)and ind>1)ind-=1--change selected if(btnp(❎)and ind<#ptt)ind+=1--point if(btnp(⬆️))pts+=1ptt={ p1}respts()--add/remove if(btnp(⬇️))pts-=1ptt={ p1}respts()--points if(btnp(⬅️)and t>.05)t-=.05 if(btnp(➡️)and t<.95)t+=.05 end function _draw() cls() spr(0,mx,my) sp=ptt[ind] --selected point if(sp)?"\^i#"..ind.." selected",sp[1]+5,sp[2]+5,7 foreach(ptt,drawp) if(stat(34)~=0 and ptt[ind])then sp[1]=mx --move point sp[2]=my end end |
ello, ty for posting it (I'm using pico8 edu edition)
O basically selects the next point, X the previous point, the arrow up/down keys change the amount of points and reset the existing one's positions
another thing I forgor was the left and right arrow keys: they change the T value all across the lerp functions
[Please log in to post a comment]