How to draw a perfect Hexagon!

Posted by (twitter: @theupgradezone)
November 11th, 2014 9:55 am

So I did that maths of drawing a perfect octagon. Now, for a perfect hexagon!

Maths:

First, we must imagine a rectangle with width a and height b.
You must now choose which side, a or b, you want to specify.

Hexagon_001

If you specified a, to find b you must use this equation:

Hexagon_002

 

 

If you specified b, to find a you must use this equation:

Hexagon_003

Which can also be written as:

Hexagon_004

Remember to round to the nearest number!

 

Drawing:

Once you have a and b, we need to specify the 6 coordinates.
For this example, the frame of reference is the center of the rectangle.

The top and bottom two are b/2 up or down and a/2 to each side.

Hexagon_005

The two sides are a to the side.

Hexagon_006

Then you draw lines between the coordinates.

Hexagon_007

And there you have a perfect hexagon!

 

Pseudo Code:

Initialize:
// Let’s say we specified the width, a
width = (width of imaginary square)
height = 2 * width * sin(60)

Loop:

coord_1x = x – width/2
coord_1y = y + height/2
coord_2x = x + width/2
coord_2y = y + height/2
coord_3x = x + width
coord_3y = y
coord_4x = x + width/2
coord_4y = y – height/2
coord_5x = x – width/2
coord_5y = y – height/2
coord_6x = x – width
coord_6y = y

Hexagon_008

//draw_line(from_x,from_y,to_x,to_y)
draw_line(coord_1x,coord_1y,coord_2x,coord_2y)
draw_line(coord_2x,coord_2y,coord_3x,coord_3y)
draw_line(coord_3x,coord_3y,coord_4x,coord_4y)
draw_line(coord_4x,coord_4y,coord_5x,coord_5y)
draw_line(coord_5x,coord_5y,coord_6x,coord_6y)
draw_line(coord_6x,coord_6y,coord_1x,coord_1y)

 

And that’s how to draw a perfect hexagon!

Coming soon: Hexagonal grids!

Tags: ,


One Response to “How to draw a perfect Hexagon!”

  1. And this is where OpenGL is good. In 5 lines of code one can write a function that can draw a triangle quadrangle (square) pentagon hexagon et cetera. One can Evan draw a ‘circle’. (40-side-agone looks like a circle). However, I find the math you did easear then what you have to do to split the x-sided figure into x triangles. OpenGL can be nice, but sometime more complex /more math seams more straight forward.

Leave a Reply

You must be logged in to post a comment.

[cache: storing page]