How to draw / manage a hexagonal grid?

I read this article: creating / creating a hexagonal grid in C. But look, both the author and the defendant have already abandoned it.

√(hexagonSide - hexagonWidth * hexagonWidth) : What are hexagonSide and hexagonWidth? Is it not going to be <0 (so the square root cannot be calculated).

And can I put a hexagon in a rectangle? I need to create a grid as follows:

Source: Wikipedia

Another thing, how can I organize my array to store data, and also get which cells are next to one cell?

I have never been taught about hexagons, so I don’t know anything about it, but I can easily learn new things, so if you can explain or give me the key, I can do it myself.

+7
source share
3 answers

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); } 
+4
source

One way of presenting data would be to think of it as follows:

 abcde- -fghij klmno- -pqrst uvwxy- 

Dashes are zero locations β€” they exist in the array, but they are not a hexagon. Here the hexagon m is connected with the hexagons c, g, h, q, r, w. Once you are fine with this view, you can make it more compact by removing zero locations:

 abcde fghij klmno pqrst uvwxy 

The hexagon m is still connected to the hexagons c, g, h, q, r, w, which is a little harder to see.

Refresh . Read the following: http://www-cs-students.stanford.edu/~amitp/game-programming/grids/

+9
source

you can take a look at https://code.google.com/p/jhexed/ I think this might be an example

+1
source

All Articles