Code archives/Algorithms/Get Area Of Circle

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

Download source code

Get Area Of Circle by PolarixMarch
Returns the area of an oval.
Graphics 640,480
SetBuffer BackBuffer()

Global xpos = 320
Global ypos = 240
Global size# = 100
Global radius# = size/2

While Not KeyHit(1)
	Cls
	
	Oval xpos,ypos,size,size
	Text 0,0,"The area of the circle is "+GetCircleArea(radius#)+" pixels"	
	
	Flip
Wend
End

Function GetCircleArea(r#)
	Return(r#*r#*Pi)
End Function

Comments

KryzonMarch
[Corrected]


PolarixMarch
Oops. My bad, thanks for telling me!


TomToadMarch
No need to pass x and y to the function since they never get used


MattyMarch
Is Pi a defined constant in blitz. .. i didnt think it was....this will return zero


MattyMarch
Also the function will only return an integer.


TomToadMarch
Pi is defined as 3.14159 in Blitz3D.


PolarixMarch
Also the function will only return an integer.


Pixels cannot be halfed or cut into smaller bits. so it will always be a whole number of pixels.


PolarixMarch
Is Pi a defined constant in blitz. .. i didnt think it was....this will return zero


I checked the example. It works fine for me.


KryzonMarch
You're right, pixels are always at integral coordinates.

Still, in some cases it's useful to work with floating point coordinates because then you have sub-pixel coverage: a graphic covers "part" of the pixel, so the pixel is semitransparent. This is how some software do antialiased graphics.




FloydMarch
Pixels cannot be halfed or cut into smaller bits. so it will always be a whole number of pixels.

The point of the comment about the function returning an integer is that it computes a floating point value but returns an integer.

The fact that the number of pixels is an integer is not really relevant. GetCircleArea is not counting pixels and will match the number of pixels drawn only by coincidence.

For example, with width = height = 8 GetCircleArea returns 50 when the actual number of pixels is 52.


PolarixApril
I don't get what you are saying, it works fine


Code Archives Forum