Pythagorean theorem function

for all your a^2 b^2 = c^2 needs.

<?php

function pythagoras($a,$b,$c,$precision=4)
{
	($a) ? $a = pow($a,2) : $find .= 'a';
	($b) ? $b = pow($b,2) : $find .= 'b';
	($c) ? $c = pow($c,2) : $find .= 'c';
	
	switch ($find)
	{
		case 'a':
			return round(sqrt($c - $b),$precision);
		break;
		case 'b':
			return round(sqrt($c - $a),$precision);
		break;
		case 'c':
			return round(sqrt($a + $b),$precision);
		break;
	}
	
	return false;
}

/*
echo pythagoras(5,false,13); // 12
*/

?>

Usage

$precision is how much it rounds off at the decimal point. Only works for right triangles. Put false in the a, b, or c value that you are trying to find.


Comments

curtis
Posted on 21.07.2008 06:41

That is a very cool method of implementing a way to use the theorem. Very nice =)

steve
Posted on 01.07.2008 12:15

That is one cool function!

Add your comment