An example of how to calculate bezier curves.

Happy programming everyone!

[ DrawBezier(...) ]
sx, sy: coord start
asx, asy: coord start anchor
ex, ey: coord end
aex, aey: coord end anchor
segm: number of segments (the higher this is, the more detailed&the slower it becomes)

Code: 
sync on
sync rate 0

`Constants
#constant ML_Start       1
#constant ML_End         2
#constant ML_StartAnchor 3
#constant ML_EndAnchor   4

`Setup
sx# = 100 : asx# = 100
sy# = 100 : asy# = 200
ex# = 600 : aex# = 600
ey# = 600 : aey# = 500

do

    cls
    ink rgb(255, 255, 255)
    DrawBezier(sx#, sy#, asx#, asy#, ex#, ey#, aex#, aey#, 100)
    DrawBezierInfo(sx#, sy#, asx#, asy#, ex#, ey#, aex#, aey#)

    if mouseclick() > 0
        if MLock = 0
            if mousex() < sx# + 2 and mousex() > sx# - 2 and mousey() < sy# + 2 and mousey() > sy# - 2 then MLock = ML_Start
            if mousex() < ex# + 2 and mousex() > ex# - 2 and mousey() < ey# + 2 and mousey() > ey# - 2 then MLock = ML_End
            if mousex() < asx# + 3 and mousex() > asx# - 3 and mousey() < asy# + 3 and mousey() > asy# - 3 then MLock = ML_StartAnchor
            if mousex() < aex# + 3 and mousex() > aex# - 3 and mousey() < aey# + 3 and mousey() > aey# - 3 then MLock = ML_EndAnchor
        endif
    endif

    if MLock > 0
        if mouseclick() = 0 then MLock = 0
        select MLock
            case ML_Start
                sx# = mousex()
                sy# = mousey()
            endcase
            case ML_End
                ex# = mousex()
                ey# = mousey()
            endcase
            case ML_StartAnchor
                asx# = mousex()
                asy# = mousey()
            endcase
            case ML_EndAnchor
                aex# = mousex()
                aey# = mousey()
            endcase
        endselect
    endif

    sync
loop

`[ DrawBezier ]
`sx, sy: coord start
`asx, asy: coord start anchor
`ex, ey: coord end
`aex, aey: coord end anchor
`segm: number of segments (the higher this is, the more detailed&the slower it becomes)
function DrawBezier(sx#, sy#, asx#, asy#, ex#, ey#, aex#, aey#, segm)

    `Calculate constants
    cx# = 3 * (asx# - sx#)
    bx# = (3 * (aex# - asx#)) - cx#
    ax# = ex# - sx# - cx# - bx#
    cy# = 3 * (asy# - sy#)
    by# = (3 * (aey# - asy#)) - cy#
    ay# = ey# - sy# - cy# - by#

    `Draw
    st# = 1.0 / segm
    for t = 1 to segm
        t# = st# * t
        ox# = sx# + BezierFunc(ax#, bx#, cx#, t# - st#)
        oy# = sy# + BezierFunc(ay#, by#, cy#, t# - st#)
        nx# = sx# + BezierFunc(ax#, bx#, cx#, t#)
        ny# = sy# + BezierFunc(ay#, by#, cy#, t#)
        line ox#, oy#, nx#, ny#
    next t
endfunction
function BezierFunc(a#, b#, c#, t#)
    result# = (a# * (t# * t# * t#)) + (b# * (t# * t#)) + (c# * t#)
endfunction result#

`[ DrawBezierInfo ]
function DrawBezierInfo(sx#, sy#, asx#, asy#, ex#, ey#, aex#, aey#)

    `Anchors
    line sx#, sy#, asx#, asy#
    line ex#, ey#, aex#, aey#
    circle asx#, asy#, 5
    circle aex#, aey#, 5
    box sx# - 2, sy# - 2, sx# + 2, sy# + 2
    box ex# - 2, ey# - 2, ex# + 2, ey# + 2
endfunction
0
No votes yet