' Ellipse Drawing — midpoint ellipse 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 rx AS LONG
    LOCAL ry AS LONG
    LOCAL x AS LONG
    LOCAL y AS LONG
    LOCAL d1 AS DOUBLE
    LOCAL d2 AS DOUBLE
    LOCAL dx AS LONG
    LOCAL dy AS LONG
    LOCAL i AS LONG
    LOCAL j AS LONG
    LOCAL idx AS LONG
    LOCAL lineStr AS STRING
    width = 50
    height = 25
    PRINT "============================================"
    PRINT "     ELLIPSE DRAWING (Midpoint Algorithm)"
    PRINT "============================================"
    PRINT ""
    FOR i = 0 TO width * height - 1
        grid(i) = 0
    NEXT i
    cx = 25
    cy = 12
    rx = 20
    ry = 10
    x = 0
    y = ry
    d1 = (ry * ry) - (rx * rx * ry) + (0.25 * rx * rx)
    dx = 2 * ry * ry * x
    dy = 2 * rx * rx * y
    DO WHILE dx < dy
        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 d1 < 0 THEN
            x = x + 1
            dx = dx + 2 * ry * ry
            d1 = d1 + dx + ry * ry
        ELSE
            x = x + 1
            y = y - 1
            dx = dx + 2 * ry * ry
            dy = dy - 2 * rx * rx
            d1 = d1 + dx - dy + ry * ry
        END IF
    LOOP
    d2 = ((ry * ry) * ((x + 0.5) * (x + 0.5))) + ((rx * rx) * ((y - 1) * (y - 1))) - (rx * rx * ry * ry)
    DO WHILE y >= 0
        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 d2 > 0 THEN
            y = y - 1
            dy = dy - 2 * rx * rx
            d2 = d2 + rx * rx - dy
        ELSE
            y = y - 1
            x = x + 1
            dx = dx + 2 * ry * ry
            dy = dy - 2 * rx * rx
            d2 = d2 + dx - dy + rx * rx
        END IF
    LOOP
    PRINT "Ellipse at (25,12) rx=20 ry=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
    rx = 10
    ry = 20
    x = 0
    y = ry
    d1 = (ry * ry) - (rx * rx * ry) + (0.25 * rx * rx)
    dx = 2 * ry * ry * x
    dy = 2 * rx * rx * y
    DO WHILE dx < dy
        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 d1 < 0 THEN
            x = x + 1
            dx = dx + 2 * ry * ry
            d1 = d1 + dx + ry * ry
        ELSE
            x = x + 1
            y = y - 1
            dx = dx + 2 * ry * ry
            dy = dy - 2 * rx * rx
            d1 = d1 + dx - dy + ry * ry
        END IF
    LOOP
    d2 = ((ry * ry) * ((x + 0.5) * (x + 0.5))) + ((rx * rx) * ((y - 1) * (y - 1))) - (rx * rx * ry * ry)
    DO WHILE y >= 0
        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 d2 > 0 THEN
            y = y - 1
            dy = dy - 2 * rx * rx
            d2 = d2 + rx * rx - dy
        ELSE
            y = y - 1
            x = x + 1
            dx = dx + 2 * ry * ry
            dy = dy - 2 * rx * rx
            d2 = d2 + dx - dy + rx * rx
        END IF
    LOOP
    PRINT "Vertical ellipse at (25,12) rx=10 ry=20:"
    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
    rx = 15
    ry = 7
    x = 0
    y = ry
    d1 = (ry * ry) - (rx * rx * ry) + (0.25 * rx * rx)
    dx = 2 * ry * ry * x
    dy = 2 * rx * rx * y
    DO WHILE dx < dy
        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 d1 < 0 THEN
            x = x + 1
            dx = dx + 2 * ry * ry
            d1 = d1 + dx + ry * ry
        ELSE
            x = x + 1
            y = y - 1
            dx = dx + 2 * ry * ry
            dy = dy - 2 * rx * rx
            d1 = d1 + dx - dy + ry * ry
        END IF
    LOOP
    d2 = ((ry * ry) * ((x + 0.5) * (x + 0.5))) + ((rx * rx) * ((y - 1) * (y - 1))) - (rx * rx * ry * ry)
    DO WHILE y >= 0
        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 d2 > 0 THEN
            y = y - 1
            dy = dy - 2 * rx * rx
            d2 = d2 + rx * rx - dy
        ELSE
            y = y - 1
            x = x + 1
            dx = dx + 2 * ry * ry
            dy = dy - 2 * rx * rx
            d2 = d2 + dx - dy + rx * rx
        END IF
    LOOP
    PRINT "Wide ellipse at (25,12) rx=15 ry=7:"
    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 "Ellipse drawing complete"
    FUNCTION = 0
END FUNCTION
