# teaching machines

## Capsule

May 26, 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.

Suppose you grabbed the North Pole, somehow, and I grabbed the South Pole. And then we pulled on the poles. What shape would we get? Earth would look like a pill or capsule. In this exercise, you will make a capsule.

### Draw

On your paper, draw an arc that forms the bottom-right quarter-arc of a circle, starting at the south pole and moving counter-clockwise to the east pole. Draw a straight line upward. Then draw another quarter-arc, this time stopping at the pole. You should see the radial cross section of a capsule.

Draw five or more points along the arcs only. Do not include the poles.

As with the sphere, the points you have drawn represent a set of seed points along the first line of longitude. To generate the other lines of longitude, you will rotate this first line around the y-axis.

### Function

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

• An integer nlatitudesPerHemisphere that specifies the number of lines of latitude that each hemisphere of the capsule has. The higher the number, the more spherical they will be.
• An integer nlongitudes that specifies the number of lines of longitude that the capsule has. The higher the number, the more spherical it will be.
• The radius of the capsule.
• The height of the capsule’s cylindrical body.

Copy your code from generateSphere into this function. Much of it will be the same.

### Seed Positions

The seed positions of the capsule are very similar to the seed positions of the sphere, but the first half of them are pushed down and the second half are pushed up. That’s it. You’ll accomplish this by breaking the loop into two, one for each half. Follow these steps to adapt your code:

• Note how the first parameter has a different meaning that it did with the sphere. You still need to know the total number of lines of latitude for your later code. Compute the total number of lines of latitude and store it in nlatitudes.
• All told, each quarter-arc is $\frac{\pi}{2}$ radians, and you want to break each into nlatitudesPerHemisphere slices. Compute the number of radians per slice and store it in radiansPerSlice.
• Adapt the loop to visit each latitude index in just one hemisphere.
• Range-map the latitude index so that it generates the seed points in just the southern hemisphere, whose interval is $[-\frac{\pi}{2} + \mathrm{radiansPerSlice}, 0]$.
• Tweak the y-coordinate calculation so that the seed points are pushed downward by half the cylinder’s height.
• Duplicate the loop.
• Modify the range-mapping of the second loop so that it generates the seed points in the northern hemisphere.
• Tweak the y-coordinate calculation so that the seed points are pushed upward by half the cylinder’s height.

As you did with the sphere, check that your points look like a cross section of a capsule. Return just the seed points as a flat array with this return statement:

return {positions: seeds.flatMap(seed => seed.toArray()), triangles};


Render these as points.

### Vertex Positions

Once you’ve confirmed that the seed points are in good order, generate the full positions array just as you did with the sphere. Restore your return statement, but continue to render the capsule as points. Do you see a capsule shape? If not, let’s talk!

### Triangles

The triangles of a capsule are generated the same way as a sphere’s triangles. Render your capsule as a mesh. Does it look like a capsule? If not, let’s talk!