Skip to content

Core\Math::toDegrees

Converts an angle from radians to degrees. Core\Math::atan2, Core\Math::asin and Core\Math::acos return angles in radians. People usually read angles in degrees, so you use this before you show an angle. PI radians is 180 degrees. Core\Math::toRadians converts the other way. An infinity gives an infinity, and NaN (a value that means "not a number") gives NaN.

The examples below show a few angles, then angles that other functions return, then how a delivery app finds the compass direction to a customer.

Parameters

  • $radiansfloatThe angle, in radians.

Return value

Returns float. The same angle in degrees. Core\Math::toDegrees(Core\Math::PI) is 180.0. A result too big for a float is an infinity, and NaN gives NaN.

Examples

A few angles

<?nvs
// Shows a few angles in radians converted to degrees.
// `PI` radians is half a circle. This prints 180.
echo Core\Math::toDegrees(Core\Math::PI), "\n";
// `PI / 2` radians is a quarter circle. This prints 90.
echo Core\Math::toDegrees(Core\Math::PI / 2.0), "\n";
// One radian is a little more than 57 degrees. This prints 57.295779513082.
echo Core\Math::toDegrees(1.0), "\n";
Output
180
90
57.295779513082

Angles from other functions

<?nvs
// Shows how to read an angle that another function returns in radians.
// `Core\Math::atan2` returns the angle of the point (1, 1). This prints 45.
echo Core\Math::toDegrees(Core\Math::atan2(1.0, 1.0)), "\n";
// `Core\Math::acos` returns the angle whose cosine is 0.5. This prints 60.
echo Core\Math::toDegrees(Core\Math::acos(0.5)), "\n";
Output
45
60

Compass direction

<?nvs
// Shows how a delivery app finds the compass direction from a driver to a customer.
class Compass
{
// `$east` and `$north` are how many meters the customer is from the driver.
// North is 0 degrees and east is 90 degrees.
public static function bearing(float $east, float $north): float
{
float $degrees = Core\Math::toDegrees(Core\Math::atan2($east, $north));
if ($degrees < 0.0) {
$degrees = $degrees + 360.0;
}
return Core\Math::round($degrees, { precision: 1 });
}
}
// The customer is 100 meters east and 100 meters north. This prints 45.
echo Compass::bearing(100.0, 100.0), "\n";
// The customer is 50 meters south. This prints 180.
echo Compass::bearing(0.0, -50.0), "\n";
// The customer is 300 meters west and 40 meters north. This prints 277.6.
echo Compass::bearing(-300.0, 40.0), "\n";
Output
45
180
277.6