Jump to content
MakeWebGames

PHP: Circle Functions


Karlos94

Recommended Posts

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);
}
Link to comment
Share on other sites

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. ^^

Link to comment
Share on other sites

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);
}
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...