Classic Computer Magazine Archive COMPUTE! ISSUE 79 / DECEMBER 1986 / PAGE 86

Apple Math Graphics

Warren Block

If you're looking for a programming challenge, you might try to duplicate this programmer's feat: Writing an entire graphics program in one line of BASIC. The results are interesting to see, and the process can teach you a lot about efficient programming techniques. The program runs on any Apple II-series computer.

There is only one requirement in creating a useful one-line program. It must do something significant and, of course, be no longer than one line. This condition often results in splendid examples of the hacker's art—programs so obscure that even their author can't figure them out. Even though it would seem that very few programs of this type could do anything at all, some quite elegant little programs have been written in this way.

Some time ago, I decided to try my hand at creating some one-liners limited specifically to unusual graphics displays. Additionally, I decided not to use any machine language routines for speed, although any and all of the features provided by Applesoft would be fair game. I set out writing routines that used polar and Cartesian math functions, shape tables (tables of instructions that define shapes which can be drawn quickly), and several other graphics goodies to draw interesting shapes. Each was a separate program in its own right, but I found that almost every one needed certain common constants (the x and y coordinates of the center of the screen, for example).

After some time, my disk directory was filling up with lots of these little programs. If I wanted to show them to anyone, I had to sit at the keyboard and load them in sequence. The need to reenter all the common constants for every new one-liner I wished to write was slowing me down, too. The obvious solution was to combine all of them into one larger program that shared variable definitions, shape tables, and all the other things that they had in common. This program would cycle through all the routines repeatedly, so there would be no need for anyone to operate the computer.

