Calculate Width of Circle?

Miscellaneous Forums/General Discussion/Calculate Width of Circle?

Hi does anyone know how to calculate the Width of circle at different vertical positions. So imagine I wanted to animate a scanning horizontal line down from the top of the circle to the bottom of the circle. At the top the line would not be very wide and in the middle it would be 2*radius, and it would be small again by the bottom.

So how to work the width out for a given Y coord? Any ideas? Thx :-)

Your question is not so clear... can you explain it better?

here's some random stuff about circles:

The width of a circle is the diameter(d) and the radius(r=d/2) is half of the diameter, the circumference is always about 3.14*d(the distance across) it can be expressed as 2(3.14)r or d(3.14).


You probably already know this stuff though.

How are you determining where to scan? If you are going by x,y values around the circle, then it's as easy as doing XDistanceFromCenter*2.
If you are going by angle, then calculate the x position and do XDistanceFromCenter * 2 again.

SuperStrict

Graphics 800,600

Local CircleX:Float = 400 'put circle in center of screen
Local CircleY:Float = 300
Local CircleRadius:Float = 200

Cls
For Local Angle:Float = -90 To 90 Step 5 'scanning from top of circle to bottom along right side
	Local x:Float = Cos(Angle) * CircleRadius 'calculating x and y
	Local y:Float = Sin(Angle) * CircleRadius
	
	Local Width:Float = x*2 'The width of the circle
	DebugLog Width
	DrawLine x+CircleX,y+CircleY,(x-Width)+CircleX,y+CircleY
Next
Flip
WaitKey()


cos( ( y / r ) * ( pi / 2 ) ) * r * 2
cos being in radian.
y being the offset from the center.
r being the radius.
pi being pie.

I think you're talking simply using Cos

Middle: width = Cos(0) * xscale
Top: width = Cos(90) * xscale
Bottom: width = Cos(-90) * xscale

The results will range from 0 - 1 so you'll need to multiply this by your x scale. Likewise you'll need to normalise your y scale to range from -90 to 90 [(y/90) * scale]

For y = -90 to 90
cw = cos(y)*90
Line 320-cw,y+240,320+cw,y+240
next
waitkey


You can just use Pythagoras' theorem.

local width:Float = 2.0 * Sqr( radius*radius - ypos*ypos )

Strict

Graphics 640,480,0

Local radius:Float=100

While Not KeyDown(KEY_ESCAPE)

Cls

For Local y:Float=0 To radius Step 4
	Local hw:Float = circleHalfWidthAtY( radius, y )
	DrawLine 320-hw,240-y,320+hw,240-y
	DrawLine 320-hw,240+y,320+hw,240+y
Next

Flip
Wend


Function CircleHalfWidthAtY:Float( radius:Float, y:Float )
	Return Sqr(radius*radius - y*y )
End Function


Wow thanks all for your help! Lots of different solutions.

And the winner is Birdie with exactly what I needed (to know the radius at a given Y coord). Thx! Basically I didn't want to use an angle as I wanted to step the Y coord by a constant amount. Gosse also had a formula that would have worked by the look of things.

pi being pie

Not pie, pi!
http://www.keithschofield.com/pi/

You could also try looking at an integer-based (ie bresenham's algorithm) circle/ellipse drawing routine line the one I posted in the code archives for BlitzMax.