This post is the first in a series of posts about drawing (and later animating) in 3D using hexagons. I don't spend time here discussing the code in detail, and instead present it as a series of Gists that can be downloaded and experimented with. Here the focus is mainly on the basic theory of drawing and positioning of 3D objects using hexagons.
For the maths side of things, I've found mathopenref.com particularly helpful.
Cube theory
It all begins with a hexagon.
If you can draw a hexagon in code (by determining its vertices from its origin and radius) then you can also draw a cube.
A cube (in the orientation shown above) is simply a hexagon that has lines drawn from every other vertex) to its origin. These lines complete the visible faces of the cube.
Cube code
The following code produces a coloured cube that is shaded to assist in the illusion of the cube. (Note: There are areas where I already know the code could be optimised, but I'm writing down the theory first and will improve the code over time.)
In the code, I simple drawing the visible sides, but there is nothing stopping us from adding the hidden sides should we wish to manipulate the object in 3D space.
Prism theory
Hexagons can be used for more than creating cubes. By determining the midpoint between the uppermost vertices that are opposite one another it is possible to draw a prism.
A much flatter prism could be drawn by using the origin of the hexagon as the point at which the tips meet.
The code for adding the prism can be found here.A much flatter prism could be drawn by using the origin of the hexagon as the point at which the tips meet.
Prism code
In this example the base stretches to the extremity but the tip does not. If we wish to know the co-ordinates of the tip we need to store that information or calculate anew when required.
Spacing and positioning cubes
A second hexagon centred on a vertex of a first hexagon will provide a position next to, behind, above and so on, depending on which vertex is chosen. For example:
This information cannot only be used to place blocks but to also leave block-sized spaces.
Spacing and positioning code
Spacing and positioning code can be located here.
And that concludes this first post on the power of hexagons. (I'll leave you to add the flags you see in the image at the top of the page.)
Comments
Post a Comment