Displaying a Triangle Grid
Learn to display a grid of triangles by adding new methods to the TriangleGrid class in Ruby.
We'll cover the following
Displaying a grid of triangles in Ruby
Displaying a grid of equilateral triangles turns out to be really straightforward. 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 side is the same length (
width = s
half_width = width / 2.0
height = s * Math.sqrt(3) / 2
half_height = height / 2.0
We’ll let our center point be at a point halfway between the apex and the base and halfway between the east and west vertices. If we base our coordinates on that point, then our vertices can be defined relative to it using the computed width and height values:
west_x = cx - half_width
mid_x = cx
east_x = cx + half_width
Get hands-on with 1400+ tech skills courses.