public function Grid($min, $round_up = false)
{
$this->uneven = false;
$negative = $this->min_value < 0;
$min_sub = max($min, $this->length / 200);
if ($round_up || $this->min_value == $this->max_value) {
$this->max_value += $this->min_unit;
}
$scale = $this->max_value - $this->min_value;
// get magnitude from greater of |+ve|, |-ve|
$abs_min = abs($this->min_value);
$magnitude = max(pow(10, floor(log10($scale))), $this->min_unit);
if ($this->fit) {
$count = ceil($scale / $magnitude);
} else {
$count = ceil($this->max_value / $magnitude) - floor($this->min_value / $magnitude);
}
if ($count <= 5 && $magnitude > $this->min_unit) {
$magnitude *= 0.1;
$count = ceil($this->max_value / $magnitude) - floor($this->min_value / $magnitude);
}
$neg_count = ceil($abs_min / $magnitude);
$this->find_division($this->length, $min_sub, $count, $neg_count, $magnitude);
$grid = $this->length / $count;
// guard this loop in case the numbers are too awkward to fit
$guard = 20;
while ($grid < $min && --$guard) {
$this->find_division($this->length, $min_sub, $count, $neg_count, $magnitude);
$grid = $this->length / $count;
}
if ($guard == 0) {
// could not find a division
while ($grid < $min && $count > 1) {
$count *= 0.5;
$magnitude *= 2;
$grid = $this->length / $count;
$this->uneven = true;
}
} elseif (!$this->fit && $magnitude > $this->min_unit && $grid / $min > 2) {
// division still seems a bit coarse
$this->sub_division($this->length, $min_sub, $count, $neg_count, $magnitude);
$grid = $this->length / $count;
}
$this->unit_size = $this->length / ($magnitude * $count);
$this->zero = $negative ? $neg_count * $grid : -$this->min_value * $grid / $magnitude;
return $grid;
}