6.0 Introduction
6.1 Choosing a function and scaling it
6.2 Activities
6.3 Drawing the surface
6.4 Hidden lines
6.5 Activities
6.6 Discussion of activities
You have probably admired the three dimensional pictures that computers can draw. This chapter is about those beautifully symmetrical and shaded pictures of ripple surfaces, and it shows you how easy it is to design and draw them yourself.
Screen Display 6.1
A three dimensional ripple surface can be built up from any suitable function. Any one will do as long as it can be written in terms recognisable to BASIC. So, to draw one for yourself, you must first choose a function. We explain the process using a function which, incidentally, derives from optics, where it represents the amplitude of light penetrating through a pin hole onto a screen behind:
SIN(R)/R
You can get an idea of the general shape of this function by plotting it in a suitable graphics mode, in two dimensions as a simple graph. This particular function has a maximum value of 1 where R=0 and a minimum value of about -0.2 so you have to scale up the values worked out for SIN(R)/R before you can plot with a suitable size to fill the screen. If you scale the whole function up, by multiplying by 800, it will fit on the screen nicely. Listing 6.1 gives a program to display the two dimensional shape of this function. Screen Display 6.1 is the result.
As you see, the program uses the point-plotting version of the PLOT statement. Line 40 uses the following special version of the VDU statement, which allows the origin for any future graphics to be altered to X,Y:
VDU29,X;Y;
In the program this sets the origin for graphics to the point 640,200. This is because the function is symmetrical about X=0; so we felt the program would be clearer if the values of X ran from -640 to +640. The scaling takes place in line 60. The 800 enlarges the plot to fill most of the screen and the X/32 controls the number of bumps on the curve. Such scaling is usually best done by starting with an intelligent guess, displaying the resulting plot and then adjusting the scaling.
This activity helps you to appreciate the importance of scaling on the appearance of a display.
i. Enter the program of Listing 6.1 and run it.
Screen Display 6.2
ii. Try altering the overall size of the display scaling factor 800 in line 60.
iii. Try altering the number of 'bumps' in the display by varying the scaling factor 32 in line 60.
iv. In line 50 the value of X is purposely set to start at -640.1 rather than at -640 exactly. Investigate why, by altering to -640.
v. Try adding STEP 4 to line 50 in order to speed things up.
We now show how to use SIN(R)/R to produce the symmetrical three dimensional ripple surface of Screen Dispiay 6.2. The height of any point on the surface is dictated by the value of the function at that point. There is a central, main 'bump' just as there is for the two dimensional view of Screen Display 6.1. Indeed this two dimensional view is a section through the three dimensional one.
As you can see from Listing 6.2, the origin for all the plotting is set centrally on the screen using the VDU29,640;300; statement on line 100. The scale for the surface is set up in lines 50 to 70 in terms of XM, the breadth, ZM the apparent distance front to back and YM which scales the diagram in the up/down direction, affecting the apparent height of the 'bumps'.
Screen Display 6.3
The value of the function SIN(KR)/KR is calculated in the function definition in lines 250 to 270. If you have your own function, you could substitute a different definition. Other functions would be equally suitable, provided that they are calculatable in terms of X and Y, although only certain types give an attractive appearance. By way of illustration, Screen Displays 6.3 and 6.4 rely on different functions. Listings 6.3 and 6.4 give the programs that produce them.
The appearance of three dimensional displays is very much improved if a line or curve is not drawn where it seems to be behind something else. In Listing 6.2, this is achieved in lines 140 to 160. However, as the process is so important in all areas of graphics, we explain in general terms in the next section.
Displays of three dimensional objects invariably have some lines, which, if the surface were solid, would be hidden from sight. Where a program does not remove these lines, the whole impression of solidarity is spoilt. Where the lines are removed, they are referred to as 'hidden lines'.
To remove these lines, the program draws the surface, point by point, moving from the nearest part, which must be in view, to the furthest part, which may well be hidden by something drawn earlier. While each point of the surface is being drawn, the program keeps a note of the heights up and down the screen reached so far. If the program then finds a point which lies between these two values, it must be further away, and therefore hidden from view. So it is not plotted.
Screen Display 6.4
In Listing 6.2, the hidden line removal occurs in lines 140 to 160. Line 140 sets the starting value for the maximum and minimum height on the screen. In Lines 150 and 160 the current value is tested, to see if it lies within the range of the current maximum and minimum. If it does, the point is not plotted. If it does not, the current maximum or minimum is updated and the point is plotted.
i. Try running the three dimensional surface program using the functions which we supply.
ii. Ripple surface programs run rather slowly. How could you make the program of, for example, Listing 6.2 run more quickly? Would there be any disadvantages'? (See Section 6.6.)
iii. Try producing ripple surfaces with your own functions.
Activity 6.5ii: You can make the program run more quickly by changing the step sizes in line 110 and line 120. You might try, for example, 12 and 80 respectively. The resulting display is less attractive because it is less dense.