Displaying a Triangle Grid

Displaying a grid of equilateral triangles turns out to be really straightforward. If you previously worked through the derivation of the measurements for the hexagons, then you’ve already worked through most of what we need for the triangles, too. Again, we won’t go over the derivation itself here, but we’ll walk through the resulting measurements, and then we’ll implement it all in code.

Because we’re working with equilateral triangles, we know that each of their sides are the same length (s). Further, geometry tells us that the height of an equilateral triangle is s3/2. Our triangle’s dimensions, then, are:

 
width = s
 
half_width = width / 2.0
 
 
height = s * Math.sqrt(3) / 2
 
half_height = height / 2.0

We’ll let ...

Get Mazes for Programmers now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.