Skip to content

Instantly share code, notes, and snippets.

@tonylegrone
Created August 16, 2012 22:10
Show Gist options
  • Save tonylegrone/3374056 to your computer and use it in GitHub Desktop.
Save tonylegrone/3374056 to your computer and use it in GitHub Desktop.
Scales and integer to defined steps.
<?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