Plotting contour surfaces of a function of three variables

9
Plotting Contour Surfaces of a Function of Three Variables GRANVILLE SEWELL University of Texas at El Paso A technique is presented for the graphical representation of some contour (level) surfaces of a function of three variables defined by its values on an array of points (zi, yj, .zk). The algorithm involves drawing and projecting the contour curves corresponding to the cross sections I: = xi and y = yj. Because the curves are drawn as thick, opaque bands, which mask all bands in the immediate background, a sense of depth is retained, yet background contours are still visible between the bands. Categories and Subject Descriptors: 1.3.5 [Computer Graphics]: Computational Geometry and Object Modeling-curue and surface representation; 1.3.7 [Computer Graphics]: Three- Dimensional Graphics and Realism--visible line/surface algorithm General Terms: Algorithms, Performance Additional Key Words and Phrases: Contour surfaces 1. INTRODUCTION Visual representation of a function of two variables is routinely done using computer graphics, usually by means of a three-dimensional surface plot [2] or by drawing the contour (level) curves of that function [l]. A pleasing visual display of a function of three variables, on the other hand, is much more difficult to obtain, and usually several plots are required, each displaying the function on one cross section. The computer program presented here, CON3D, attempts visually to represent a function of three variables in a single plot by drawing several contour surfaces. The difficulties in plotting even a single contour surface are illustrated in Figures 1 and 2. In Figure 1 the top half of the surface x2 + y2 + z2 = 1 is shown as drawn by a common surface plotter routine [2], which uses a “hidden-line” algorithm to mask those portions of the surface hidden by nearer portions. The masking technique employed by this algorithm is designed only for surfaces that can be represented in the form z = f (x, y), and generalization to arbitrary surfaces is difficult. A more important defect of the traditional hidden-line approach is that one contour may completely obscure other contours, for example, if the contours are concentric spheres. Author’s address: Mathematics Department, University of Texas at El Paso, El Paso, TX 79963. Permission to copy without fee all or part of this material is granted provided that the copies are not made or distributed for direct commercial advantage, the ACM copyright notice and the title of the publication and its date appear, and notice is given that copying is by permission of the Association for Computing Machinery. To copy otherwise, or to republish, requires a fee and/or specific permission. 0 1966 ACM 009%3506/38/0300-0033 $01.50 ACM Transactions on Mathematical Software, Vol. 14, No. 1, March 1988, Pages 33-44.

Transcript of Plotting contour surfaces of a function of three variables

Page 1: Plotting contour surfaces of a function of three variables

Plotting Contour Surfaces of a Function of Three Variables

GRANVILLE SEWELL University of Texas at El Paso

A technique is presented for the graphical representation of some contour (level) surfaces of a function of three variables defined by its values on an array of points (zi, yj, .zk). The algorithm involves drawing and projecting the contour curves corresponding to the cross sections I: = xi and y = yj. Because the curves are drawn as thick, opaque bands, which mask all bands in the immediate background, a sense of depth is retained, yet background contours are still visible between the bands.

Categories and Subject Descriptors: 1.3.5 [Computer Graphics]: Computational Geometry and Object Modeling-curue and surface representation; 1.3.7 [Computer Graphics]: Three- Dimensional Graphics and Realism--visible line/surface algorithm

General Terms: Algorithms, Performance

Additional Key Words and Phrases: Contour surfaces

1. INTRODUCTION

Visual representation of a function of two variables is routinely done using computer graphics, usually by means of a three-dimensional surface plot [2] or by drawing the contour (level) curves of that function [l]. A pleasing visual display of a function of three variables, on the other hand, is much more difficult to obtain, and usually several plots are required, each displaying the function on one cross section. The computer program presented here, CON3D, attempts visually to represent a function of three variables in a single plot by drawing several contour surfaces.

The difficulties in plotting even a single contour surface are illustrated in Figures 1 and 2. In Figure 1 the top half of the surface x2 + y2 + z2 = 1 is shown as drawn by a common surface plotter routine [2], which uses a “hidden-line” algorithm to mask those portions of the surface hidden by nearer portions. The masking technique employed by this algorithm is designed only for surfaces that can be represented in the form z = f (x, y), and generalization to arbitrary surfaces is difficult. A more important defect of the traditional hidden-line approach is that one contour may completely obscure other contours, for example, if the contours are concentric spheres.

Author’s address: Mathematics Department, University of Texas at El Paso, El Paso, TX 79963. Permission to copy without fee all or part of this material is granted provided that the copies are not made or distributed for direct commercial advantage, the ACM copyright notice and the title of the publication and its date appear, and notice is given that copying is by permission of the Association for Computing Machinery. To copy otherwise, or to republish, requires a fee and/or specific permission. 0 1966 ACM 009%3506/38/0300-0033 $01.50

