I try to figure out an angle of a triangle.
If one side of the triangle is 100 and the other side is 100.
How do I get that to be 45 degrees.
If I run tan-1(100/100) on my calculator i ge开发者_如何学Got 45. How do I do this in PHP?
Use the atan
function.
tan-1(100/100)
on the calculator is the equivalent to atan(100/100)
in Php.
Edit: Sorry, here's the same output as the calculator:
$input = 100 / 100;
print atan($input) * (180 / pi());
//Ouputs 45
Since you have both rise and run what you really want to use is atan2()
, which will handle the difference between e.g. 1/4*pi and -3/4*pi properly.
Here you go:
$base = 100;
$perpendicular = 100;
echo rad2deg(atan($perpendicular/$base)); // 45
$degrees = atan($tan);
Source
You should be able to use atan2
from the math functions. This returns a value in radians though (by multiplying by 180 degrees over pi), so you also have to convert that to degrees. So something like this should work (also using the pi()
function from the math library):
$degrees = atan2( 100, 100 ) * ( 180 / pi() );
精彩评论