Code: Select all
function countSpiral($step) {
$sequence = ceil(sqrt($step));//1 =1, 2...4 = 2, 5...9 = 3, 10...16 = 4
$change = $sequence % 2;
if ($change==0) {
//even, these aren't final because they don't account for the upright part yet. That's calculated below
$ychange = floor($sequence/2);
}
else {
//odd
$ychange = floor(($sequence-1)/2) * -1;
}
$straight_part = $sequence+1;
$upright_part = $sequence-2;
$behind = pow($sequence, 2) - $step;
if ($behind<$straight_part) {
//The x is in the straight part
if ($ychange<0) $xchange = $ychange*-1 - $behind + 1;
else if ($ychange>0) $xchange = $ychange*-1 + $behind;
else $xchange = 1 - $behind;//This takes step 0 into account
}
else {
$xchange = $ychange;
//The x is in the upright part
if ($ychange>0) {
$ychange-=$behind-$straight_part+1;
}
else if ($ychange<0) {
$ychange+=$behind-$straight_part+1;
}
}
return array(
"xchange" => $xchange,
"ychange" => $ychange
);
}