• FEATURED

View more

View more

View more

Image of the Day Submit

IOTD | Top Screenshots

The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

Sign up now

How2 - Draw a hex grid / add cartesian coords

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

2 replies to this topic

#1Malabyte  Members

Posted 09 August 2012 - 08:11 AM

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.

http://youtu.be/s6sEdkIYMdA

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, 12 August 2012 - 12:46 PM.

- Awl you're base are belong me! -

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

#2Malabyte  Members

Posted 11 August 2012 - 02:35 PM

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, 12 August 2012 - 08:41 AM.

- Awl you're base are belong me! -

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

#3Malabyte  Members

Posted 12 August 2012 - 12:44 PM

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).

http://youtu.be/qh6L90wQPv8

Edited by DrMadolite, 12 August 2012 - 12:48 PM.

- Awl you're base are belong me! -

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

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.