Code archives/Graphics/Fast Circle

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

Download source code

Fast Circle by Grey Alien2005
Uses Shagwana's cool circle algo but is 200 times faster due to WritePixelFast, so is able to be used realtime. Careful not to draw outside of the screen though or it'll crash.
Function PlotFastCircle(xpos,ypos,radius) ;JB modification 28/08/05
  ;originally by Shagwana 2002-09-16 
  x=0 
  y=radius
  h=1-radius  
  c=255
  LockBuffer(BackBuffer)
  WritePixelFast(xpos+x,ypos+y,c)  ;Draw the starting pixels
  WritePixelFast(xpos-x,ypos-y,c)
  WritePixelFast(xpos+x,ypos-y,c)
  WritePixelFast(xpos-x,ypos+y,c)
  WritePixelFast(xpos+y,ypos+x,c)
  WritePixelFast(xpos-y,ypos-x,c)
  WritePixelFast(xpos+y,ypos-x,c)
  WritePixelFast(xpos-y,ypos+x,c)
  While y>x            ;Loop the arc
   If h<0
     h=h+(2*(x+1))
     x=x+1
     Else
     h=h+(2*(x-y))+5
     x=x+1
     y=y-1
     EndIf  
    WritePixelFast(xpos+x,ypos+y,c) ;Draw 1/8 at a time 
    WritePixelFast(xpos+y,ypos+x,c)
    WritePixelFast(xpos-x,ypos-y,c)
    WritePixelFast(xpos-y,ypos-x,c)
    WritePixelFast(xpos-x,ypos+y,c)
    WritePixelFast(xpos-y,ypos+x,c)
    WritePixelFast(xpos+x,ypos-y,c)
    WritePixelFast(xpos+y,ypos-x,c)
    Wend
  UnlockBuffer(BackBuffer)
End Function

Comments

Grey Alien2005
You can make it "safe" by changing WritePixelFast (all 16 of them) to wpf and adding this function
Function wpf(x,y,c)
  ;safe WritePixelFast
  If x<0 Or x>=GAME_WIDTH Then Return 0
  If y<0 Or y>=GAME_HEIGHT Then Return 0
  WritePixelFast(x,y,c)
End Function

but this slows it down by about three times. Note that GAME_WIDTH and GAME_HEIGHT are global variables or constants.

You could also make the buffer that is locked into a variable that is passed in for greater flexibility.


VP2005
the adventorous might try replacing the function call with a gosub or even inlining the 'safeness' part :)


Grey Alien2005
God I forgot about the gosub command, very useful thanks!


Shagwana2005
Just for completness, heres the original one.


Code Archives Forum