Classic Computer Magazine Archive COMPUTE! ISSUE 17 / OCTOBER 1981 / PAGE 96

Graph It On The Atari

John Malcolm Neil
Portland, OR

One application for which the Atari is suitable is drawing accurate plots and graphs. With a minimum of programming effort, one can create graphs of any function — a handy aid for algebra and trigonometry homework.

The following listing is a 2K program to plot functions in graphics mode eight. It will run on either the Atari 400 or 800, providing that you have at least 16K of memory. A major feature of the program is that the user can look at any part of the Cartesian plane. One is not limited to the range of the graphics mode (320×160) when making graphs.

Line 500 sets the default limits for the range and domain of the function:

-20 < x < 20, -10 < y < l0

This region is the user's window on the coordinate plane. To change these values, enter your own window when the computer prints "WINDOW?" in the form:

x1, x2, y1, y2

Where x1 and x2 are the minimum and maximum range, and y1 and y2 are the minimum and maximum domain.

To use the default values, just press RETURN.

The prompt "INTERVAL?" asks you to enter the interval at which to draw the tic marks. The default values are set in line 500 to one.

Enter the interval in the form:

x1, y1

Where x1 is the interval on the x-axis, and y1 on the y-axis.

Press RETURN to use the default values.

The relative positions of the x and y axes are drawn in lines 600-700; lines 800-890 draw the tic marks. The actual plotting of the function (lines 1000-1080) is rather unimpressive. The total range (Y2-Y1) is divided into 320 points, and the value at each is plotted. Should an error occur (divide by zero, cursor out of range), the program skips over that point.

Line 1030 contains the function to be graphed.

To graph another, simply change it to read:

FUNCTION = (your function)

Any variables in it must be "X".

Now you are ready to graph! Try the functions in Appendix E for starters, and this one:

1030 FUNCTION = PEEK(53279)

Press some of the console keys (START, SELECT, OPTION). Wow!

A few words of warning — functions like f(x) = 0 will not be visible because the x-axis is drawn on zero. Also, if it is essential that your graph not be distorted, make sure that the range is twice the domain.

One idea I have for improvement is to couple this program with a machine language routine to dump the screen to a printer. I'm working on that one right now. In the meantime, happy graphing!

400 GRAPHICS 0
410 REM *** INITIAL CONSTANTS ***
500 X1 = -20 : X2 = 20 : Y1 = 10 : Y2 = 10 : XIN = 1 : YIN = 1
510 LIST 1030
520 PRINT "WINDOW (X1, X2, Y1, Y2)";
530 TRAP 550
540 INPUT X1, X2, Y1, Y2
545 IF X2 - X1 = 0 OR Y2 - Y1 = 0 THEN 520
550 PRINT CHR$(125) ; "INTERVAL (X AXIS, Y AXIS)";
560 TRAP 590
570 INPUT XIN, YIN
580 IF XIN < = 0 OR YIN <= 0 THEN 550
590 GRAPHICS 8 : SETCOLOR 2, 0, 0 : SETCOLOR 1, 0, 10 : COLOR 1 : POKE 752, 1
595 REM *** DRAW AXES ***
600 XTOP = Y2/(Y2 - Y1) * 159
610 IF Y2 < 0 THEN XTOP = 0
620 IF Y1 > 0 THEN XTOP = 159
630 YSIDE = ABS(X1)/(X2 - X1) * 319
640 IF X1 > 0 THEN YSIDE = 0
650 IF X2 < 0 THEN YSIDE = 319
700 PLOT 0, XTOP : DRAWTO 319, XTOP : PLOT YSIDE, 0: DRAWTO YSIDE, 159
710 REM *** DRAW TIC MARKS ***
800 TIC1 = XTOP - 1 * (XTOP > 0) : TIC2 = XTOP + 1 * (XTOP < 159)
810 FX = INT(X2/XIN) * XIN
820 XV = (FX - X1)/(X2 - X1) * 319 : IF XV < 0 THEN 850
830 PLOT XV, TIC1 : DRAWTO XV, TIC2
840 FX = FX - XIN : GOTO 820
850 TIC1 = YSIDE - 1 * (YSIDE > 0) : TIC2 = YSIDE + 1 * (YSIDE < 319)
860 FY = INT(Y2/YIN) * YIN
870 YV = (Y2 - FY)/(Y2 - Y1) * 159 : IF YV > 159 THEN 1000
880 PLOT TIC1, YV : DRAWTO TIC2, YV
890 FY = FY - YIN : GOTO 870
900 REM *** GRAPH FUNCTION ***
1000 PRINT CHR$(125) : LIST 1030
1005 TRAP 2000
1010 C = 0 : FLAG = 1
1020 FOR X = X1 TO X2 STEP (X2 - X1)/319
1030 FUNCTION = SIN(X)
1040 IF FLAG THEN PLOT C, (Y2 - FUNCTI0N)/(Y2 - Y1) * 159 : FLAG = 0 : GOTO 1060
1050 DRAWTO C, (Y2 - FUNCTION)/( Y2 - Y1) * 159
1060 C = C + 1
1070 NEXT X
1080 POKE 752, 0 : GOTO 520
1090 REM *** ERROR HANDLER ***
2000 TRAP 2000 : FLAG = 1
2010 GOTO 1060