java 如何绘制/管理六边形网格?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7142931/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to draw/manage a hexagon grid?
提问by Luke Vo
I've read this article: generating/creating hexagon grid in C. But look like both the author and answerer have already abandoned it.
我读过这篇文章:在 C 中生成/创建六边形网格。但是看起来作者和回答者都已经放弃了。
√(hexagonSide - hexagonWidth * hexagonWidth)
: What's hexagonSide and hexagonWidth? Isn't it will < 0 (so square root can't be calculated).
√(hexagonSide - hexagonWidth * hexagonWidth)
: 什么是六边形边和六边形宽度?是不是将 < 0(因此无法计算平方根)。
And, can I put a hexagon into a rectangle? I need to create a grid like this:
而且,我可以将六边形放入矩形中吗?我需要创建一个这样的网格:
One more thing, how can I arrange my array to store data, as well as get which cells are next to one cell?
另一件事,我如何安排我的数组来存储数据,以及获取哪些单元格与一个单元格相邻?
I have never been taught about hexagon, so I know nothing about it, but I can easily learn new thing, so if you can explain or give me a clue, I may do it myself.
我从来没有学过六边形,所以我对它一无所知,但是我可以很容易地学习新东西,所以如果你能解释或给我一个线索,我可能会自己做。
回答by Fantius
One way to represent the data would be to think of it like this:
表示数据的一种方法是这样考虑:
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-
The dashes are null locations -- they exist in the array, but do not represent any hexagon. Here, hexagon m is connected to hexagons c, g, h, q, r, w. Once you are ok with that representation, you can make it more compact by removing the null locations:
破折号是空位置——它们存在于数组中,但不代表任何六边形。这里,六边形 m 连接到六边形 c、g、h、q、r、w。一旦你对这个表示没问题,你可以通过删除空位置来使它更紧凑:
abcde
fghij
klmno
pqrst
uvwxy
Hexagon m is still connected to hexagons c, g, h, q, r, w, it's just a little harder to see.
六边形 m 仍然与六边形 c、g、h、q、r、w 相连,只是有点难看。
UpdateRead this: http://www-cs-students.stanford.edu/~amitp/game-programming/grids/
更新阅读:http: //www-cs-students.stanford.edu/~amitp/game-programming/grids/
回答by Luke Vo
This is how I draw the hexagon:
这就是我绘制六边形的方式:
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);
}
回答by Igor Maznitsa
you can take a look at https://code.google.com/p/jhexed/I guess it can be an example
你可以看看https://code.google.com/p/jhexed/我想这可以是一个例子