teaching machines

Cylinder

May 20, 2021 by . Filed under public, slai-2021.

This post is part of a course on geometric modeling at the Summer Liberal Arts Institute for Computer Science held at Carleton College in 2021.

You are on a roll. Literally. Your next shape is a cylinder, and the word cylinder comes from the Greek noun kulindros, which means roller. A cylinder is a bit like a cone in that it’s round. However, it has two caps, and it doesn’t taper. Interestingly, the code to produce a cylinder will have a very similar structure to the code that you wrote to produce a grid.

Draw

Before you go anywhere near code, first draw on paper a cylinder made of triangles. What decisions do you find yourself having to make as you draw? Those decisions are likely to suggest the parameters your function will need.

Function

Write a function named generateCylinder. Have it accept these parameters:

Create an empty positions and triangle arrays and return them in an object.

Vertex Positions

A cylinder has the same topology as the grid you wrote earlier. In a sense, a cylinder is a grid that has been rolled up. Vertices in both are connected to their neighbors to the north, east, south, and west. Though the connections are the same, the positions themselves are computed a bit differently.

Copy the code you used to generate positions in generateGrid to your generateCylinder function. What about the coordinates needs to change?

With the grid, you mapped the latitude to y-coordinate of the grid. The cylinder is no different than the grid along the y-axis, so the y-coordinates do not need to change. However, the longitude must be manipulated so that the x- and z-coordinates curl around. Make this curl happen by turning the longitude index into an angle, as shown in this pseudocode:

radians = ilongitude / nlongitudes * 2 * pi

Compute the x- and z-coordinates using the angle and the radius. Render your cylinder as points and make sure you see the curled grid.

Add two more vertices in the centers of the bottom cap and the top cap. What are the indices of the positions at the centers of the caps? You’ll need them later.

Side Triangles

Good news. The triangles on the sides of the cylinder and are almost connected together in the same way as the grid. Start by copying the code for generating the grid’s triangles. Render it with shading. What do you notice?

You should find one wedge of the cylinder missing. This is where the cylinder’s topology is different than the grid. We want the final line of longitude to connect back up with the first line of longitude. To make this happen, extend your loop to visit each possible longitude index. When calculating the next line of longitude inside the loop, use wrapped addition.

Cap Triangles

Add triangles to seal off the caps. As with your circle and cone, all triangles on a cap should share the vertex at its center.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *