Code archives/Graphics/Draw a Circle

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

Download source code

Draw a Circle by Matt McFarland2009
A simple explanation and introduction to Cos() and Sin() functions
'Simple Circles with Max2D
SuperStrict

'Turn the graphics on
Graphics 640,480

'Declare Variables
Local x:Float
Local y:Float
Local originx:Float = 640/2
Local originy:Float = 480/2
Local degree:Float 
Local radius:Float = 64
Local toggle:Byte = True

'Main loop
Repeat
	Cls
	DrawText("originx:"+String(originx),0,0)
	DrawText("originy:"+String(originy),0,12)
	DrawText("radius:"+String(radius),0,24)
	DrawText("Use arrow keys and +/- to modify",0,36)	
	DrawText("Use [SPACE] to toggle",0,48)
	
	'Draw the whole circle
	If Toggle=True
		For degree = 0 To 360 Step .5	
			x = originx +Cos(degree) * radius
			Y = originy +Sin(degree) * radius
			Plot x,y
		Next
	'Show the plot of the circle
	Else
		x = originx +Cos(degree) * radius
		Y = originy +Sin(degree) * radius
		Plot x,y
		degree:+5
	End If

	'User Interface
	If KeyDown(key_left) originx:-1
	If KeyDown(key_right) originx:+1
	If KeyDown(key_up) originy:-1
	If KeyDown(key_down) originy:+1
	If KeyDown(key_numadd) radius :+.5
	If KeyDown(key_numsubtract) radius:-.5
	If KeyHit(Key_space) toggle:+1
	If toggle > 1 toggle = 0
	Flip
Until KeyHit(key_escape)

Comments

dw8172016
This is an interesting program you've written, Matt. If you make the following change:
For degree# = 0 To 360 Step .25
You can draw a circle that does not have any gaps appear between points (at least for this resolution).

Really sterling work, a good study for people interested in usage of COS() and SIN().


Bobysait2016
the number of pixels to interpolate is the perimeter.
For a simple circle :
Local perimeter:int = ceil(radius*2*Pi) * 1.5
'(multiply this by 1.5 to prevent from the gap)

For d = 0 to perimeter
 Local degree:float = float(360.0 * d) / perimeter
 [...]


for an elipse, take the largest axis to compute the perimeter, it will be a good approximation (or use a real elipse formula)


Code Archives Forum