At the time I combined my original one-liners, there were 12 of them. This made for a moderately interesting program, but some of the routines seemed too similar to me. I removed these and then something strange happened. Maybe it was just the common variable definitions; in any case, after the dust had settled there were 20 different routines in the program, at least half of which were new. [Editor's note: Although all of the routines will fit on one program line if typed in with all spaces omitted, three of the one-liners will not fit on one line if typed with the extra spaces between BASIC keywords that LIST adds when it displays a program line. Thus, to avoid confusion, w; found it necessary when listing the program to split three of the routines—Square Spinner, Roller Coaster, and Roller Coaster II—into two lines. This in no way affects the operation of the routines.]

Portable Logic

If you just want to view the graphics, type in and save the program; then run it. However, many of the program's modules are worth a closer look, especially if you're interested in graphing math functions. The techniques used apply to almost any relative of the Algol language, a group that includes BASIC, FORTRAN, Pascal, Modula-2, and many others. Representatives of polar and Cartesian coordinate systems and functions in both of those systems are present, as are shape table methods that mimic "real" math functions. Oddly, some of the most impressive looking routines were the easiest to create.

Ball of String(line 380) was the first one-liner I wrote for this project, and it is largely responsible for the program's existence. The algorithm it uses is:

1. Pick a random point on the edge of a circle. (Call it Point A).

2. Pick another random point on the circle. (Call this one Point B).

3. Draw a line from A to B.

4. Make the old Point B the new Point A.

5. Go back to step 2.



Each of these interesting designs can be created with just one program line of Applesoft BASIC.

When I wrote this program, I didn't know exactly how it would look, but that's how programming is sometimes. If a happy accident occurs, be ready to accept it and make it work for you.

Multiple Lobes (line 520) is simply a graphic representation of a polar math function that is often seen in textbooks. For those who are curious, its formula is R=C* SIN(A*Theta). The constant A in this formula determines how many lobes the figure will have. In Math Graphics, this number changes randomly, so the display will differ from time to time.

Sunflowers (line 540), Logo Squares (line 560), and Snail Shells (line 580) are all based on the same routine. A random location on the screen is picked; then a shape table is rotated and enlarged while being drawn in that spot. There are minor differences among the three, but the main one is simply which shape table is used. For those who like to experiment, try the following procedure. Press the CTRL and C keys (while the program is running) to stop it; then type the following, pressing the RETURN key at the end of each line (if you have an Apple IIe, make sure the CAPS LOCK key is down):

HGR

HCOLOR = 3

SCALE = 1:ROT=0

DRAW 1 AT 140,96

In the center of the screen, you'll see a tiny mark shaped like a plus sign; this is the shape table that Sunflowers uses, seen at its ordinary size (SCALE = 1). When SCALE is changed, the plus sign can be drawn in. a much larger size—and at a much greater speed—than results from simply using HPLOTs, To see the other two shape tables used by Math Graphics, change the 1 in the last line to 2 or 3

Math Graphics creates some interesting displays, but it's just a starting point. Programming can be a very creative experience, and I encourage you to jump in and modify constants, change loop increments, and generally have a lot of fun.

Apple Math Graphics

For instructions on entering this program, please refer to "COMPUTE!'s Guide to Typing in Programs" in this issue of COMPUTE!.

C2 110 VTAB 4 : HTAB 10 : INVERSE

CC 120 PRINT "

71 130 HTAB 10 : PRINT "APPLE MATH GRAPHICS"

D2 140 HTAB 10 : PRINT "

88 150 NORMAL : PRIN : PRINT

9C 160 PRINT " A GRAPHICS PROGRAM. "

D2 170 FOR L = 1 TD 4000: NEXT

49 180 REM SET UP SHAPE TABLES

56 190 FOR A = 32768 TO 32788:READ D: POKE A,D:NEXT

31 200 POKE 232,0:POKE 233,128

A7 210 REM INITIALIZE

98 220 HGR2:HCOLOR=3

59 230 REM SET NUMERIC CONSTANTS

A9 240 PI = 3.14159:P2=PI * 2:P3-PI/2

B9 250 X=140:Y=96:0=1

J8 260 REM SHAPE TABLE NUMBERS

88 270 S1-1:S2=2:S3=3

C3 280 FOR T=1 TO 20

5D 290 ON T GOSUB 380,400,420,440,460,480,500,520,540,560,580,600,620,640,660,680 700,720,740,760

69 300 REM WAIT A SECOND

3F 310 FOR L=1 TO 1500

FE 320 NEXT

6D 330 REM RESET AND CONTINUE

A9 340 HGR2

D5 350 NEXT

21 360 GOTO 280

BD 370 REM *A BALL OF STRING*

4C 380 OX=52:OY=59:R=95: FOR L=1 TO 150:Z=RND (0)*P2:NX=R*COS (Z)+X:NY=R*SIN (Z)+Y:HPLOT OX,OY TONX,NY:OX=NX:OY=NY:NEXT:RETURN

5B 390 REM*LINED CIRCLE*

E9 400 Z = P2*RND (O):R=95:CX=R*COS(Z)+ X:CY=R*SIN(Z)+Y:FOR L=1 TO 110:Z=P2 * RND (O):NX=R*COS + X:NY=R * SIN (Z) + Y:HPLOT CX,CY TO NX,NY: NEXT:RETURN

B1 410 REM *SQUARE CIRCLE*

F8 420 R=95:R2=R ^ 2+1:FOR J=0 TO R STEP 5:PX=SQR (R2-J*J)PY=SQR (R2-PX*PX):HPLOT PX+X,PY+Y TO X-PX,PY+Y TO X-PX,Y-PY TO PX+X,Y-PY TO PX+X,PY+ Y:NEXT:RETURN

43 430 REM *CLOVERLEAF*

9E 440 Z=2:R=0:ZI=.5:RI=.5:C=10.18:SCALE=3: FOR L=1 TO 150:Z=Z+Z I:R=R+RI:NX=R*COS(Z)+X:NY=R*SIN (Z)+Y : HPLOT X,Y TO NX,NY:ROT= (Z – P2*INT(Z/P2))*C: DRAW S1 AT NX,NY: NEXT:RETURN

41 450 REM *CIRCULAR SPIKES*

B6 460 R1=20:R2=45:R3=115:R4=85:FOR L=0 TO P2 STEP PI/3:FOR L1=0 TO P2 STEP PI/6:X1=R3* COS (L1)+X:Y1=R4*SIN (L1)+Y:X2=R1*COS (L) +X:Y2=R2*SIN(L)+Y: HPLOT X1,Yl TO X2 ,Y2:NEXT:NEXT:RETURN

8A 470 REM*(EXPLODED CIRCULAR SP IKES*

E7 480 R1=20:R2=45:R3=115:R4=85:FOR L=0 TO P2 STEP PI/3:FOR L1=0 TO P2 STEP PI/6:X1=R3*COS (L1)+X:Y1=R4*SIN(L)+Y:X2=R1*COS(L)+ X:Y2=R2*SIN (L1)+Y: HPLOT

X1,Yl TO X2,Y2: NEXT:NEXT:RETURN

B3 490 REM (FLOWING CDNE*

D9 500 R=95:L= 3:FOR J=0 TO P2 STEP PI/100:PX=R*COS (J*L)+X:PY=R *SIN(J)+Y:HPLOT X,Y TO PX,PY:NEXT:RETURN

5F 510 REM *MULTIPLE LOBES*

C3 520 C=95:L=2+2*(RND (O) > .5): FOR TH=0 TO P2 STEP P2/128:R =C*SIN (L*TH):NX=R*COS (TH) + X:NY=R*SIN (TH>+Y:HPLOT X,Y TO NX,NY:NEXT:RETURN

12 530 REM *SUNFLOWERS*

4D 540 FOR J=1 TO 14:RX=280* RND (O):RY=192*RND (O): FOR K=1 TO 50 STEP 5*RND (O)+1.5: SCALE=K:ROT=K:DRAW S1 AT RX,RY:NEXT:NEXT:RETURN