Chapter 18 - Graphics
Here are some of the most elegant features of the ZX81; they use what are called pixels
(picture
elements). The screen you can display on has 22 lines & 32 columns, making 22*23 = 704 character
positions, & each of these contains 4 pixels, divided up like a slice of Battenburg cake.
A pixel is specified by two numbers, its coordinates. The first, its x-coordinate, says how for it is across
from the extreme left-hand column (remember, X is ACROSS), & the second, its y-coordinate, says how far
if is up from the bottom. These coordinates are usually written as a pair in brackets, so (0,0), (63,0), (0,43)
& (63,43) are the bottom left-, bottom right-, top left-, & top right-hand corners.
The statement
PLOT
x-coordinate, y-coordinate
blacks in the pixel with these coordinates, while the statement
UNPLOT
x-coordinate, y-coordinate
blanks it out.
Try this measles program:
10
PLOT INT
(
RND
*64),
INT
(
RND
*44)
20
INPUT
A$
30
GOTO
10
This plots a random point each time you press newline.
Here is a rather more useful program. It plots a graph of the function
SIN
(a sine wave) for values
between 0 & 2 .
10
FOR
N=0
TO
63
20
PLOT
N, 22+20*
SIN
(N/32*
PI
)
30
NEXT
N
This next one plots a graph of
SQR
(part of a parabola) between 0 & 4:
10
FOR
N=0
TO
63
20
PLOT
N,20*
SQR
(N/16)
30
NEXT
N
Notice that pixel coordinates are rather different from the line & column in an
AT
item. At the end of this
chapter is a diagram which you may find useful in working out pixel coordinates & line & column numbers.
Exercises
1. There are three differences between the numbers in an
AT
item & pixel coordinates; what are they?
Suppose that the
PRINT
position corresponds to
AT
L,C (for line & column). Prove to yourself that the
four pixels in that position have x-coordinates 2*C or 2*C+1, & y-coordinates 2*(21-L) or 2*(21-L)+1. (Look
at the diagram.)
2. Make a cheese nibbler by altering the measles program so that it first fills the screen with black (a black
square is an inverse video space), & then unplots random points, if you have only 1K of memory - that is to
say, the standard machine without any extra memory, - you will find yourself running out of store, so you
will have to fix the program so that it uses only part of the screen.
3. Modify the
SIN
graph program so that before plotting the graph itself it prints a horizontal line of "-"s for
an x-axis, & a vertical line of "/"s for a y-axis.
4. Write programs to plot graphs of more functions, e.g.
COS
,
EXP
,
LN
,
ATN
,
INT
& so on. For each one
you will have to make sure that the graph fits the screen, so you will need to consider
(i) over what range you are going to take the functions (corresponding to the range 0 to 2 for the
SIN