' bezier_curves.pb — Bezier curves (quadratic and cubic)
#COMPILE EXE
#DIM ALL

FUNCTION PBMAIN() AS LONG
    LOCAL quadX0 AS DOUBLE
    LOCAL quadY0 AS DOUBLE
    LOCAL quadX1 AS DOUBLE
    LOCAL quadY1 AS DOUBLE
    LOCAL quadX2 AS DOUBLE
    LOCAL quadY2 AS DOUBLE
    LOCAL cubicX0 AS DOUBLE
    LOCAL cubicY0 AS DOUBLE
    LOCAL cubicX1 AS DOUBLE
    LOCAL cubicY1 AS DOUBLE
    LOCAL cubicX2 AS DOUBLE
    LOCAL cubicY2 AS DOUBLE
    LOCAL cubicX3 AS DOUBLE
    LOCAL cubicY3 AS DOUBLE
    LOCAL t AS DOUBLE
    LOCAL xVal AS DOUBLE
    LOCAL yVal AS DOUBLE
    LOCAL oneMinusT AS DOUBLE
    LOCAL steps AS LONG
    LOCAL i AS LONG
    LOCAL j AS LONG
    LOCAL numCurves AS LONG
    LOCAL iter AS LONG
    LOCAL quadPtsX(50) AS LONG
    LOCAL quadPtsY(50) AS LONG
    LOCAL cubicPtsX(50) AS LONG
    LOCAL cubicPtsY(50) AS LONG
    LOCAL numQuadPts AS LONG
    LOCAL numCubicPts AS LONG
    LOCAL arcLenQuad AS DOUBLE
    LOCAL arcLenCubic AS DOUBLE
    LOCAL dx AS DOUBLE
    LOCAL dy AS DOUBLE
    LOCAL sampleX AS LONG
    LOCAL sampleY AS LONG
    LOCAL curveType AS LONG

    steps = 15

    PRINT "=== Bezier Curves ==="

    quadX0 = 0.0
    quadY0 = 0.0
    quadX1 = 100.0
    quadY1 = 200.0
    quadX2 = 200.0
    quadY2 = 0.0

    PRINT "Quadratic Bezier:"
    PRINT "  P0=(" + STR$(quadX0) + "," + STR$(quadY0) + ")"
    PRINT "  P1=(" + STR$(quadX1) + "," + STR$(quadY1) + ")"
    PRINT "  P2=(" + STR$(quadX2) + "," + STR$(quadY2) + ")"

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

        xVal = oneMinusT * oneMinusT * quadX0
        xVal = xVal + 2.0 * oneMinusT * t * quadX1
        xVal = xVal + t * t * quadX2

        yVal = oneMinusT * oneMinusT * quadY0
        yVal = yVal + 2.0 * oneMinusT * t * quadY1
        yVal = yVal + t * t * quadY2

        sampleX = xVal
        sampleY = yVal
        quadPtsX(numQuadPts) = sampleX
        quadPtsY(numQuadPts) = sampleY
        numQuadPts = numQuadPts + 1

        IF i > 0 THEN
            dx = xVal - quadPtsX(numQuadPts - 2)
            dy = yVal - quadPtsY(numQuadPts - 2)
            IF dx < 0 THEN dx = 0 - dx
            IF dy < 0 THEN dy = 0 - dy
            arcLenQuad = arcLenQuad + dx + dy
        END IF

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

    cubicX0 = 0.0
    cubicY0 = 0.0
    cubicX1 = 50.0
    cubicY1 = 150.0
    cubicX2 = 150.0
    cubicY2 = 150.0
    cubicX3 = 200.0
    cubicY3 = 0.0

    PRINT "Cubic Bezier:"
    PRINT "  P0=(" + STR$(cubicX0) + "," + STR$(cubicY0) + ")"
    PRINT "  P1=(" + STR$(cubicX1) + "," + STR$(cubicY1) + ")"
    PRINT "  P2=(" + STR$(cubicX2) + "," + STR$(cubicY2) + ")"
    PRINT "  P3=(" + STR$(cubicX3) + "," + STR$(cubicY3) + ")"

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

        xVal = oneMinusT * oneMinusT * oneMinusT * cubicX0
        xVal = xVal + 3.0 * t * oneMinusT * oneMinusT * cubicX1
        xVal = xVal + 3.0 * t * t * oneMinusT * cubicX2
        xVal = xVal + t * t * t * cubicX3

        yVal = oneMinusT * oneMinusT * oneMinusT * cubicY0
        yVal = yVal + 3.0 * t * oneMinusT * oneMinusT * cubicY1
        yVal = yVal + 3.0 * t * t * oneMinusT * cubicY2
        yVal = yVal + t * t * t * cubicY3

        sampleX = xVal
        sampleY = yVal
        cubicPtsX(numCubicPts) = sampleX
        cubicPtsY(numCubicPts) = sampleY
        numCubicPts = numCubicPts + 1

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

    PRINT "Quadratic points: " + STR$(numQuadPts)
    PRINT "Cubic points: " + STR$(numCubicPts)
    PRINT "=== End Bezier ==="
    FUNCTION = 0
END FUNCTION
