Task

Using the data storage type defined on this page for raster images,
write an implementation of the midpoint circle algorithm   (also known as Bresenham's circle algorithm).

(definition on Wikipedia).




include c:\cxpl\codes;          \include 'code' declarations
 
proc Circle(X0, Y0, Radius, Color);     \Display a circle
int  X0, Y0,    \coordinates of center
     Radius,    \radius in (pixels)
     Color;     \line color
int  X, Y, E, U, V;
 
        proc PlotOctants;               \Segment
        [Point(X0+Y, Y0+X, Color);      \ 0
        Point(X0+X, Y0+Y, Color);       \ 1
        Point(X0-X, Y0+Y, Color);       \ 2
        Point(X0-Y, Y0+X, Color);       \ 3
        Point(X0-Y, Y0-X, Color);       \ 4
        Point(X0-X, Y0-Y, Color);       \ 5
        Point(X0+X, Y0-Y, Color);       \ 6
        Point(X0+Y, Y0-X, Color);       \ 7
        ]; \PlotOctants
 
[X:= 0;  Y:= Radius;
U:= 1;
V:= 1 -Radius -Radius;
E:= 1 -Radius;
while X < Y do
        [PlotOctants;
        if E < 0 then
                [U:= U+2;  V:= V+2;  E:= E+U]
        else    [U:= U+2;  V:= V+4;  E:= E+V;  Y:= Y-1];
        X:= X+1;
        ];
if X = Y then PlotOctants;
]; \Circle
 
[SetVid($112);          \640x480 in 24-bit RGB color
Circle(110, 110, 50, $FFFF00);
if ChIn(1) then [];     \wait for keystroke
SetVid(3);              \restore normal text mode
]