Created
August 16, 2012 22:10
-
-
Save tonylegrone/3374056 to your computer and use it in GitHub Desktop.
Scales and integer to defined steps.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Scales and integer to defined steps. | |
* | |
* @param int $int | |
* The integer to test against. | |
* @param int $min | |
* The minimum number of the range. | |
* @param int $max | |
* The maximum number of the range. | |
* @param int $steps | |
* The total steps to use in the scale. | |
* | |
* @return int | |
* Returns the appropriate step if $int is within the range of $min and $max. | |
* If $int is below or above the provided limits, 1 or $steps will be | |
* returned respectively. | |
*/ | |
function int_scale($int, $min, $max, $steps = 10) { | |
// If $steps are defined as 1 or lower, or if $int is below the range, | |
// return 1. | |
if ($steps <= 1 || $int <= $min) return 1; | |
// If $int is above the provided range, return the max step. | |
if ($int > $max) return $steps; | |
$step_range = ($max - $min ) / ($steps); | |
return ceil(($int - $min) / $step_range); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment