Writing Mathlets III: A Call to Math Professionals - The Method

To draw a surface given by z = f(x,y ) in three dimensions, the applet uses a procedure with these steps:

Divide the xy-plane into small triangular regions -- essentially a tiling of the plane with triangles. The triangles should be nonoverlapping, except for shared edges. In the applet, each unit grid square in the xy-plane is divided into four triangles, using the edges of the grid square and the line segments joining each corner of the square to the point at the center of the square.

For each vertex (x,y) of a triangle, compute z = f(x,y). Then the point (x,y,z) in space will be a vertex of the facet directly above the triangle in the xy-plane. In this way, the surface is represented in approximation by a collection of contiguous planar polygonal facets. The vertices of each facet lie on the surface, so smaller facets give better approximations.

For each facet, find a center point (a,b,c) -- there are several ways this could be done, and the choice used here will be discussed below.

Compute the length of the (linear algebra) projection of the vector from the origin to (a,b,c) onto the view vector. (The view vector is the unit vector perpendicular to the computer's screen -- see the second article in this series for a thorough discussion of view vectors.) Since the view vector is a unit vector, this is just the dot product of the vector (a,b,c) with the view vector.

Sort the facets by the value of the length of the projection computed in the preceding step. This should have the effect (and this is where the problems appear) of listing the facets in order from front to back.

Draw the facets on the computer screen (projected to two dimensions, as described in the preceding article), opaquely and in order from back to front, so the facets at the front overdraw the facets at the back.

Since the computation of a center point and its projection onto the view vector must be repeated for every facet, it now becomes important to count the facets. With bounds of -10 to 10 on x and y values, and using the procedure of dividing the xy -plane into four triangles for each unit grid square, each surface has 1600 facets. For two surfaces, that comes to a total of 3200 facets. Note that the last three steps above must be repeated every time the graph is rotated even just a little bit, because the view vector changes, so mouse motions would require these steps to be executed many times and rapidly. The two problematic steps are computing a center point for each facet and sorting the facets by the length of the projection. The real question is whether this center-point projection gives an accurate representation of front-to-back ordering for the facets.