' data_bezier.pb — Cubic Bezier curve computation
#COMPILE EXE
#DIM ALL

FUNCTION PBMAIN() AS LONG
    LOCAL px0 AS DOUBLE
    LOCAL py0 AS DOUBLE
    LOCAL px1 AS DOUBLE
    LOCAL py1 AS DOUBLE
    LOCAL px2 AS DOUBLE
    LOCAL py2 AS DOUBLE
    LOCAL px3 AS DOUBLE
    LOCAL py3 AS DOUBLE
    LOCAL t AS DOUBLE
    LOCAL xVal AS DOUBLE
    LOCAL yVal AS DOUBLE
    LOCAL oneMinusT AS DOUBLE
    LOCAL b0 AS DOUBLE
    LOCAL b1 AS DOUBLE
    LOCAL b2 AS DOUBLE
    LOCAL b3 AS DOUBLE
    LOCAL steps AS LONG
    LOCAL i AS LONG
    LOCAL j AS LONG
    LOCAL totalPoints AS LONG
    LOCAL sampleX AS LONG
    LOCAL sampleY AS LONG
    LOCAL curvePointsX(200) AS LONG
    LOCAL curvePointsY(200) AS LONG
    LOCAL numPoints AS LONG
    LOCAL iter AS LONG

    px0 = 0.0
    py0 = 0.0
    px1 = 100.0
    py1 = 200.0
    px2 = 300.0
    py2 = 50.0
    px3 = 400.0
    py3 = 150.0

    steps = 20

    PRINT "=== Cubic Bezier Curve ==="
    PRINT "P0=(" + STR$(px0) + "," + STR$(py0) + ")"
    PRINT "P1=(" + STR$(px1) + "," + STR$(py1) + ")"
    PRINT "P2=(" + STR$(px2) + "," + STR$(py2) + ")"
    PRINT "P3=(" + STR$(px3) + "," + STR$(py3) + ")"
    PRINT "Steps: " + STR$(steps)

    FOR i = 0 TO steps
        t = i / steps
        oneMinusT = 1.0 - t

        b0 = oneMinusT * oneMinusT * oneMinusT
        b1 = 3.0 * t * oneMinusT * oneMinusT
        b2 = 3.0 * t * t * oneMinusT
        b3 = t * t * t

        xVal = b0 * px0 + b1 * px1 + b2 * px2 + b3 * px3
        yVal = b0 * py0 + b1 * py1 + b2 * py2 + b3 * py3

        sampleX = xVal
        sampleY = yVal
        curvePointsX(numPoints) = sampleX
        curvePointsY(numPoints) = sampleY
        numPoints = numPoints + 1

        PRINT "  t=" + STR$(t) + " (" + STR$(sampleX) + "," + STR$(sampleY) + ")"
    NEXT i

    PRINT "Total curve points: " + STR$(numPoints)
    PRINT "=== End Bezier ==="
    FUNCTION = 0
END FUNCTION
