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\$
1
  • 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
    Commented Aug 3, 2017 at 19:01

1 Answer 1

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\$
1
  • \$\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
    Commented Sep 6, 2018 at 2:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .