用C生成/创建六边形网格 [英] generating/creating hexagon grid in C

查看:148
本文介绍了用C生成/创建六边形网格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我米试图使六边形网格的下一场比赛。我真的很愚蠢的成立,从哪里开始就可​​以了。任何人有任何想法。

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.

编辑:我需要在抓形状都加入约15-20六边形,就像一个游戏板。对于一个游戏I M工作。对不起,我没有说清楚。

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

推荐答案

因此​​,让我们这个直,比赛将在控制台上播放?对了,现在好了,你将需要设置你的数据结构,最明显的是节点。

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.

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

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:

 /\
|  |
 \/

它具有以下的边缘,东北,华东,东南,西南,西部和西北部。接下来观察他们将如何安排(10,11和12重新在十六进制psented $ P $,使他们能够以一个空格):

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

所以 0 将通过它的东南 5 >链接,并通过它 4 作者链接。还要注意如何元素的奇数和偶数之间的交替行。让我们把 {0,1,2,3} 行[0] {4,5,6, 7,8} 行[1] 。让我们称之为的 5X3 的hexmap。创建这个阵列的最简单的方法是用的malloc(sizeof运算(节点)*宽*高)

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

首先我要指出,每一个连排(0,2,4,...)将有宽度-1 元素。但是,还有更多,每一个元素(X,Y),该行会链接到下面的元素数组中:

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) - 东北

  • (X + 1,Y) - 东部

  • (X + 1,Y + 1) - 东南

  • (X,Y + 1) - 西南

  • (X-1,Y) - 西

  • (X,Y-1) - 西北

在其他行的元素,如{ 4,5,6,7,8 }将宽度元素,其中元素(X,Y)的链接如下:

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) - 东北

  • (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<高度

When trying to link (x1,y1) with (x2, y2), ensure that 0 <= x < width and 0 <= y < height.

您数组包含一个是在每两行的结束元素(行[0],行[2]等)。您可能还需要为他们提供所有带有某种的标签首页的,这样你可以参考播放器给他们。您的可能的标签他们为(X,Y)对,或数字用索引,这一切都取决于你。第(x,y)的一对是非常容易的,因为这将直接映射到它们被存储在阵列

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.

这篇关于用C生成/创建六边形网格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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