How2 - Draw a hex grid / add cartesian coords

Started by
1 comment, last by Ronnie Mado Solbakken 11 years, 8 months ago
I've posted this youtube video on how to easily draw (manually, not through code) a hex grid on any canvas. This method applies to all angles and sizes, but my own example won't result in perfect hexes (with all sides the same length; you'll need to use trigonometry for that). But if you do use trigonometry, you can then use my method to make the grid itself. In addition, I'm also showing how to add cartesian coordinates (-x^n thru +x^n, in this case -x,-y thru +x,+y) to each hex in the grid.

[media]
[/media]

I just thought I'd post it on Gamedev.net because some of us are using hex grids for our game. I hope you like it.

Cheers.

====================

Edit: I've made a second video (for programmers; a programming algorithm) and posted it in my third post below. Have fun.

- Awl you're base are belong me! -

- I don't know, I'm just a noob -

Advertisement
Here's a simple algorithm on adding an "almost-perfect" hex (it just looks good, but isn't actually perfect - the diagonal lines are about 10% longer than the vertical ones):

For a vertical (pointy) hexagon w/ an outer diameter of 100 pixels (length between two opposing angles):

1. Start drawing a line from x, y-50 (x and y = center of hex) ==> x-50, y-25. (top left diagonal)
2. Draw a line from x-50, y-25 ==> x-50, y+25. (left vertical)
3. Draw a line from x-50, y+25 ==> x, y+50. (bottom left diagonal)
4. Draw a line from x, y+50 ==> x+50, y+25. (bottom right diagonal)
5. Draw a line from x+50, y+25 ==> x+50, y-25. (right vertical)
6. Draw a line from x+50, y-25 ==> x, y-50. (top right diagonal)

Notice how you can apply this to any size (+/- 25 is 25% of outer diameter, while +/-50 is 50%, so for a 40-pixel hexagon this would be +/- 10px and +/- 20px, respectively). Note however, that the smaller a hexagon is, the more difference there'll be between vertical/horizontal sides and diagonal sides, because a vertical/horizontal line cannot be a decimal number of pixels in length (although diagonal lines can, as long as both its x and y span equals an integer number of pixels).

====================

Thankfully, in programming, there's really no reason to make the hexes absolutely perfect. As long as they look good, opposing sides are of same length (e.g. top right == lower left) and the hexes are drawn at minimum processing, they work.

- Awl you're base are belong me! -

- I don't know, I'm just a noob -

Update:
This video shows you (algorithmically) how you make a hex grid in any given programming language. Only visuals though, no actual coding involved, so I'm assuming you already know how to draw lines and make 2D arrays and nested if statements (and for loops, possibly).

[media]
[/media]

- Awl you're base are belong me! -

- I don't know, I'm just a noob -

This topic is closed to new replies.

Advertisement