Thinking in 3-D. (plotter tutorial: part 3) Michiel van de Panne.
Of all the things that are impressive about computers, graphics rank high on the list. The set of programs in this article will have your computer expressing its genius and thinking in three dimensions in no time at all. At the same time you will gain a new understanding of the way the graphics are made.
First, a word on using the programs. All the programs are intended to be used on a wide variety of machines with graphic capabilities. I have included routines for using the programs with Apple II, TRS-80 Model I/III screen, TRS-80 Model I/III with CGP-115 plotter, and Color Computer with CGP-115.
To use the programs, simply type them in and add the appropriate lines given for your computer. In addition, fill in the values for XM and YM in line 16 with the values given in Table 1 for your machine and the program you are using. This last step is not necessary for machines using the CGP-115.
For those with other types of computers, it should be easy to write your own routine. If you are using a screen, follow the Apple modifications. Those with plotters should find the routines for the CGP-115 easy to translate.
Line 15 in your own modification should contain the start-up routines for your graphics device such as a clear screen or a create origin for a plotter. It should also contain the coordinates for the center of your screen or paper. Line 16 should contain appropriate scaling factors and will vary with each figure. Table 2 should help you decide on the values for this. For example, the coordinates of the plot in Figure 1b vary from -100 to 100 in both X and Y directions. To put this on a screen of 160 by 160 pixels, you would indicate XC (X center) and YC (Y center) in line 15 as 80. XM and YM in line 16 would both be 0.8 so that the 200 by 200 plot would be reduced to fit the 160 by 160 screen size. Lines 1000 and on should contain a routine to move to a point or draw a line to a point given by X and Y. They should be followed by a RETURN.
The only other recommendation is to make the step size in the programs larger for those computers that have a lower resolution, such as the TRS-80 Model I/III screen.
Now to start with the actual figures. The figures progress in complexity from Figure 1a, a curve formed by straight lines, to the three dimensional objects depicted in Figures 5, 6, 7, and 9 which all rank about equally in difficulty. If you plan on understanding how one of the harder figures is drawn, follow the explanations of the figures from the beginning. On the other hand, if you couldn't care less about the trigonometry involved, you can still benefit from the programs without knowing the principles behind them. Smooth curve
To begin with, Figure 1a is simply a smooth curve that is formed by drawing a set of straight lines. Some of you have undoubtedly constructed this type of figure before. It is also somewhat reminiscent of the string art discussed in our first tutorial. The math involved in this figure is simple enough. The first line is a vertical line. For each successive line, the line starts a given step lower while the line ends the same step farther to the right.
Instead of stopping at this point, a much more interesting figure can be made by placing four of the same curves back to back. The resulting star pattern is shown in Figure 1b. But why stop here? Place five of the star patterns together, and the much more attractive pattern in Figure 1c can be made.
Note how a simple curve can be used many times over to create a complex pattern. This is the technique that is used frequently in graphics. By repeating a simple pattern to form larger patterns and then repeating these larger patterns to form even larger ones, a complex, symmetrical pattern can be created. Sine Wave
The abstract flower-like patterns in Figures 2a and 2b are the first to use a trigonometric function. In this case the radius of all the points is determined with a sine wave. As the radius increases or decreases as determined by the sine wave, the angle from the center is constantly being increased. The actual X and Y coordinates of the points to be plotted are determined as follows if AN is the angle from the center:
X=COS(AN)*RADIUS:Y=SIN(AN)* RADIUS
The complete pattern in Figure 2a is made by repeatedly drawing the same pattern and multiplying the radius by a constant each time to make each pattern larger than the one before. Figure 2b is much the same as figure 2a except that it only has three petals and each successively larger pattern is twisted slightly with respect to the one before it.
Figure 3 also makes use of the sine function. It this case it gives the resulting figure something of a three-dimensional look. The basic pattern used to make Figure 3c is shown in Figure 3a. This figure is then spun around from 0 to 180 degrees (0 to 3.14 radians) in steps of ten degrees. This is exactly what Figure 3b is.
Notice that the Y coordinates remain exactly the same. The X coordinate is multiplied by the cosine of the angle to give the X coordinate that is plotted. The result of this is that it looks almost as if the object is coming out toward you.
The completed pattern depicted in Figure 3c is made from two copies of Figure 3b placed at 90 degrees to each other. This is done easily enough by switching the X and Y coordinates when drawing the second copy. Again, a relatively complex figure is made from a simple pattern that is repeated. Sphere
The next drawing, Figure 4, employs the same type of construction technique as the previous figure. This time the starting figure is a semicircle instead of the pointed outline in Figure 3a. The math used to draw it is also exactly the same as in the last figure. This time, however, the points along the starting figure, namely the semicircle, are calculated instead of being given by an array as done previously.
The points along the semicircle are calculated at five-degree intervals to approximate a real semicircle. The resulting figure has a more three-dimensional appearance than Figure 3c because we can all recognize Figure 4 as a sphere or a ball. Three-Dimensions
The complexities of drawing objects in three dimensions first become apparent in Figure 5. In reality, the way it is drawn is similar to the way the sphere was drawn. Instead of a semicircle, the pattern in figure 5a is spun on its axis to create the three-dimensional object.
Instead of just rotating it through 180 degrees, however, it is spun through a complete 360 degrees (2*PI in radians). This has both its advantages and disadvantages. The advantage is that the object will look truly three-dimensional when it is tilted at an angle so that the whole top surface becomes visible. The disadvantage is that all the lines you can't see in reality (hidden lines) must be removed somehow.
The first problem is to tilt the object so that the top surface can be seen. This requires you to begin to think in terms of three dimensions. Each point on the object has an X, Y and Z coordinate. Think of the X and Y coordinates as being the same as on any graph, while the Z coordinate is a measure of the depth into the paper or distance above the paper. Now if the object is tilted up as in Figures 5c, 5d, and 5e, the X coordinate always remains the same for a given point on the object. All that remains is to find the Y coordinate to be plotted because this will change depending on the depth, or Z coordinate.
To do this, the first thing we must do is calculate the angle of the point with respect to the center. This angle is given by ATN (Y/Z). The distance from the X axis, which will become the radius when the object is tilted around the X axis, should also be known. The distance is given by SQR(Y*Y +Z*Z).
Finally, to actually tilt the object a certain number of degrees, for example 30 degrees, simply add 30 degrees to the angle that was just calculated. The new Y coordinate of the point will be equal to SIN(ANGLE) *RADIUS. In reality the Z coordinate will also change when the object is tilted, but this will not change the two-dimensional view that is plotted.
Now all that remains to be done is to remove the hidden lines. First, the bottom cannot be seen so those lines can simply be deleted. Secondly, the lines representing the sides cannot be seen on the far side of the object, so they can also be deleted from the drawing.
The last remaining hidden lines are those that might be hidden because of the inside edge which is facing out of the paper (see Figure 5c). These can be removed by calculating the angle between the point to be plotted and the corresponding point on the inside edge. If this angle is greater than the angle of tilt, then the point will not be plotted. (For more on hidden line removal, see "Hidden Surface Elimination" in this issue.--Ed.)
The objects in Figures 5c, 5d, and 5e all take a while to plot, so go do something else for ten minutes. Also try tilting the objects at different angles. Figure 5c is tilted at 17 degrees, 5d at 35 degrees, and 5e at 50 degrees. They are all done with 40 steps.
Figure 6 is what is commonly referred to as a torus. It is made in exactly the same way as Figure 5. The outline that is spun on its axis is shown in Figure 6a. The only difference that exists in plotting the two figures is that the hidden lines along the outside edge are more difficult to remove because the edges are round rather than square. The angle between the point to be plotted and the edge possibly blocking the view must be calculated. If it is greater than the angle of tilt, the point will not be drawn. Using A Grid
A much different approach to plotting in three dimensions is shown in Figures 7a and 7b. Instead of having only the actual object drawn, the object is shown as a projection from a flat plane. The procedure for plotting the points is exactly the same as for Figures 5 and 6. The important difference lies in that the points that are to be plotted lie along a grid while with the torus the points to be plotted lie along the radius of the object. Both approaches result in a three dimensional effect.
Removing the hidden lines is a fairly involved procedure once again. The highest points along the half-sphere lie along a line that is at an angle of 30 degrees to the X axis or where Z=0. If the angle between the point to be plotted and the corresponding point in the same line of sight on the sphere is greater than the angle of tilt then the point will not be plotted. I won't go into detail on the trigonometry here because it is only applicable to the drawing of this half-sphere.
There is one more point that should be made about the half spheres in Figure 7. Figure 7b looks much more like a half sphere than Figure 7a. Obviously, this is because the grid is much finer in Figure 7b. The grid is twice as fine in both the X and Z dimensions, which makes for an overall resolution that is four times better. Unfortunately, this means it also takes four times as long to draw. I recommend the finer resolution, a step size of five, if you have the time. The step size is given by ST in line 20.
Figure 8 and 9 have been included to contrast the grid method and the radii method. The points in Figure 8 are located along radius spaced at given angles while those in Figure 9 are located along a grid. Both Figures 8 and 9 show the same figure, namely a cosine wave from 0 to 2*PI that is spun about to create a three-dimensional object.
The grid projection in Figure 9a still does not have the hidden lines removed. To take out the hidden lines in this figure would take far too many calculations and too much time. So instead of removing the hidden lines, I tilted the plane from the original 50 degrees in Figure 9a to 63 degrees in Figure 9b. VT in line 30 represents the angle of tilt of the plane. This way there are no hidden lines and the three dimensions are still retained.
Figure 9c is the same cosine wave used in Figures 9a and 9b, but the wave is not stopped at 2*PI. Instead it is continued to the corners of the grid, making for a most impressive design.
The program in Listing 9 will plot Figure 9b as is. To plot Figure 9c, delete the rest of lines 70 and 160 after the first statement. The figures as shown were plotted with a step size of five, but a step size of ten will still allow reasonable resolution while taking only one quarter of the amount of time.
I hope I have been able to pass on some valuable insights into the techniques used to draw various graphic patterns and three-dimensional figures. Simply polish up on your trigonometry, use your ingenuity, and experiment a lot. Impressive graphics are difficult to make, but are definitely not beyond your reach.