在 C 中生成/创建六边形网格

发布于 2024-11-06 10:18:25 字数 119 浏览 0 评论 0原文

所以我试图用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

疯狂的代价 2024-11-13 10:18:25

绝对地。尽管形状很奇怪,六边形仍然可以包含在您通常的多维数组中,以供将来使用(我假设您想将东西放入六边形中)。至于画它们,很简单。角度之和 = (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 out hexagonWidth, 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 whether row * 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?

柳絮泡泡 2024-11-13 10:18:25

那么让我们弄清楚这一点,游戏将在控制台上玩吗?是的,现在您需要设置数据结构,最明显的是节点。

节点

每个六边形都是一个具有六个边的节点。

typedef struct Node {
  void *object; /* Pointer to object */
  Node *node_array; /* Pointer to node_array with 'node_count' nodes */
  int node_count; /* size of node_array */
} Node;

如何初始化和连接节点结构

想象一下下面的六边形:

 /\
|  |
 \/

它有以下边:NORTHEAST、EAST、SOUTHEAST、SOUTHWEST、WEST 和 NORTHWEST。接下来观察它们将如何排列(10、11 和 12 以十六进制表示,以便它们可以容纳在一个空格中):

//  0 1 2 3
// 4 5 6 7 8
//  9 A B C

因此 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) 将链接到数组中的以下元素:

  • (x+1, y-1) - NORTHEAST
  • (x+1, y) - EAST
  • (x+1, y) +1) - SOUTHEAST
  • (x, y+1) - SOUTHWEST
  • (x-1, y) - WEST
  • (x, y-1) - NORTHWEST

其他行上的元素,例如{4, 5, 6, 7, 8} 将具有 width 元素,其中元素 (x, y) 链接到以下内容:

  • (x, y-1) -东北
  • (x+1, y) - 东
  • (x, y+1) - 东南
  • (x-1, y+1) - 西南
  • (x-1, y) - 西
  • (x-1, y-1) - 西北

当尝试将 (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.

typedef struct Node {
  void *object; /* Pointer to object */
  Node *node_array; /* Pointer to node_array with 'node_count' nodes */
  int node_count; /* size of node_array */
} Node;

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):

//  0 1 2 3
// 4 5 6 7 8
//  9 A B C

So 0 will link to 5 through it's SOUTHEAST link, and 4 through it's SOUTHWEST 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 with malloc(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:

  • (x+1, y-1) - NORTHEAST
  • (x+1, y) - EAST
  • (x+1, y+1) - SOUTHEAST
  • (x, y+1) - SOUTHWEST
  • (x-1, y) - WEST
  • (x, y-1) - NORTHWEST

Elements on the other rows, such as {4, 5, 6, 7, 8} will have width elements, where element (x, y) links to the following:

  • (x, y-1) - NORTHEAST
  • (x+1, y) - EAST
  • (x, y+1) - SOUTHEAST
  • (x-1, y+1) - SOUTHWEST
  • (x-1, y) - WEST
  • (x-1, y-1) - NORTHWEST

When trying to link (x1,y1) with (x2, y2), ensure that 0 <= x < width and 0 <= 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文