在一个六角形格子砖之间的距离曼哈顿 [英] Manhattan Distance between tiles in a hexagonal grid

查看:239
本文介绍了在一个六角形格子砖之间的距离曼哈顿的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关方格砖A和B之间的欧氏距离为:

For a square grid the euclidean distance between tile A and B is:

distance = sqrt(sqr(x1-x2)) + sqr(y1-y2))

有关限制为沿方形网格移动演员,曼哈顿距离为更好地衡量实际距离,我们必须前往:

For an actor constrained to move along a square grid, the Manhattan Distance is a better measure of actual distance we must travel:

manhattanDistance = abs(x1-x2) + abs(y1-y2))

我如何得到如下图下方的红线和蓝线的六角网格的两个区域之间的曼哈顿距离?

How do I get the manhattan distance between two tiles in a hexagonal grid as illustrated with the red and blue lines below?

推荐答案

我曾经成立了一个六边形坐标系中的一场比赛,这样的的的轴是在一个60度角 X 轴。这避免了奇数偶数行区别。

I once set up a hexagonal coordinate system in a game so that the y-axis was at a 60-degree angle to the x-axis. This avoids the odd-even row distinction.

这个坐标系中的距离是:

The distance in this coordinate system is:

dx = x1 - x0
dy = y1 - y0

if sign(dx) == sign(dy)
    abs(dx + dy)
else
    max(abs(dx), abs(dy))

您可以转换( X 的',的的),从你的坐标系( X 的)这一次使用:

You can convert (x', y) from your coordinate system to (x, y) in this one using:

x = x' - floor(y/2)

所以 DX 变为:

dx = x1' - x0' - floor(y1/2) + floor(y0/2)

仔细实现此使用整数除法时,四舍五入。在C语言中的 INT是 楼(Y / 2)(Y%2?ÿ -1:Y)/ 2

这篇关于在一个六角形格子砖之间的距离曼哈顿的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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