' Circle Drawing — midpoint circle algorithm on character grid
#COMPILE EXE
#DIM ALL

FUNCTION PBMAIN() AS LONG
    LOCAL grid(2500) AS LONG
    LOCAL width AS LONG
    LOCAL height AS LONG
    LOCAL cx AS LONG
    LOCAL cy AS LONG
    LOCAL radius AS LONG
    LOCAL x AS LONG
    LOCAL y AS LONG
    LOCAL d AS LONG
    LOCAL i AS LONG
    LOCAL j AS LONG
    LOCAL idx AS LONG
    LOCAL lineStr AS STRING
    width = 50
    height = 25
    PRINT "============================================"
    PRINT "      CIRCLE DRAWING (Midpoint Algorithm)"
    PRINT "============================================"
    PRINT ""
    FOR i = 0 TO width * height - 1
        grid(i) = 0
    NEXT i
    cx = 25
    cy = 12
    radius = 10
    x = 0
    y = radius
    d = 3 - 2 * radius
    DO WHILE x <= y
        IF cx + x < width AND cy + y < height THEN
            idx = (cy + y) * width + (cx + x)
            grid(idx) = 1
        END IF
        IF cx - x >= 0 AND cy + y < height THEN
            idx = (cy + y) * width + (cx - x)
            grid(idx) = 1
        END IF
        IF cx + x < width AND cy - y >= 0 THEN
            idx = (cy - y) * width + (cx + x)
            grid(idx) = 1
        END IF
        IF cx - x >= 0 AND cy - y >= 0 THEN
            idx = (cy - y) * width + (cx - x)
            grid(idx) = 1
        END IF
        IF cx + y < width AND cy + x < height THEN
            idx = (cy + x) * width + (cx + y)
            grid(idx) = 1
        END IF
        IF cx - y >= 0 AND cy + x < height THEN
            idx = (cy + x) * width + (cx - y)
            grid(idx) = 1
        END IF
        IF cx + y < width AND cy - x >= 0 THEN
            idx = (cy - x) * width + (cx + y)
            grid(idx) = 1
        END IF
        IF cx - y >= 0 AND cy - x >= 0 THEN
            idx = (cy - x) * width + (cx - y)
            grid(idx) = 1
        END IF
        IF d < 0 THEN
            d = d + 4 * x + 6
        ELSE
            d = d + 4 * (x - y) + 10
            y = y - 1
        END IF
        x = x + 1
    LOOP
    PRINT "Circle at (25,12) radius 10:"
    PRINT ""
    FOR j = 0 TO height - 1
        lineStr = ""
        FOR i = 0 TO width - 1
            idx = j * width + i
            IF grid(idx) = 1 THEN
                lineStr = lineStr + "#"
            ELSE
                lineStr = lineStr + "."
            END IF
        NEXT i
        PRINT lineStr
    NEXT j
    PRINT ""
    FOR i = 0 TO width * height - 1
        grid(i) = 0
    NEXT i
    cx = 25
    cy = 12
    radius = 5
    x = 0
    y = radius
    d = 3 - 2 * radius
    DO WHILE x <= y
        IF cx + x < width AND cy + y < height THEN
            idx = (cy + y) * width + (cx + x)
            grid(idx) = 1
        END IF
        IF cx - x >= 0 AND cy + y < height THEN
            idx = (cy + y) * width + (cx - x)
            grid(idx) = 1
        END IF
        IF cx + x < width AND cy - y >= 0 THEN
            idx = (cy - y) * width + (cx + x)
            grid(idx) = 1
        END IF
        IF cx - x >= 0 AND cy - y >= 0 THEN
            idx = (cy - y) * width + (cx - x)
            grid(idx) = 1
        END IF
        IF cx + y < width AND cy + x < height THEN
            idx = (cy + x) * width + (cx + y)
            grid(idx) = 1
        END IF
        IF cx - y >= 0 AND cy + x < height THEN
            idx = (cy + x) * width + (cx - y)
            grid(idx) = 1
        END IF
        IF cx + y < width AND cy - x >= 0 THEN
            idx = (cy - x) * width + (cx + y)
            grid(idx) = 1
        END IF
        IF cx - y >= 0 AND cy - x >= 0 THEN
            idx = (cy - x) * width + (cx - y)
            grid(idx) = 1
        END IF
        IF d < 0 THEN
            d = d + 4 * x + 6
        ELSE
            d = d + 4 * (x - y) + 10
            y = y - 1
        END IF
        x = x + 1
    LOOP
    PRINT "Small circle at (25,12) radius 5:"
    PRINT ""
    FOR j = 0 TO height - 1
        lineStr = ""
        FOR i = 0 TO width - 1
            idx = j * width + i
            IF grid(idx) = 1 THEN
                lineStr = lineStr + "#"
            ELSE
                lineStr = lineStr + "."
            END IF
        NEXT i
        PRINT lineStr
    NEXT j
    PRINT ""
    FOR i = 0 TO width * height - 1
        grid(i) = 0
    NEXT i
    cx = 25
    cy = 12
    radius = 15
    x = 0
    y = radius
    d = 3 - 2 * radius
    DO WHILE x <= y
        IF cx + x < width AND cy + y < height THEN
            idx = (cy + y) * width + (cx + x)
            grid(idx) = 1
        END IF
        IF cx - x >= 0 AND cy + y < height THEN
            idx = (cy + y) * width + (cx - x)
            grid(idx) = 1
        END IF
        IF cx + x < width AND cy - y >= 0 THEN
            idx = (cy - y) * width + (cx + x)
            grid(idx) = 1
        END IF
        IF cx - x >= 0 AND cy - y >= 0 THEN
            idx = (cy - y) * width + (cx - x)
            grid(idx) = 1
        END IF
        IF cx + y < width AND cy + x < height THEN
            idx = (cy + x) * width + (cx + y)
            grid(idx) = 1
        END IF
        IF cx - y >= 0 AND cy + x < height THEN
            idx = (cy + x) * width + (cx - y)
            grid(idx) = 1
        END IF
        IF cx + y < width AND cy - x >= 0 THEN
            idx = (cy - x) * width + (cx + y)
            grid(idx) = 1
        END IF
        IF cx - y >= 0 AND cy - x >= 0 THEN
            idx = (cy - x) * width + (cx - y)
            grid(idx) = 1
        END IF
        IF d < 0 THEN
            d = d + 4 * x + 6
        ELSE
            d = d + 4 * (x - y) + 10
            y = y - 1
        END IF
        x = x + 1
    LOOP
    PRINT "Large circle at (25,12) radius 15:"
    PRINT ""
    FOR j = 0 TO height - 1
        lineStr = ""
        FOR i = 0 TO width - 1
            idx = j * width + i
            IF grid(idx) = 1 THEN
                lineStr = lineStr + "#"
            ELSE
                lineStr = lineStr + "."
            END IF
        NEXT i
        PRINT lineStr
    NEXT j
    PRINT ""
    FOR i = 0 TO width * height - 1
        grid(i) = 0
    NEXT i
    cx = 25
    cy = 12
    FOR radius = 2 TO 10 STEP 2
        x = 0
        y = radius
        d = 3 - 2 * radius
        DO WHILE x <= y
            IF cx + x < width AND cy + y < height THEN
                idx = (cy + y) * width + (cx + x)
                grid(idx) = 1
            END IF
            IF cx - x >= 0 AND cy + y < height THEN
                idx = (cy + y) * width + (cx - x)
                grid(idx) = 1
            END IF
            IF cx + x < width AND cy - y >= 0 THEN
                idx = (cy - y) * width + (cx + x)
                grid(idx) = 1
            END IF
            IF cx - x >= 0 AND cy - y >= 0 THEN
                idx = (cy - y) * width + (cx - x)
                grid(idx) = 1
            END IF
            IF cx + y < width AND cy + x < height THEN
                idx = (cy + x) * width + (cx + y)
                grid(idx) = 1
            END IF
            IF cx - y >= 0 AND cy + x < height THEN
                idx = (cy + x) * width + (cx - y)
                grid(idx) = 1
            END IF
            IF cx + y < width AND cy - x >= 0 THEN
                idx = (cy - x) * width + (cx + y)
                grid(idx) = 1
            END IF
            IF cx - y >= 0 AND cy - x >= 0 THEN
                idx = (cy - x) * width + (cx - y)
                grid(idx) = 1
            END IF
            IF d < 0 THEN
                d = d + 4 * x + 6
            ELSE
                d = d + 4 * (x - y) + 10
                y = y - 1
            END IF
            x = x + 1
        LOOP
    NEXT radius
    PRINT "Concentric circles:"
    PRINT ""
    FOR j = 0 TO height - 1
        lineStr = ""
        FOR i = 0 TO width - 1
            idx = j * width + i
            IF grid(idx) = 1 THEN
                lineStr = lineStr + "#"
            ELSE
                lineStr = lineStr + "."
            END IF
        NEXT i
        PRINT lineStr
    NEXT j
    PRINT ""
    PRINT "Circle drawing complete"
    FUNCTION = 0
END FUNCTION
