Code archives/Graphics/Circle plotter

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

Download source code

Circle plotter by furbrain2011
Stumbled across this routine on the net from a C64 site and just turned it into a blitz3d function. Credit goes to the original author Stephen Judd for the code and it is in the public domain. Uses only addition & subtraction

Usage is self explanatory i should hope :)
Function Circle(xpos,ypos,radius,colour,buffer)
	Y = radius
	X = 0
	A = radius/2
	LockBuffer buffer
	WritePixelFast(xpos+x,ypos+y,colour)		; Draw the starting pixels
	WritePixelFast(xpos-x,ypos-y,colour)		
	WritePixelFast(xpos+x,ypos-y,colour)
	WritePixelFast(xpos-x,ypos+y,colour)
	WritePixelFast(xpos+y,ypos+x,colour)
	WritePixelFast(xpos-y,ypos-x,colour)
	WritePixelFast(xpos+y,ypos-x,colour)
	WritePixelFast(xpos-y,ypos+x,colour)
	While X < Y
		X = X + 1
		A = A - X
		If A<0 Then 
			A=A+Y 
			Y=Y-1
		EndIf
		WritePixelFast(xpos+x,ypos+y,colour) ; Draw 1/8 at a time 
		WritePixelFast(xpos+y,ypos+x,colour)
		WritePixelFast(xpos-x,ypos-y,colour)
		WritePixelFast(xpos-y,ypos-x,colour)
		WritePixelFast(xpos-x,ypos+y,colour)
		WritePixelFast(xpos-y,ypos+x,colour)
		WritePixelFast(xpos+x,ypos-y,colour)
		WritePixelFast(xpos+y,ypos-x,colour)
	Wend
	UnlockBuffer buffer
End Function

Comments

Andy_A2011
That's a very fast routine according to this simple test.

Graphics 800,600,32,2
SetBuffer GraphicsBuffer()


st = MilliSecs()
For i = 0 To 999
	Circle(400, 300, 250, $FFE020, GraphicsBuffer())
Next 
et1 = MilliSecs()-st


st = MilliSecs()
For i = 0 To 999
	Oval(150, 50, 500, 500, 0)
Next
et2 = MilliSecs()-st

Text 5, 5,"Circle: "+et1	;Elapsed Time using 'Circle'

Text 5,25,  "Oval: "+et2	;Elapsed Time using 'Oval'

Flip
WaitMouse()

End





Function Circle(xpos,ypos,radius,colour,buffer)
	Y = radius
	X = 0
	A = radius Shr 1
	LockBuffer buffer
	WritePixelFast(xpos+x,ypos+y,colour)		; Draw the starting pixels
	WritePixelFast(xpos-x,ypos-y,colour)		
	WritePixelFast(xpos+x,ypos-y,colour)
	WritePixelFast(xpos-x,ypos+y,colour)
	WritePixelFast(xpos+y,ypos+x,colour)
	WritePixelFast(xpos-y,ypos-x,colour)
	WritePixelFast(xpos+y,ypos-x,colour)
	WritePixelFast(xpos-y,ypos+x,colour)
	While X < Y
		X = X + 1
		A = A - X
		If A<0 Then 
			A=A+Y 
			Y=Y-1
		EndIf
		WritePixelFast(xpos+x,ypos+y,colour) ; Draw 1/8 at a time 
		WritePixelFast(xpos+y,ypos+x,colour)
		WritePixelFast(xpos-x,ypos-y,colour)
		WritePixelFast(xpos-y,ypos-x,colour)
		WritePixelFast(xpos-x,ypos+y,colour)
		WritePixelFast(xpos-y,ypos+x,colour)
		WritePixelFast(xpos+x,ypos-y,colour)
		WritePixelFast(xpos+y,ypos-x,colour)
	Wend
	UnlockBuffer buffer
End Function



JBR2011
If you 'lockbuffer backbuffer()' for the oval, the oval is about 8 times faster!

Jim


Andy_A2011
I re-did the code so that the buffer is locked before doing any drawing by either function. I also remmed out the lock/unlock buffer lines in 'Circle' function since buffer is locked prior to calling function.

I don't have B3D, using Blitzplus instead. I consistently get times of 116ms for 'Circle' and 1186ms for 'Oval'.

XP SP3
P4 2.8GHz
1GB RAM
Intel 82915G/GV/910GL graphics

The other thing is that 'Oval' doesn't draw anything if I lock the buffer. Probably one of the differences between B+ & B3D.

Graphics 800,600,32,2
SetBuffer GraphicsBuffer()

LockBuffer GraphicsBuffer()		;lock the buffer for both functions
st = MilliSecs()

For i = 0 To 999
	Circle(400, 300, 250, $FFE020, GraphicsBuffer())
Next 

et1 = MilliSecs()-st


Color 255,255,255
st = MilliSecs()

For i = 0 To 999
	Oval(150, 50, 500, 500, False)
Next

UnlockBuffer GraphicsBuffer()	;unlock the buffer for both functions
et2 = MilliSecs()-st

Text 5, 5,"Circle: "+et1	;Elapsed Time using 'Circle'
Text 5,25,"  Oval: "+et2	;Elapsed Time using 'Oval'

Flip
WaitMouse()

End



MCP2011
Great stuff. How difficult would it be to modify the Circle code above to draw elipses quickly?


JBR2011
First Circle method : 1200ms
Second : 24ms
Locked oval : 150ms

Looks like GraphicsBuffer() is faster inside a locked buffer. (you can drop it from the function too)

Jim

AMD Phenom 2 955 3.2GHz


Matty2011
Something to be wary of, which may slow it down if you check for this , is making sure the writepixelfast's don't write outside the buffer boundaries as blitz usually crashes on writing out of bounds.


Code Archives Forum