Code archives/Graphics/Draw Filled Circle Row by Row with Integer Math
This code has been declared by its author to be Public Domain code.
Download source code
| |||||
This draws a filled circle row by row and does not use any floating point math or trigonometry - it is entirely integer based. The rows are filled using calls to DrawRect and each is drawn separately. This example actually draws a filled circle and a hollow circle at the same time. You can comment out the Plot commands to see only the filled circle, or comment out the DrawRect commands to see only the hollow circle. By both drawing at once in the same loop, they somewhat overwrite each other. | |||||
'Midpoint Circle algorithm Strict Graphics 640,480,0 Local xCenter:Int=320 Local yCenter:Int=240 Local radius:Int Local p,x,y,prevy:Int Repeat Cls If MouseDown(1) xCenter=MouseX() yCenter=MouseY() EndIf radius=Abs(xCenter-MouseX()) x=0 y=radius SetColor $FF,$88,$00 DrawRect xCenter-y,yCenter+x,y Shl 1,1 SetColor $FF,$FF,$FF 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-1 prevy=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 If y<prevy And x<y SetColor $FF,$88,$00 DrawRect xCenter-x,yCenter+y,x Shl 1,1 DrawRect xCenter-x,yCenter-y,x Shl 1,1 SetColor $FF,$FF,$FF Plot xCenter+x,yCenter+y Plot xCenter-x,yCenter+y Plot xCenter+x,yCenter-y Plot xCenter-x,yCenter-y EndIf SetColor $FF,$88,$00 DrawRect xCenter-y,yCenter+x,y Shl 1,1 DrawRect xCenter-y,yCenter-x,y Shl 1,1 SetColor $FF,$FF,$FF 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
| ||
The previous code was inefficient in that it drew some of the rows more than once. I've now edited and uploaded modified code which makes sure to only draw each row once. It therefore works better with modes like LIGHTBLEND and is faster. This is meant to be in the BlitzMax code archives. You also don't have to use Plot or DrawRect - those are just a way of outputting the circle `data` to show that it works. You could write to memory pointers, to an array, whatever. |
| ||
Port to BlitzPlus |
| ||
Cool! |
Code Archives Forum