Oihana PHP

polarToCartesian.php

Table of Contents

Functions

polarToCartesian()  : array{x: float, y: float}
Converts a polar coordinate to a cartesian vector.

Functions

polarToCartesian()

Converts a polar coordinate to a cartesian vector.

polarToCartesian(array{angle: float, radius: float} $vector[, bool $degrees = true ][, bool $throwable = false ]) : array{x: float, y: float}
Parameters
$vector : array{angle: float, radius: float}

Polar coordinates with keys 'angle' and 'radius'.

$degrees : bool = true

Whether the angle is in degrees (default: true).

$throwable : bool = false
Tags
throws
InvalidArgumentException

If $throwable is true and keys are missing.

example
use function oihana\core\maths\polarToCartesian;

// Example 1: normal usage
$polar = ['angle' => 45, 'radius' => 10];
$cartesian = polarToCartesian($polar) ;
print_r($cartesian);
// Output: ['x' => 7.0710678118655, 'y' => 7.0710678118655]

// Example 2: missing angle, throwable = false (default)
$polar2 = ['radius' => 5];
$cartesian2 = polarToCartesian($polar2) ;
print_r($cartesian2);
// Output: ['x' => 0, 'y' => 0]

// Example 3: missing angle, throwable = true
$polar3 = ['radius' => 5];
try
{
    $cartesian3 = polarToCartesian($polar3, true, true);
}
catch ( InvalidArgumentException $e )
{
    echo $e->getMessage();
}
author

Marc Alcaraz (ekameleon)

since
1.0.0
Return values
array{x: float, y: float}

Cartesian representation with keys 'x' and 'y'.


        
On this page

Search results