0
\$\begingroup\$

I have this method to get the cells inside a given N range:

public function getRadius($range, $center) {
    // $center[0] = X or Q
    // $center[1] = Y or R
    $cell = array();
    for ($dx = -$range; $dx <= $range; $dx++)
    {
        for ($dy = max(-$range, -$dx - $range); $dy <= min($range, -$dx + $range); $dy++)
        {   

            $ddy = $dy + $center[0];
            $ddx = $dx + $center[1];

            $cell[] = array($ddx,$ddy);
        }
    }
    return $cell;
}

But it returns bad coordinates by adding the offset on the delta X and delta Y.

\$\endgroup\$
  • 2
    \$\begingroup\$ Can you define in more detail what makes the coordinates you get as output "bad" and what "good" coordinates would look like? Concrete test cases with expected outputs can often help remove ambiguity in questions like these. \$\endgroup\$ – DMGregory Aug 3 '17 at 19:01
2
\$\begingroup\$

Found a better way to do by moving a cursor N times.

    public function getRadius($x, $y, $range) {
        $points = array(array($x,$y));
        for($i=1;$i<=$range;$i++) {
            $curp = array($x,$y);
            for($j=1;$j<=$i;$j++) {
                $p = $this->hexagon->moveTo("N",$curp[0],$curp[1]);
                $points[] = $p;
                $curp = $p;
            }
            for($j=1;$j<=$i;$j++) {
                $p = $this->hexagon->moveTo("SE",$curp[0],$curp[1]);
                $points[] = $p;
                $curp = $p;
            }
            for($j=1;$j<=$i;$j++) {
                $p = $this->hexagon->moveTo("S",$curp[0],$curp[1]);
                $points[] = $p;
                $curp = $p;
            }
            for($j=1;$j<=$i;$j++) {
                $p = $this->hexagon->moveTo("SO",$curp[0],$curp[1]);
                $points[] = $p;
                $curp = $p;
            }
            for($j=1;$j<=$i;$j++) {
                $p = $this->hexagon->moveTo("NO",$curp[0],$curp[1]);
                $points[] = $p;
                $curp = $p;
            }
            for($j=1;$j<=$i;$j++) {
                $p = $this->hexagon->moveTo("N",$curp[0],$curp[1]);
                $points[] = $p;
                $curp = $p;
            }
            for($j=1;$j<=$i;$j++) {
                $p = $this->hexagon->moveTo("NE",$curp[0],$curp[1]);
                $points[] = $p;
                $curp = $p;
            }
        }
        return $points;
    }
\$\endgroup\$
  • \$\begingroup\$ Glad to see that you found a solution! Don't forget to mark it as accepted - it helps others who may run into the same or similar problems. \$\endgroup\$ – Pikalek Sep 6 '18 at 2:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.