ACM Transactions on Mathematical Software, Vol. 14, No. 1, March 1988, Pages 33-44.

Page 2: Plotting contour surfaces of a function of three variables

34 l Granville Sewell

Fig. 1. Sphere drawn using hidden-line masking.

Fig. 2. Sphere drawn without masking.

ACM Transactions on Mathematical Software, Vol. 14, NO. 1, March 1988.

Page 3: Plotting contour surfaces of a function of three variables

Plotting Contour Surfaces of a Function of Three Variables 35

Fig. 3. Sphere drawn using thick bands.

An obvious alternative is simply to draw the surfaces without masking. As illustrated by Figure 2, this approach is unacceptable as it leaves us without any depth perception. We have chosen an approach intermediate between Figures 1 and 2, and use thick lines, or bands, to draw the contours. These bands are made to look opaque by masking the area behind them, and thus a sense of depth is retained, yet the area between the bands is transparent so that the back sides of the contour, and other contours, are still visible. Figure 3 shows the surface x2 + y2 + .z2 = 1 as drawn using this idea.

2. DRAWING THE CONTOURS

The subroutine CON3D expects the function to be defined by a three-dimensional array specifying its values on a uniform three-dimensional rectangular grid of points (Xi, yj, zk), which, for simplicity of presentation, we will assume to correspond to the unit box, 0 5 xi 5 1, 0 I yj 5 1, 0 5 zk I 1. For each value of xi, the contour curves of the bivariate function f(x;, y, z) are drawn. On each grid rectangle of this cross section, the contours of the bilinear interpolant to the function f are drawn, approximately, using straight lines to connect the points where these contours intersect the edges of the grid rectangle. Great care is taken in this stage of the calculation to avoid “accidents of probability zero” (which are not really impossible in practice), that is, to ensure that contours that are coincident with a grid line pass exactly through a grid corner, etc., are properly treated.

ACM Transactions on Mathematical Software, Vol. 14, No. 1, March 1988.

Page 4: Plotting contour surfaces of a function of three variables

36 l Granville Sewell

If the user specifies a view direction of longitude 8 and latitude a, the points (x, y, z) on each contour curve are projected onto the plane perpendicular to the view direction using the transformation

a = x sin8 - ycose P = x sin@cos8 + ysin@sinO + zcos@,

where (a, p) are the rectangular coordinates of the plot surface. Each contour line is drawn as a thick band that masks all lines directly behind

it, as seen from the view direction. This masking is accomplished by parameter- izing the plot area using the coordinate system

6(y, 2) = -asin@cos8 + psin8 = ysincP + zcos+sin8 C(X, 2) = asin9sin8 + pcos8 = xsin@ + zcos~cose.

Since it is required that 0 < 8 < $r, 0 < CD < $T, 6 and 6 are always nonnegative. When a contour line segment (xi, y + sY, z + sZ), 0 5 s 5 1, is to be plotted, the two line segments (Xi T WAX, y + sY, z + sZ), 0 I s I 1, are drawn (w is currently 0.1, and Ax is the spacing of the x planes). The pen is up whenever these lines pass through an area masked, as explained below, by a previously drawn band.

Then the area between them, represented in the (6, E) coordinate system by

(6(y f sY, z f SZ), C(Xi + ~wAx, z + SZ))

or, using the linearity of 6 and E, by

for 0 5 s 5 1, -1 5 t 5 1,

(~(Y,z)+sS(Y~Z), c(Xi,~ +sZ)+ t~(wAx,O)) for 05s5l,-llt51,

is masked; that is, this area is marked using a two-dimensional bit array, MASK, to ensure that no further lines are drawn in the area. MASK(1, J) is flagged if the point 6 = I/M, c = J/M is in the masked area (M is a resolution parameter). All the elements MASK(1, J) that correspond to points in the masked area can then be flagged using a simple double loop, in which I varies from I1 to I1 + 12, where I1 = M X 6 (y, z) and I2 = M x 6 (Y, Z), and then J varies, for each fixed I, from Jl - 52 to Jl + 52, where Jl = M X E (Xi, z + (I - 11)/12 X 2) and 52 = M X e(wAx, 0). If 6 had not been chosen to be independent of x, it would have been much more difficult to identify all the integer pairs (1, J) corresponding to points in the masked area.

For each value of yj, the contour curves of the bivariate function f(x, yj, z) are drawn in a similar fashion. Naturally, the cross sections x = xi and y = yj are processed in an order that ensures foreground lines precede background lines.

