Three Dimensions!

See all articles from CATS v2 n10
Author(s): Roald Schrack
Publication: CATS Newsletter
Pub Details: CATS v2 n10 (Volume 2, Issue 10)
Date: January - February 1985
Pages: 7

Here is a 3-dimensional plotting program that removes hidden lines. You can easily revise the program to plot any function you desire. For illustrative purposes I have plotted the function: y=250/(r+3) * cos(r/10) where r=sqr(x*x+z*z).

I have chosen to represent the axis that is supposed to be sticking out of the paper (the z axis) by a vector depicted in the plane of the paper (the x,y plane) by a line 30 degrees to the x axis. The values plotted are then xp and yp as calculated on lines 70 and 80. This is a convenient and widely used representation. Other representations can be tried which have the effect of looking at the object from different angles. The formulae for the general rotation by angle A about the y-axis and then by angle B about the x axis is:

   xp=x*cos(A)  - z*sin(A)
   yp=x*(-sin(A)*sin(B)) + y*cos(B) -
        z*(sin(B)*cos(A))

The commonly used isometric projection is achieved with A= 45 ° and B= 35.27 °.

The hidden line elimination is achieved by saving in the array V(xp) the largest value of yp plotted at xp. If the point to be plotted has a value of yp less than that of V(xp) then the point is not plotted. The suppression of a point on a particular x-scan is noted by placing a 1. in the array U(xp). This information is then used to determine if the short sections of the z-scan should be plotted. Watching how the code draws the picture will help a great deal in understanding.

The code as written is in need of cleaning up. Lines 140 to and including 190 are essentially repeats of lines 50 through 82 so a subroutine could have been used.

Roald A. Schrack

3-D hidden-line plot of y=250/(r+3)*cos(r/10)
3-D hidden-line plot of y=250/(r+3)*cos(r/10)
  20 DIM v(256)
22 DIM u(256)
30 FOR z=0 TO 120 STEP 10
32 FOR k=1 TO 256
33 LET u(k)=0
34 NEXT k
40 FOR x=1 TO 200 STEP 1
50 LET r=SQR ((x-60)*(x-60)+(z-60)*(z-60))
60 LET y=250/(r+3)*COS (r/10)+15
70 LET yp=y+.433*z
80 LET xp=x+.75*z
81 IF (xp>=255) THEN GO TO 100
82 IF (yp>175) THEN LET yp=175
84 IF (yp<v(xp)) THEN LET u(xp)=1: GO TO 100
86 LET v(xp)=yp
90 PLOT xp,yp
100 NEXT x
115 IF z=0 THEN GO TO 400
120 FOR x=1 TO 200 STEP 10
130 FOR w=z-10 TO z
140 LET r=SQR ((x-60)*(x-60)+(w-60)*(w-60))
150 LET y=250/(r+3)*COS (r/10)+15
160 LET yp=y+.433*w
170 LET xp=x+.75*w
180 IF (xp>=255) THEN GO TO 220
190 IF (yp>175) THEN LET yp=175
200 IF (u(INT (xp))=1) THEN GO TO 220
210 PLOT xp,yp
220 NEXT w
230 NEXT x
400 REM
410 NEXT z
500 STOP

Editor’s note

For those interested in helping the newsletter out, read this.

This comes close to being an ideal submission. Mechanically, the copy was dark, and clean. Editorially, the program is short, with a text explanation of some of the logic behind the program. This text is enough to give the reader a feeling for how much time he would want to invest in understanding the program.

Improvements? Very few people go to the trouble, but adding REMs to explain the function of each element would be helpful. When readying a program for the newsletter, take a little time to review the flow of the program, to see if an outsider could easily understand it. Also, renumbering to an even interval would speed typing it in. Don’t forget – use Radio Shack thermal paper!

MF

Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.