Log In  

Cart [#bezzy-0#] | Code | 2019-07-18 | No License | Embed

Hi guys,

I was working on a train game when it hit me that i have no idea how to make a smooth curve.
So I came across bezier curves and after reading some math articles I managed to write a function myself.
Even though many other people have shared thier version here is my bezier curve, with only a single control point sadly (I will figure out the rest later).

Here is the code i have tried to make it very eazy to implement into other projects (since I was gonna use it for my train haha).


function lv(v1,v2,t)
    return (1-t)*v1+t*v2
end

--Quadratic Bezier Curve Vector
function qbcvector(v1,v2,v3,t) 
    return  lv(lv(v1,v3,t), lv(v3,v2,t),t)
end

--draw Quadratic Bezier Curve
--x1,y1 = starting point 
--x2,y2 = end point
--x3,y3 = 3rd manipulating point 
--n = "amount of pixels in curve"(just put it higher than you expect)
--c = color
function drawqbc(x1,y1,x2,y2,x3,y3,n,c)
    for i = 1,n do 
        local t = i/n
       pset(qbcvector(x1,x2,x3,t),qbcvector(y1,y2,y3,t),c)
    end
end

Cart [#bezzy_cubic-0#] | Code | 2019-07-19 | No License | Embed

update: ive added cubic aswell here is the code:

function lv(v1,v2,t)
    return (1-t)*v1+t*v2
end

--Quadratic Bezier Curve Vector
function qbcvector(v1,v2,v3,t) 
    return  lv(lv(v1,v3,t), lv(v3,v2,t),t)
end

-- cubic bezier curve vector
function cbcvector(v1,v2,v3,v4,t) 
    return  lv(qbcvector(v1,v2,v3,t), qbcvector(v1,v2,v4,t),t)
end
--draw cubic bezier curve
function drawcbc(x1,y1,x2,y2,x3,y3,x4,y4,n,c)
    for i = 1,n do 
        local t = i/n
        pset(cbcvector(x1,x2,x3,x4,t),cbcvector(y1,y2,y3,y4,t),c)
    end
end
P#65926 2019-07-18 20:21 ( Edited 2019-07-19 11:14)


[Please log in to post a comment]

About | Contact | Updates | Terms of Use
Follow Lexaloffle:        
Generated 2019-09-21 14:14 | 0.026s | 4194k | Q:39