# How2 - Draw a hex grid / add cartesian coords

This topic is 2230 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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. Edited by DrMadolite

##### Share on other sites
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. Edited by DrMadolite

##### Share on other sites
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]

1. 1
2. 2
Rutin
21
3. 3
4. 4
frob
17
5. 5

• 9
• 33
• 13
• 13
• 10
• ### Forum Statistics

• Total Topics
632582
• Total Posts
3007207

×