Flat topped hexes hexagonal grid coordinates To Pixel Coordinates

怎甘沉沦 提交于 2019-12-23 03:13:03

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!