Homework 3, due Thursday, Sep 30

This assignment is going to focus on building parametric shapes. The example parametric shape that we will use is the parametric sphere, for which x,y and z can each be defined as a function of two parameters u and v:

θ = 2 π u
φ = π v - π/2

x(u,v) = cos(θ) cos(φ)
y(u,v) = sin(φ)
z(u,v) = -sin(θ) cos(φ)

Note that the range of values for θ and φ are, respectively:

0 ≤ θ ≤ 2π Longitude: circle around the equator
-π/2 ≤ φ ≤ π/2 Latitude: south pole to north pole

Your job is to do several things. First, use the above information to fill a mesh

   double sphereMesh[][][] = new double[M][N][3];
where M and N are the number of columns and rows of the sphereMesh, and each entry sphereMesh[i][j] is a vertex of the mesh. You should choose values of M and N that produce a sphereMesh that you think looks good.

You'll want to generate i and j in a loop:

   for (int i = 0 ; i < M ; i++)
   for (int j = 0 ; j < N ; j++) {
      double u = i / (M - 1.0);
      double v = j / (N - 1.0);

      // COMPUTE x,y,z HERE AND PUT THE RESULTS INTO sphereMesh[i][j]

Once you have this sphereMesh, then you can use it to create and display lots of spheres and ellipsoids. To create any one sphere or ellipsoid, you just need to define a matrix T (using the same techniques that you used for last week's assignment).

You do this as follows: loop through your source sphereMesh and copy transformed points to a temporary array temp[M][N][3]:

   for (int i = 0 ; i < M ; i++)
   for (int j = 0 ; j < N ; j++)
      T.copy(sphereMesh[i][j], temp[i][j]);
Then you can loop through all the little squares of your temporary mesh, and display each one as a triangle:
   for (int i = 0 ; i < M-1 ; i++)
   for (int j = 0 ; j < N-1 ; j++) {
      drawTriangle(temp[i][j], temp[i+1][j], temp[i+1][j+1]);
      drawTriangle(temp[i][j], temp[i+1][j+1], temp[i][j+1]);
As we said in class, for now you can draw one triangle by drawing the three lines needed to connect its three vertices (just like you did when you drew the flying house in last week's assignment).

I would like you to make some sort of cool shape as a collection of spheres and ellipsoids. You make the different ellipsoids by creating the appropriate matrices.

You'll be able to get a lot more interesting things done if you can use a stack matrix with push() and pop() methods, as we showed last week in that little swinging arm example that I developed in claass. This is a great time for you to implement the Matrix stack. Below is one implementation of a Matrix stack class that you can use if you like:

public class MatrixStack {
   int top = 0, stackSize = 20;
   Matrix3D stack[] = new Matrix3D[stackSize];

   public MatrixStack() {
      for (int i = 0 ; i < stackSize ; i++)
         stack[i] = new Matrix3D();

   public void push() {
      if (top < stackSize) {

   public void pop() {
      if (top > 0)

   public Matrix3D get() { return stack[top]; }