在 C 中生成/创建六边形网格
所以我试图用 C 语言为游戏制作六边形网格。我真的很愚蠢,不知道从哪里开始。任何人都有任何想法。
编辑:我需要大约 15-20 个握把形状的六边形全部连接起来,就像游戏板一样。我正在开发一款游戏。抱歉没说清楚
So i m trying to make hexagonal grid in C for a game. I am really dumb founded on where to start on it. ANyone have any ideas.
EDIT: I need about 15-20 hexagons in a grip shape all joined,something like a game board. for a game i m working on. Sorry for not being clear
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
绝对地。尽管形状很奇怪,六边形仍然可以包含在您通常的多维数组中,以供将来使用(我假设您想将东西放入六边形中)。至于画它们,很简单。角度之和 = (6 - 2) * 180 = 4 * 180 = 720。一个角度为 720 / 6 = 120 度。首先计算最左边角的 Y 位置,等于
√(hexagonSide - hexagonWidth * hexagonWidth)
。我相信你能算出hexagonWidth
,对吧?好的,现在相对于最后一个的 X 位置将为 0。您需要将 Y 位置偏移之前六边形高度的一半,向上还是向下,具体取决于是否row * col
是偶数还是奇数。因为您知道六边形的宽度,所以您就有了对角的坐标。旋转 120° 并重复。在我继续之前,这应该在控制台中吗?还是真实的图形?
Absolutely. Despite their odd shape, hexagons can still be contained in your usual multidimensional array, for future use (I assume you'll want to put things in your hexagons). As for drawing them, it's simple. Sum of angles = (6 - 2) * 180 = 4 * 180 = 720. One angle is 720 / 6 = 120 degrees. Calculate first the leftmost angle's Y position, which is equal to
√(hexagonSide - hexagonWidth * hexagonWidth)
. I'm sure you can figure outhexagonWidth
, right? Okay, now the X position relative to the last one will be 0. You'll need to offset the Y position by half the height of the hexagon before it, up or down depending on whetherrow * col
is even or odd. Since you know the hexagon's width you have the coordinates of the opposite angle. Rotate by 120° and repeat.Before I continue, is this supposed to be in the console? Or is it real graphics?
那么让我们弄清楚这一点,游戏将在控制台上玩吗?是的,现在您需要设置数据结构,最明显的是节点。
节点
每个六边形都是一个具有六个边的节点。
如何初始化和连接节点结构
想象一下下面的六边形:
它有以下边:NORTHEAST、EAST、SOUTHEAST、SOUTHWEST、WEST 和 NORTHWEST。接下来观察它们将如何排列(10、11 和 12 以十六进制表示,以便它们可以容纳在一个空格中):
因此
0
将通过其5
链接到5
code>SOUTHEAST 链接,以及4
通过它的SOUTHWEST
链接。另请注意行如何在奇数和偶数元素之间交替。让我们调用{0, 1, 2, 3}
row[0] 和{4, 5, 6, 7, 8}
<强>行[1]。我们将其称为 5x3 六角形贴图。创建此数组的最简单方法是使用malloc(sizeof(Node) * width * height)
。连接节点
首先我要指出的是,每个偶数行 (0, 2, 4, ...) 都会有
width-1
元素。但还有更多,该行上的每个元素 (x, y) 将链接到数组中的以下元素:其他行上的元素,例如{
4, 5, 6, 7, 8
} 将具有width
元素,其中元素 (x, y) 链接到以下内容:当尝试将 (x1,y1) 与 (x2, y2) 链接时,请确保
0 <= x <宽度
和0 <= y
高度。
请记住...
您的数组每两行(行[0]、行[2]等)末尾包含一个未使用元素。此外,您可能希望为它们提供某种标签或索引,以便您可以将玩家推荐给它们。您可以将它们标记为(x,y)对,或者通过它们的索引进行数字标记,这一切都取决于您。 (x, y) 对对您来说非常简单,因为它将直接映射到它们存储的数组。
So let's get this straight, the game will be played on the console? Right, well now you will need to set up your data structures, the most obvious is with nodes.
The nodes
Each hexagon is a node with six edges.
How to initialize and connect the node structure
Imagine the following hexagon:
It has the following edges, NORTHEAST, EAST, SOUTHEAST, SOUTHWEST, WEST and NORTHWEST. Next observe how they will be arranged (10, 11 and 12 were represented in Hex so that they can fit in one space):
So
0
will link to5
through it'sSOUTHEAST
link, and4
through it'sSOUTHWEST
link. Also notice how the rows alternate between odd and even numbers of elements. Let's call{0, 1, 2, 3}
row[0], and{4, 5, 6, 7, 8}
row[1]. And let's call this a 5x3 hexmap. The easiest way to create this array is withmalloc(sizeof(Node) * width * height)
.Connecting the nodes
First of all let me point out that every even row (0, 2, 4, ...) will have
width-1
elements. But there's more, each element (x, y) on this row will link to the following element in your array:Elements on the other rows, such as {
4, 5, 6, 7, 8
} will havewidth
elements, where element (x, y) links to the following:When trying to link (x1,y1) with (x2, y2), ensure that
0 <= x < width
and0 <= y < height
.Remember ...
Your array contains one unused element at the end of every two rows (row[0], row[2], etc.). Also you might want to provide them all with some sort of label or index so that you can refer the player to them. You could label them as (x,y) pairs, or numerically by their index, it's all up to you. The (x, y) pair is very easy for you since that will map directly to the array they are stored in.