PDA

View Full Version : PHP: Circle Functions



Karlos94
06-07-2010, 10:16 AM
Well I got bored this morning as I only have to go into school for exams and I have one today, maths! So I was wondering how I could help myself remember the formula to calculate the area of a circle and the circumference of a circle. So I was browsing this forum and I saw LordDan post a mathematics function to calculate percentage so I thought to myself that I should do this with the formula and create two functions. I ended up writing these functions and I have tested them but I would like to improve them if others are kind enough to give feedback and other ideas.

Constructive criticism is welcome, flaming is not.


<?php

## Created by Karl Ballard
function area_of_circle($val, $radius = true, $dec_return = 3)
{
if (!is_numeric($val))
{
return false;
}

if (!is_numeric($pi))
{
$pi = pi();
}

if ($radius === false)
{
$val = $val / 2;
}

return $dec_return === false ? $pi * $val * $val : number_format($pi * $val * $val, $dec_return);
}

## Created By Karl Ballard
function circum_of_circle($val, $radius = false, $dec_return = 3)
{
if (!is_numeric($val))
{
return false;
}

if (!is_numeric($pi))
{
$pi = pi();
}

if ($radius === true)
{
$val = $val * 2;
}

return $dec_return === false ? $pi * $val : number_format($pi * $val, $dec_return);
}

LordDan
06-07-2010, 03:27 PM
<?php

function areaOfCircle( $radius ){
return number_format( pi() * $radius * $radius, 2 );
}

echo areaOfCircle( 10 ); // 314.15

?>


Enjoy :)
Edit: Not sure about circumference, never paid attention during that..

Karlos94
06-07-2010, 04:19 PM
I added more than the base code because I wanted to make the function to be used more easily and more customizable without editing the function source code.

Dillion & Amanda 4 Life!
06-07-2010, 05:40 PM
The circumference is either Diameter times 3.14 twice, or Radius times 3.14 twice.

LordDan
06-07-2010, 07:34 PM
I added more than the base code because I wanted to make the function to be used more easily and more customizable without editing the function source code.

I know :) I just posted the one I use for reference.. Obviously, if your project is open source or you have a team it's good to have all the extra checks, but I rarely need to find areas so I left it bare bones :thumbup:


The circumference is either Diameter times 3.14 twice, or Radius times 3.14 twice.

Glad someone paid attention in school. ^^

Karlos94
06-07-2010, 09:14 PM
Ahh I see, well if it was 100% personal to myself and no-one else would develop with myself then I would do it almost the same way... This is just so I can limit the amount of digits after the decimal are returned.


## Created by Karl Ballard
function area_of_circle($rad, $dec_return = 2)
{
if (!is_int($dec_return))
{
$dec_return = 2;
}

return $dec_return === false ? pi() * $rad * $rad : number_format(pi() * $rad * $rad, $dec_return);
}

## Created By Karl Ballard
function circum_of_circle($dia, $dec_return = 2)
{
if (!is_int($dec_return))
{
$dec_return = 2;
}

return $dec_return === false ? pi() * $dia : number_format(pi() * $dia, $dec_return);
}