平顶六边形六边形网格坐标到像素坐标 [英] Flat topped hexes hexagonal grid coordinates To Pixel Coordinates

查看:170
本文介绍了平顶六边形六边形网格坐标到像素坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用平顶六角形网格(遵循此处列出的手册 http://www.redblobgames .com/grids/hexagons/).

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.

肯定是在平顶十六进制的情况下,x坐标可以用作x像素坐标.因此,根据立方体坐标计算X像素坐标相对容易.假设$this->hexSize是十六进制的总宽度,并且$cubeCoordinate是x,y,z坐标的数组,则x像素坐标为:

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;

我不知道如何计算y像素坐标.相邻十六进制之间的高度应不足以等于$this->hexSize.但是如何根据立方体坐标计算偏移量呢?

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?

推荐答案

我已经解决了问题,将不同的变量插入此处列出的等式中

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;
        }

这篇关于平顶六边形六边形网格坐标到像素坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