Oihana PHP

maths

Table of Contents

Functions

bearing()  : float
Calculates the initial bearing (sometimes referred to as forward azimuth) which if followed in a straight line along a great-circle arc will take you from the start point to the end point (in degrees).
cartesianToPolar()  : array{angle: float, radius: float}
Converts a cartesian vector to polar coordinates.
ceilValue()  : int|float
Rounds and returns the ceiling of the specified number or expression.
fixAngle()  : float
Normalize an angle in degrees to the range [0, 360).
floorValue()  : int|float
Rounds and returns a number by a count of floating points, using floor.
gcd()  : int
Calculate the Greatest Common Divisor (GCD) of two integers using the Euclidean algorithm.
haversine()  : float
Calculate the great-circle distance between two points on a sphere using the Haversine formula.
polarToCartesian()  : array{x: float, y: float}
Converts a polar coordinate to a cartesian vector.
roundValue()  : int|float
Rounds and returns the rounded value of the specified number or expression.

Functions

bearing()

Calculates the initial bearing (sometimes referred to as forward azimuth) which if followed in a straight line along a great-circle arc will take you from the start point to the end point (in degrees).

bearing(float $latitude1, float $longitude1, float $latitude2, float $longitude2) : float
Parameters
$latitude1 : float

The first latitude coordinate in degrees.

$longitude1 : float

The first longitude coordinate in degrees.

$latitude2 : float

The second latitude coordinate in degrees.

$longitude2 : float

The second longitude coordinate in degrees.

Tags
example
$position1 = [ 'x' => 37.422045 , 'y' => -122.084347 ]; // Google HQ
$position2 = [ 'x' => 37.77493  , 'y' => -122.419416 ]; // San Francisco, CA

echo bearing($position1['x'], $position1['y'], $position2['x'], $position2['y']);
// 323.1477743368166
author

Marc Alcaraz (ekameleon)

since
1.0.0
Return values
float

The bearing in degrees from North.

cartesianToPolar()

Converts a cartesian vector to polar coordinates.

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

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

$degrees : bool = true

Whether the returned angle should be in degrees (default: true).

$throwable : bool = false

Whether to throw an exception if keys are missing (default: false).

Tags
throws
InvalidArgumentException

if $throwable is true and required keys are missing.

example
$polar = cartesianToPolar(['x' => 0, 'y' => 5]);
// Returns: ['angle' => 90, 'radius' => 5]

$polar = cartesianToPolar(['x' => 1, 'y' => 1], false);
// Returns angle in radians
author

Marc Alcaraz (ekameleon)

since
1.0.0
Return values
array{angle: float, radius: float}

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

ceilValue()

Rounds and returns the ceiling of the specified number or expression.

ceilValue(int|float $value[, int $floatCount = 0 ]) : int|float

The ceiling of a number is the closest integer that is greater than or equal to the number.

Parameters
$value : int|float

The number to round.

$floatCount : int = 0

The number of decimal places to round up to.

Tags
example
echo ceilValue(4.1234);       // Outputs: 5
echo ceilValue(4.1234, 2);    // Outputs: 4.13
echo ceilValue(4.9999, 3);    // Outputs: 5.000
author

Marc Alcaraz (ekameleon)

since
1.0.0
Return values
int|float

The rounded number.

fixAngle()

Normalize an angle in degrees to the range [0, 360).

fixAngle(float|int $angle) : float

If the input is not numeric (NaN, null, etc.), the function will return 0.

Parameters
$angle : float|int

The angle in degrees.

Tags
example
echo fixAngle(370);   // 10
echo fixAngle(-90);   // 270
echo fixAngle(720.5); // 0.5
echo fixAngle(NAN);   // 0
author

Marc Alcaraz (ekameleon)

since
1.0.0
Return values
float

The normalized angle between 0 (inclusive) and 360 (exclusive).

floorValue()

Rounds and returns a number by a count of floating points, using floor.

floorValue(int|float $value[, int $floatCount = 0 ]) : int|float

The floor of a number is the closest integer less than or equal to the number.

Parameters
$value : int|float

The number to round.

$floatCount : int = 0

The number of decimal places to round down to.

Tags
example
echo floorValue(4.9876);       // Outputs: 4
echo floorValue(4.9876, 2);    // Outputs: 4.98
echo floorValue(4.1234, 3);    // Outputs: 4.123
author

Marc Alcaraz (ekameleon)

since
1.0.0
Return values
int|float

The rounded number.

gcd()

Calculate the Greatest Common Divisor (GCD) of two integers using the Euclidean algorithm.

gcd(int $a, int $b[, bool $throwable = false ]) : int

This function returns the absolute value of the GCD. If both numbers are zero, it either returns 0 or throws an exception depending on the $throwable parameter.

Parameters
$a : int

The first integer.

$b : int

The second integer.

$throwable : bool = false

If true, throws an exception when both numbers are zero.

Tags
example
echo gcd(48, 18); // Outputs: 6
echo gcd(-48, 18); // Outputs: 6
echo gcd(0, 5);    // Outputs: 5
echo gcd(0, 0);    // Outputs: 0
author

Marc Alcaraz (ekameleon)

since
1.0.0
Return values
int

The greatest common divisor of $a and $b.

haversine()

Calculate the great-circle distance between two points on a sphere using the Haversine formula.

haversine(float $latitude1, float $longitude1, float $latitude2, float $longitude2[, float $radius = 6371000 ][, int|null $precision = null ]) : float

This is commonly used for distances on Earth. It's faster than the Vincenty formula but less precise.

Parameters
$latitude1 : float

The latitude of the first point in degrees.

$longitude1 : float

The longitude of the first point in degrees.

$latitude2 : float

The latitude of the second point in degrees.

$longitude2 : float

The longitude of the second point in degrees.

$radius : float = 6371000

The radius of the sphere (default is Earth's mean radius: 6,371,000 meters).

$precision : int|null = null

Number of decimal places to round to. If null, returns full precision.

Tags
example
use function oihana\core\maths\haversine;

$pos1 = [ 37.422045, -122.084347 ] ; // Google HQ
$pos2 = [ 37.77493,  -122.419416 ] ; // San Francisco, CA

echo haversine( $pos1[0] , $pos1[1], $pos2[0] , $pos2[1] ) ;       // Full precision
echo haversine( $pos1[0] , $pos1[1], $pos2[0] , $pos2[1] , 6371000, 3 ); // Rounded to 3 decimals
author

Marc Alcaraz (ekameleon)

since
1.0.0
Return values
float

The distance between the two points in meters.

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

roundValue()

Rounds and returns the rounded value of the specified number or expression.

roundValue(int|float $value[, int $floatCount = 0 ]) : int|float
Parameters
$value : int|float

The number to round.

$floatCount : int = 0

The number of decimal places to round to.

Tags
example
echo roundValue(4.9876);       // Outputs: 5
echo roundValue(4.9876, 2);    // Outputs: 4.99
echo roundValue(4.1234, 3);    // Outputs: 4.123
author

Marc Alcaraz (ekameleon)

since
1.0.0
Return values
int|float

The rounded number.


        
On this page

Search results