Code archives/Algorithms/Sine Circle

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

Download source code

Sine Circle by superStruct2009
This allows you to draw a circle with a given radius. The algorithm uses the sine function to draw an infinite series of triangles and plotting points corresponding to a point on a circle.
Graphics 800,600,0,2

Global originX% = 400
Global originY% = 300
Global radius% = 100
Global x1# = 0
Global x2# = 0
Global y1# = 0
Global y2# = 0

For i = 0 To 90
	y1 = (radius*Sin#(i))
	y2 = -(radius*Sin#(i))
	x1 = 400 + Sqr#(radius^2 - y1^2) 
	x2 = 400 - Sqr#(radius^2 - y1^2)
	Plot x1,y2 + 300
Next

For i = 0 To 90
	y1 = (radius*Sin#(i))
	y2 = -(radius*Sin#(i))
	x1 = 400 + Sqr#(radius^2 - y1^2) 
	x2 = 400 - Sqr#(radius^2 - y1^2)
	Plot x2,y2 + 300
Next

For i = 0 To 90
	y1 = (radius*Sin#(i))
	y2 = -(radius*Sin#(i))
	x1 = 400 + Sqr#(radius^2 - y1^2) 
	x2 = 400 - Sqr#(radius^2 - y1^2)
	Plot x2,y1 + 300
Next

For i = 0 To 90
	y1 = (radius*Sin#(i))
	y2 = -(radius*Sin#(i))
	x1 = 400 + Sqr#(radius^2 - y1^2) 
	x2 = 400 - Sqr#(radius^2 - y1^2)
	Plot x1,y1 + 300
Next

Flip

WaitKey

Comments

Andy_A2009
Here's the same routine in the form of a function.

I've also made some slight optimizations so that is should run faster than the code originally posted.

Graphics 800,600,0,2

sineCircle(100, 400, 300)

Flip

WaitKey
End


Function sineCircle(radius%, centerX%, centerY%)
	Local temp1#, temp2#
	Local x1%, y1%, x2%, y2%

	For i = 0 To 90

		;only invoke Sin() function once per iteration
		temp1 = radius * Sin(i)
		y1 = temp1
		y2 = -temp1

	        ;only invoke Sqr function once per iteration and eliminate
	        ;the exponentiation as it’s slower than multiplication
		temp2 = Sqr(radius * radius – y1 * y1)
		x1 = centerX + temp2
		x2 = centerX – temp2

		;plot all four quadrants in the same loop
		Plot x1,y2 + centerY
		Plot x2,y2 + centerY
		Plot x2,y1 + centerY
		Plot x1,y1 + centerY
	Next
End Function



Code Archives Forum