Code archives/Graphics/Draw Hollow Circle Pixel by Pixel with Integer Math

This code has been declared by its author to be Public Domain code.

Download source code

Draw Hollow Circle Pixel by Pixel with Integer Math by ImaginaryHuman2005
This routine is based on the mid-point algorithm to draw an unfilled circle one pixel at a time. It does everything with integer math, not a float in sight. It also doesn't use any trigonometry so no time-consuming cos() or sin().
'Midpoint Circle algorithm

Strict
Graphics 640,480,0

Local xCenter:Int=320
Local yCenter:Int=240
Local radius:Int
Local p,x,y:Int
Repeat
        Cls
        If MouseDown(1)
                xCenter=MouseX()
                yCenter=MouseY()
        EndIf
        radius=Abs(xCenter-MouseX())
        x=0
        y=radius
        Plot xCenter+x,yCenter+y
        Plot xCenter-x,yCenter+y
        Plot xCenter+x,yCenter-y
        Plot xCenter-x,yCenter-y
        Plot xCenter+y,yCenter+x
        Plot xCenter-y,yCenter+x
        Plot xCenter+y,yCenter-x
        Plot xCenter-y,yCenter-x
        p=1-radius
        While x<y
                If p<0
                        x:+1
                Else
                        x:+1
                        y:-1
                EndIf
                If p<0
                        p=p+(x Shl 1)+1
                Else
                        p=p+((x-y) Shl 1)+1
                EndIf
                Plot xCenter+x,yCenter+y
                Plot xCenter-x,yCenter+y
                Plot xCenter+x,yCenter-y
                Plot xCenter-x,yCenter-y
                Plot xCenter+y,yCenter+x
                Plot xCenter-y,yCenter+x
                Plot xCenter+y,yCenter-x
                Plot xCenter-y,yCenter-x
        Wend
        Flip
Until KeyHit(KEY_ESCAPE)
End

Comments

Rck2005


Port to BlitzPlus


ImaginaryHuman2005
Groovy!


Jesse2006
Is anybody having problems running the BMax sample, I can't get it to work right. All I get is a dot that moves along the y axis and relative to the movement of the mouse along the x axis. Some how the plot command is not working right with my version of BMax I am running version 1.14. I replaced plot with oval(just for trouble shooting) and it works fine.


ImaginaryHuman2006
Must be your version of max or something.


Oddball2006
Just what I needed. Thanks.


Andres2006
A bit slow, tried WritePixel?


Oddball2006
A bit slow, tried WritePixel?
WritePixel only writes to a PixMap. I needed to draw direct to a canvas, and it seems fast enough for my needs.


Code Archives Forum