问题
I am using flat topped hexagonal grid (following the manual listed here http://www.redblobgames.com/grids/hexagons/).
I need to convert my cube coordinates into pixel coordinates. I have read Hexagonal Grid Coordinates To Pixel Coordinates but the solution listed there requires some modifications to work with flat topped grid. The logic must be similar to the one described in the question linked above but I can't work it out.
Definitely in case of flat top hexes x-coordinate may be used as x pixel coordinate. Thus calculating X pixel coordinate from cube coordinates is relatively easy. Assuming that $this->hexSize
is total width of hex and $cubeCoordinate
is an array of x,y,z coordinates the x-pixel coordinate will be:
$pixelCoordinate['x'] = $this->hexSize * $cubeCoordinate['x'] * 3/4;
I can't work out though how to calculate y pixel coordinate. The height between adjacent hexes should be deficiently equal to $this->hexSize
. But how to calculate offset based on cube coordinates?
回答1:
I have worked it out, inserting diffrent variables into the equasions listed here Hexagonal Grid Coordinates To Pixel Coordinates.
Finally it occurred that the cube coordinates in flat top hexagonal grid may be calculated using the following code:
/*
* Changes cube coordinates into offset one
*/
public function coordinates_CubetoOffset($cube)
{
$return['x'] = $this->hexSize * $cube['x'] * 3/4;
$return['y'] = sqrt(3)/2 * $this->hexSize * ($cube['x']/2 + $cube['y']);
return $return;
}
来源:https://stackoverflow.com/questions/32827085/flat-topped-hexes-hexagonal-grid-coordinates-to-pixel-coordinates