A conventional surface plotter typically uses one-dimensional real arrays to flag the masked area, since in this case the masked area is a simply connected region that can be defined by its upper and lower boundary curves. For our problem, however, the masked area is multiply connected, and a two-dimensional array is required.

3. RESULTS

Figure 4 shows two concentric spheres, representing two contour surfaces of the function f(x, y, z) = x2 + y2 + .z2 (defined by its values on an 11-by-11-by-11 grid). From this figure it is clear that the number of contour ACM Transactions on Mathematical Software, Vol. 14, No. 1, March 1988.

Page 5: Plotting contour surfaces of a function of three variables

Plotting Contour Surfaces of a Function of Three Variables 37

Fig. 4. Two level surfaces of x2 + y2 + z2. Contours and labels are drawn using contrasting colors in original.

surfaces that can be represented in a pleasing manner using this algorithm is still very limited. Nevertheless, four contour surfaces of the function f(x, y, z) = x2 + (y - 1)2 + z2 are plotted in Figure 5, showing that, under more favorable circumstances, several contour surfaces may be represented in an intelligible manner. The subroutine CON3D calls for a new pen for each new contour, and thus normally the contours are drawn using different colors. The black and white figures reproduced here were drawn by a version of CONSD that was modified to retrace each stroke three times while drawing certain contour surfaces, in order to simulate the contrast between contours normally achieved with color.

Figure 6 displays the two level surfaces f = -0.5 and f = 0.5 of the function f(x, y, 2) = -x2 - y2 + z2, while in Figure 7 the contours f = 0.5, f = 2.0 are drawn. Each of these two figures has been drawn using two slightly different view angles (3” separation), and slides of the two pictures have been made. When these slides are examined through a stereo viewer, the resulting three-dimensional image gives the viewer an even better idea of what the contour surfaces look like.

Figure 8 shows three level surfaces of a finite difference approximation to the solution U(X, y, z) of the partial differential equation problem:

with

u,, + u, + u,, = 1 in the box (0, 1) X (0, 1) X (0, 1)

u, = 0 on the boundary y = 0 ACM Transactions on Mathematical Software, Vol. 14, No. 1, March 1988.

Page 6: Plotting contour surfaces of a function of three variables

38 . Granville Sewell

Fig. 5. Four level surfaces of x2

ACM

Fig. 6. Two level surfaces of -x2 - y2 + .z*.

Transactions on Mathematical Software, Vol. 14, No. 1, March 1988.

Page 7: Plotting contour surfaces of a function of three variables

Plotting Contour Surfaces of a Function of Three Variables 39

Fig. 7. Two more level surfaces of -x2 - y2 + z*.

and

u=o on the rest of the boundary.

Such a plot provides little quantitative information, but does give us more qualitative information about the solution than perhaps any other single picture could do.

The width of the bands drawn is (currently) equal to one-fifth of the spacing between bands, and thus, if the number of bands drawn is larger than a moderate number (say, 30-40), the bands will be too narrow to be visibly opaque.

CON3D can also be used to generate surface and contour plots of a function f(x, y) of two variables. The surface plot is simply drawn as the contour surface on which the function z - f(x, y) is zero. Figure 9 shows the surface plot generated for f(r, y) = exp{-5 x (x2 + y”)). Whether the semitransparency of this surface is seen as an advantage or a disadvantage compared to conventional opaque (hidden-line) plots depends on the observer’s taste. The contour plot produced by CON3D is similar to traditional contour plots of a bivariate function, and the different contours curves are coded by color. For both contour curve and contour surface plotting, the calculations are ordered to draw one contour at a time, so that the number of pen changes (color changes) is minimized.

ACM Transactions on Mathematical Software, Vol. 14, No. 1, March 1988.

Page 8: Plotting contour surfaces of a function of three variables

Granville Sewell

Fig. 8. Three level surfaces of the solution to V’u = 1.

Fig. 9. Semitransparent surface plot of f(x, y) = exp(-5(x* + y’))

ACM Transactions on Mathematical Software, Vol. 14, No. 1, March 1988.

Page 9: Plotting contour surfaces of a function of three variables

Plotting Contour Surfaces of a Function of Three Variables 41

REFERENCES 1. SNYDER, W. V. Algorithm 531: Contour plotting. ACM Trans. Math. So@. 4, 3 (Sept. 1978),

290-294. 2. WATKINS, S. L. Algorithm 483: Masked three-dimensional plot program with rotations. Com-

mun. ACM 17,9 (Sept. 1974), 520-523.

Received January 1987, revised July 1987, accepted October 1987

ACM Transactions on Mathematical Software, Vol. 14, No. 1, March 1988.