Popular Computing Weekly 17/06/1982 Page 21

Spectrum Programming

In this new slot various contributors explore different aspects of the ZX
Spectrum.

A pixel plot to make you fast on the draw...

Nick Hampshire starts exploring the new computers graphics.

The Spectrum incorporates some quite sophisticated high resolution graphics
capability. The simplest of the high resolution command is PLOT. This simply
plots a point on the screen at the coordinates specified in the parameters
following the command.

The plotting resolution is 256 x 192 pixels, a resolution which is quite
adequate for most applications. To display a point on the screen at a location
100 pixels in the X direction and 50 pixels in the Y direction the command
would be:

PLOT 100,50

A single pixel point occupies a space which is 1/64th of a normal character.
There are 8 x 8 pixels in a character space. The PLOT command can be used to
fill areas of the screen with dots. When used in conjunction with the correct
colour command this can create blocks of colour. This program shows you how
this can be done.

See Spectrum Graphics 3.

In line 10 the input command requests the coordinates of the centre of the
block. The height and width of the block are input in line 20. The input
command in line 30 requests the spacing between dots in the block. This is very
useful since it allows one to create blocks of different density. By plotting
blocks of points of different densities a range of different displays can be
created. In the following program a bar chart is displayed which show the groups
of bars each representing three figures. Each related bar has the same density
of dots displayed.

It should be notes that the algorithm used in this program for plotting a block
of dots is much simpler than that used in the previous program since it is the
bottom left corner of the block which is specified rather than the centre.

See Spectrum Graphics 1

In line 20 the height of the bar and the density of the dots in the bar are
obtained from a DATA statement. The data statement is located in line 100. Note
that the end of data is signalled by a double 0 data which is detected by line
25.

The other principle Spectrum high resolution command is draw. This command
draws a straight line from the last plotted point specified in the coordinate
parameters following the DRAW command. The problem with the DRAW command is
that it is relative to the last plotted point rather than using absolute
coordinates.

Thus the DRAW command obtains the absolute end coordinates of the line by using
the last plotted point as the beginning coordinates and adding to these the
relative offset coordinates following the DRAW command.

A further complication is that in order to determine whether the new end line
is closer to the X or Y origin than the last plotted point the relative
coordinates can be negative.

The complexities of the DRAW command are shown in the following example which
draws a border around the screen.

See Spectrum Graphics 4

Line 10 specifies the start point, X, Y origin, which is at the bottom right
corner of the screen. Line 20 draws the bottom horizontal line, line 30 the
right vertical, line 40 the top horizontal and line 50 the left vertical. Just
as bars and dots could be used to create bar charts so rectangular boxes can be
used to display values in a bar chart. The next program does just this.

See Spectrum Graphics 2.

Note that this routine like the previous bar chart program used data statements
to store the information required to display each bar. This data is read in
line 10 where the coordinates of the bottom right corner of the bar is
obtained, X and Y variables, plus the bar width - W - and height H.

The problems involved in using the DRAW command can, to a degree, be overcome
by using a line drawing routine written in Basic. The main problem with this is
that it is considerably slower than the machine code DRAW command. The
algorithm is fairly simple and can be speeded up by not printing every dot in
the line. The program requires five parameters. These are, start of line X and
Y coordinates - line 10 - end of line X and Y coordinates - line 20 - and the
spacing between dots on the line.

See Spectrum Graphics 5.