Cone
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 your way to becoming a sculptor of bits. The next stage of your journey is to forge a cone. At one end of a cone is a single point. At the other end is a circular cap. A sloping ramp leads from the point to the cap. This ramp is shaped like a funnel.
Draw
Before you go anywhere near code, first draw on paper a cone made of triangles. Make the narrow end point upward. 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 generateCone
. Have it accept these parameters:
- An integer
n
that specifies the number of sides that the cone has. The higher the number, the more circular it will be. - The
radius
of the cap, which is on the bottom of the cone. - The
height
of the cylinder measuring the distance between the cap and the point at the top.
Create empty positions and triangle arrays and return them in an object.
Vertex Positions
Nearly all the vertices of the cone are on the cap. Generate them just like you did for the circle using Math.cos
, Math.sin
, and a loop. Instead of generating points in a plane facing the viewer, generate them in a horizontal plane.
Append an additional vertex at the cone’s top. What are its coordinates? What will its index be in the triangles list later on?
Append an additional vertex in the middle of the cap. What are its coordinates? What will its index be in the triangles list later on?
Render your vertices as points and confirm that they are in the right place before moving on.
Funnel Triangles
You are ready to assemble the triangles on the funnel. If n
is 4, how many triangles will there be? What are the indices of the first triangle? The next? What is the general pattern? Write code to generate all the index triplets.
Render your cone as a mesh with lighting. If it doesn’t appear, it may be that the indices are listed in clockwise order. Try reversing the order.
Cap Triangles
When you rotate the funnel to look at the cap, you see nothing, not even the inside of the cone, which is fascinating. Our computer graphics systems tend to discard any triangles that aren’t facing the viewer to make rendering faster. To ensure that we can never see a surface’s backside, you must seal up your models so that they are solids, fully enclosed on all sides.
Add triangles to seal off the cap. What are the indices of the first triangle? The next? What is the general pattern?
As with your circle, all triangles on the cap should share the vertex at the cap’s center.