用 Java 表示十六进制映射
好吧,我可能只是错过了一些东西,但我正在尝试制作一个六角板地图,就像在文明中一样。这个想法是将每个十六进制作为一个具有各种属性(即颜色、类型等)的对象。然后有一个由所有这些不同的六角形组成的板。有 3 种板类型:矩形、圆形、三角形。
我遇到的问题是如何表示数据类型中的每个十六进制。我认为二维数组是可行的方法,如果我只需要知道十六进制位置本身就可以了。但我需要知道每个六角形的顶点和边缘,以便我可以在这些六角形之间的边缘上添加此内容。这就是让我困惑的地方。如何用所有边和点来表示每个六边形,以便我可以询问连接到该点或边等的邻居。
任何想法,我已经阅读了很多关于此的文章,但由于某种原因我仍然不明白,也许因为这都是想法,我不知道如何实施它。
Ok so I might just be missing something but I am trying to make a hex board map, like in civilizations. The idea is to have each hex as an object that has various properties(ie, color, type, etc). Then have a board of all these various hexes. There would be 3 board types, rectangle, circle, triangle.
The issue I am having is how to represent each hex in a data type. I figured a 2 dimensional array would be the way to go, which would be fine if I just needed to know the hex locations themselves. But I will need to know the vertexes and edges of each hex so that I can do things like on the edge between these to hexes add this. That is what is confusing me. How to represent each hex with all sides and points in an way in which I can ask for neighbors connected to that point or edge etc.
Any ideas, I have read many articles about this, but for some reason I still don't understand, maybe because it is all ideas and I have no clue hoe to implement it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
二维数组将强化矩形网格的概念。
为什么不定义一个
Cell
类,并引用相邻单元格:网格边界上的
Cells
的引用将为空。您必须通过迭代行、创建每个
Cell
并根据需要将其与其邻居绑定来构建网格。A 2-dimensional array would reinforce the concept of a rectangular grid.
Why not instead define a
Cell
class, with references to the neighbouring cells thus:The references would be null for
Cells
on the boundaries of your grid.You'd have to construct the grid by iterating through the rows, creating each
Cell
and tying it to its neighbours as appropriate.您可以使用基于二维数组的类以及一些附加方法来计算邻居。
在标准数组中,单元格 E 与 B、D、F 和 H 共享边
如果您想象将交替列替换半个单元格;
现在单元格 E 与 B、D、F、GH I 共享边。事实上,这种排列与六角形网格相同 - 只是六角形变形。如果我的 ASCII 艺术技巧足够的话,这些正方形可以重新绘制为六角形。
当然,您无法物理地替换网格,但您可以提供控制对网格的访问并实现相邻单元格计算的方法。
You can use a class based on a 2D array, with some additional methods to compute neighbors.
In a standard array, cell E shares sides with B, D, F and H
If you imagine displacing alternate columns by half a cell;
Now cell E shares sides with B, D, F, G H I. In fact, this arrangement is the same as for a hex grid - just with misshapen hexes. If my ASCII art skills were up to it, the squares could be redrawn as hexes.
You can't physically displace the grid, of course, but you can provide methods that control access to the grid and implement the neighbor cell calculations.