7

我读过这篇文章:在 C 中生成/创建六边形网格。但是看起来作者和回答者都已经放弃了它。

√(hexagonSide - hexagonWidth * hexagonWidth): hexagonSide 和 hexagonWidth 是什么?它不会< 0(所以无法计算平方根)。

而且,我可以把一个六边形变成一个矩形吗?我需要创建一个这样的网格:

来源:维基百科

还有一件事,我如何安排我的数组来存储数据,以及获取一个单元格旁边的单元格?

我从来没有教过六边形,所以我对它一无所知,但是我可以很容易地学习新东西,所以如果你能解释或给我一个线索,我可能会自己做。

4

3 回答 3

9

表示数据的一种方法是这样考虑:

a-b-c-d-e-
-f-g-h-i-j
k-l-m-n-o-
-p-q-r-s-t
u-v-w-x-y-

破折号是空位置——它们存在于数组中,但不代表任何六边形。这里,六边形 m 连接到六边形 c、g、h、q、r、w。一旦您对该表示感到满意,您可以通过删除空位置使其更紧凑:

abcde
fghij
klmno
pqrst
uvwxy

六边形 m 仍然连接到六边形 c、g、h、q、r、w,只是有点难看。

更新 阅读:http ://www-cs-students.stanford.edu/~amitp/game-programming/grids/

于 2011-08-22T14:10:02.943 回答
4

这就是我绘制六边形的方式:

    public Hexagon(float pX, float pY, float pSize) {
        super(pX, pY, pSize, pSize);
//      setColor(1, 0, 0);
        setAlpha(0);

        float x1, x2, y1, y2;
        float lineWidth = 3;

        x1 = 0; y1 = pSize / 2;
        x2 = pSize / 4; y2 = (pSize * ((2 - (float)Math.sqrt(3)) / 4)); // Done
        Line line = new Line(x1, y1, x2, y2);
        line.setLineWidth(lineWidth);
        attachChild(line);

        x1 = x2; y1 = y2;
        x2 = pSize * .75f; // Done
        line = new Line(x1, y1, x2, y2);
        line.setLineWidth(lineWidth);
        attachChild(line);

        x1 = x2; y1 = y2;
        x2 = pSize; y2 = pSize / 2; // Done
        line = new Line(x1, y1, x2, y2);
        line.setLineWidth(lineWidth);
        attachChild(line);

        x1 = x2; y1 = y2;
        x2 = pSize * .75f; y2 = pSize - (pSize * ((2 - (float)Math.sqrt(3)) / 4)); // Done
        line = new Line(x1, y1, x2, y2);
        line.setLineWidth(lineWidth);
        attachChild(line);

        x1 = x2; y1 = y2;
        x2 = pSize / 4; // Done
        line = new Line(x1, y1, x2, y2);
        line.setLineWidth(lineWidth);
        attachChild(line);

        x1 = x2; y1 = y2;
        x2 = 0; y2 = pSize / 2; // Done
        line = new Line(x1, y1, x2, y2);
        line.setLineWidth(lineWidth);
        attachChild(line);

        touchableArea = new Rectangle(pSize / 4, pSize / 4, pSize * .75f, pSize * .75f);
        touchableArea.setAlpha(0);
        attachChild(touchableArea);
    }
于 2011-08-22T08:35:42.613 回答
1

你可以看看https://code.google.com/p/jhexed/我想这可以是一个例子

于 2014-07-22T14:34:40.050 回答