在六角形网格上找到相邻的邻居

编程入门 行业动态 更新时间:2024-10-24 04:47:11
本文介绍了在六角形网格上找到相邻的邻居的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 编辑:将示例映射包装在代码块中,以便格式正确。

好的,我正在尝试编写一个非常简单的A *六角形网格。我明白了,可以做A *部分。事实上,我的A *适用于方形网格。我无法环绕我的大脑是找到六边形的邻居。这里是heagonal grid的布局

0101 0301 0201 0401 0102 0302 0202 0402

等等

我需要帮助的是编写一个Hexagon类,它的十六进制坐标可以生成一个邻居列表。它需要能够生成掉落网格的邻居(比如20x20网格中的0000或2101),因为这就是我的A *如何跨多个地图并排放置。所以,这个代码片段可以工作:

planet = Hex('0214') print(planet.neighbors()) [ 'Hex 0213','Hex 0215','Hex 0115','Hex 0315','Hex 0116','Hex 0316'] 解决方案这取决于你如何定义你的六角形瓷砖的坐标。

让我们来看看。

,,,, / \ / \ / \ / \ | A1 | A2 | A3 | A4 | \ / \ / \ / \ / | B1 | B2 | B3 | / \ / \ \\ / \ / \ | C1 | C2 | C3 | C4 | \\ / \ / \ / \ / ''''

在这种情况下,对于偶数行和奇数行,邻居定义是不同的。

对于Y是偶数的单元格(X,Y),邻居是:$ (X + 1,Y),(X + 1,Y-1),(X-1,Y- Y + 1)

对于Y是奇数的单元格(X,Y),邻居是:(X-1,Y-1), (X,Y-1),(X-1,Y),(X + 1,Y),(X-1,Y + 1),

EDIT: Wrapped the example map in a code block so the formatting is correct.

Ok, I'm trying to write an extremely simple A* algorithm over a hexagonal grid. I understand, and can do the A* portion. In fact, my A* works for square grids. What I can't wrap my brain around is finding neighbors with hexagons. Here's the layout for the heagonal grid

0101 0301 0201 0401 0102 0302 0202 0402

etc, etc

So, what I need help with is writing a Hexagon class that, given it's hex coordinates, can generate a list of neighbors. It needs to be able to generate neighbors which would 'fall off' the grid (like 0000 or 2101 in a 20x20 grid) because that's how my A* tracks across multiple maps laid side-by-side. So something that would work with this code snippet:

planet = Hex('0214') print(planet.neighbors()) ['Hex 0213', 'Hex 0215', 'Hex 0115', 'Hex 0315', 'Hex 0116', 'Hex 0316']

解决方案

It depends on how you define the coordinates of your hex tiles.

Let's see.

, , , , / \ / \ / \ / \ | A1| A2| A3| A4| \ / \ / \ / \ / | B1| B2| B3| / \ / \ / \ / \ | C1| C2| C3| C4| \ / \ / \ / \ / ' ' ' '

In this case, neighbor definition is different for even and odd rows.

For a cell (X,Y) where Y is even, the neighbors are: (X,Y-1),(X+1,Y-1),(X-1,Y),(X+1,Y),(X,Y+1),(X+1,Y+1)

For a cell (X,Y) where Y is odd, the neighbors are: (X-1,Y-1),(X,Y-1),(X-1,Y),(X+1,Y),(X-1,Y+1),(X,Y+1)

更多推荐

在六角形网格上找到相邻的邻居

本文发布于:2023-11-30 02:39:17,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:网格   邻居

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!