Oihana PHP

mapRange.php

Table of Contents

Functions

mapRange()  : float
Re-maps a value from one range to another.

Functions

mapRange()

Re-maps a value from one range to another.

mapRange(float $value, float $inMin, float $inMax, float $outMin, float $outMax) : float

Linearly transforms $value from the input range [$inMin, $inMax] to the output range [$outMin, $outMax] using $outMin + ( $value - $inMin ) * ( $outMax - $outMin ) / ( $inMax - $inMin ).

The result is not clamped: values outside the input range map outside the output range.

Parameters
$value : float

The value to re-map.

$inMin : float

The lower bound of the input range.

$inMax : float

The upper bound of the input range.

$outMin : float

The lower bound of the output range.

$outMax : float

The upper bound of the output range.

Tags
throws
InvalidArgumentException

If the input range is degenerate ($inMin === $inMax).

example
use function oihana\core\numbers\mapRange;

mapRange( 5.0  , 0.0 , 10.0 , 0.0 , 100.0 ) ; // 50.0
mapRange( 0.0  , 0.0 , 10.0 , 0.0 , 100.0 ) ; // 0.0
mapRange( 10.0 , 0.0 , 10.0 , 0.0 , 100.0 ) ; // 100.0
mapRange( 0.0  , -1.0 , 1.0 , 0.0 , 255.0 ) ; // 127.5
mapRange( 15.0 , 0.0 , 10.0 , 0.0 , 100.0 ) ; // 150.0 (outside output range)
author

Marc Alcaraz (ekameleon)

since
1.0.9
Return values
float

The value re-mapped into the output range.

On this page

Search results