Opening Up Graphics BBC Dragon Spectrum Today's micros offer tremendous opportunities for colour graphics. You do not need to be Salvador Dali to conjure up surreal shapes in unnaturally bright colour any more. Time Langdell illustrates graphics for the beginner, from simple shapes to 3D rotations. [ This article discusses three different computers, one of them the ZX Spectrum. I've typed up the entire thing, both because it would be hard to cut out all the non- Spectrum material and still leave a readable article, and because the comparison between the machines is interesting. There is a TZX which goes with this text, on which I've put all programs originally intended for the Spectrum (including the one-liner), as well as those programs for the other machines (two for the Dragon and one for the Beeb) which could be reasonably converted to the Speccy. RLB - February 2012. ] High-resolution Drawing and Plotting is a feature of the BBC Model B, the Dragon 32 and the ZX Spectrum. Although the BBC machine has finer definition both the Dragon and the Spectrum have such built-in facilities as circle drawing. Simple plotting The Spectrum is the easiest to use for simple Plotting to the screen. You simply envisage the screen as a matrix of dots 256 wide by 176 high and use a straightforward PLOT x,y statement to place a dot in the required position. Adding OVER 1 to the statement removes the dot: PLOT OVER 1;x,y On both the BBC and the Dragon you must first choose your mode of resolution. The BBC offers a choice between a 256 by 16 [sic - I presume, but do not know, that 160 was meant] graphics screen, 256 by 320, or 256 by 640. Once the level of resolution is set, you can use Move or Plot to place dots, or pixels, on the screen. Move has the form Move X,Y and moves the graphics cursor to the position X,Y on the screen without drawing anything. Plot draws using the following, very simple form: PLOT x,y,k. X and Y are co-ordinates again, but K can take one of the following values: 0 Move relative to last point. 1 Draw line relative in current foreground - INK - colour. 2 Draw line relative in logical inverse colour. 3 Draw line relative in current background - PAPER - colour. 4 Move to absolute position - same as using Move. 5 Draw line absolute in current foreground colour. 6 Draw line absolute in logical inverse colour. 7 Draw line absolute in current background colour. Moreover K can have higher values: 16 to 23 draw the lines as dotted, and 80 to 87 draw filled triangles. The BBC has many more of these facilities than the Spectrum. The Plot command is also used to draw lines on the BBC, whereas a separate Draw command is used on the Spectrum. This allows you to draw between two points defined by the last position Plotted and the co-ordinates of another point given after the Draw keyword: DRAW 128,88 The Dragon does not use Plot, but rather Set and Pset depending upon which mode of resolution you have chosen. In low resolution Set is used and Pset have similar forms: Set (X,Y,C) and Pset (X,Y,C). The two co-ordinates of the point to be plotted are inside the brackets followed by a code number for the colour of the dor. In BBC Basic this colour is set with a GCol command just prior to the Plot- ting, whereas in Spectrum Basic one can either set the Ink colour globally or within the Plot statement itself, so that the colour is only that of the dot: PLOT INK 2;128,34 Drawing lines on the Dragon is done using Line, in the following form: LINE (100,100) - (130,135),PSET The co-ordinates of the line's starting point are put in- side the first brackets. The ending point is put inside the second brackets. The statement must then be terminated with PSet. The Dragon can also draw a box with these co-ordinates by simply adding a B after the PSet. Adding BF, moreover, creates a filled box at those co-ordinates. More complex statement The Dragon also has Draw, but this refers to a more com- plex Basic statement. Using Draw on the Dragon you can cre- ate a whole series of dots and lines held within a string. The following aspects may be included in a Draw expression: M = Move the draw position U = Up D = Down L = Left R = Right E = 45º angle F = 135º angle G = 225º angle H = 315º angle X = Execute as substring and return C = Colour A = Angle S = Scale N = No update of Draw position B = Blank - no Draw, just Move In many ways this range of options is similar to the range of values of K on the BBC machine, plus the ability to define Drawing at angles to current positions, and scaling a graphic up or down. A Draw string might be: 10 DRAW "BM128,96; E25; F25; G25; H25;" This draws a square standing on one of its corners. Both the Dragon and the Spectrum can draw circles with a single command; the BBC cannot. For the Spectrum, a simple CIRCLE x,y,r is needed, where x and y are the co-ordinates of the circle's centre and r is its radius. The Dragon's statement is a little more complex because it makes allow- ance for drawing ovals and only parts of circles. It has the form CIRCLE (X,Y),R,C,HW,S,E where X and Y are the centre's co-ordinates again, R is the radius, C is the code of the colour to be used, HW speci- fies the height/width ratio, S specifies the starting point of the circle, and lastly E specifies the end point of the circle. The Spectrum attains partial circles and arcs by using its Draw command in this manner: DRAW x,y,PI This would draw a semi-circle. Spectrum owners might like to try this brief program by Andrew Glaister: PLOT 55,27: DRAW OVER 1;120,120,59^3*PI This single line actually produces quite amazing results which are peculiar to Spectrum Basic's OVER and circle drawing facilities. OVER, on the Spectrum, operates exclu- sive or printing to the screen, and this is also available on the BBC machine. The Dragon 32 is the only machine of the three with a Paint command. This works by simply stating the starting point of the Painting process, the colour of the Paint and the colour of the line where the Painting should end. For those with a Dragon, program D1 uses both the Paint and Get/Put features. Get and Put are Dragon commands which can come in very useful for fast-moving games and animation. They get an area of the screen within a box, defined by co-ordinates X and Y, and store the points which make up that box in an array. This array can then be put back anywhere else on the screen. The BBC machine has such fast Basic that it can attain similar results by simply Plotting or Printing user- defined characters on the screen. The Spectrum, however, has neither the Get/Put facility, nor the speed of the BBC machine. In trying to speed up graphics in games it is worth trying to put the characters into a string on the Spectrum, and then Printing the string at progressive positions on the screen. The alternative on the Spectrum is the POKE to the screen, but this can be difficult due to the way the screen is mapped. The first byte of each character position on the first eight rows is poked first, followed by the second byte, and so on until the characters in the first eight rows are com- plete. Then the next eight rows are done in the same way, and finally the bottom eight rows. The Dragon can perform fairly smooth graphics using its Get and Put instructions. Here, for instance, is a program which Gets a circle in the upper left-hand corner of the screen and Puts it at intervals across the screen, clearing the screen between each Put. The fairly smooth motion of the Dragon is illustrated in program D2. The Spectrum is slower than the other machines, and short of machine code you will have to resort to tricks to por- tray moving graphics. For instance, if you have two objects moving on the screen at the same time - a laser beam or bullet speeding toward a spacecraft for instance, then you would be advised to determine the speed of the spacecraft - when no fire is occurring - by the length of an overall delay loop. When the laser is fired, the delay loop slowing down the craft should be decreased and the travel of the laser arranged to take its place. Craft and laser Thus the movements of the craft and laser would inter- change rather than having the craft stop every time the laser fires. Even this method, though, cannot allow you to create very complex moving games on your Spectrum, although you can Poke to the screen. First define the eight bytes which make up the character you are displaying, say, an alien created with user-defin- able graphics. Then Poke the first byte into the first location of the display file, 16384, followed by the next byte Poked to the location 32*8 bytes further on, and so on through all eight bytes. Then Poke these locations with zero to wipe the character off, and go on to Poke the same eight bytes into locations 16385, 16385+(32*8), and so on. You will find that this allows you to create faster-moving graphics, although your character will appear to scroll into and out of existence. Holding the graphics information in arrays can also be recommended for the Spectrum. Using control characters in such strings allows creation of very complex figures which would otherwise take several lines of Basic. The Dragon lets you add either S or R to a Draw statement in order to Scale the drawing up or down, or Rotate it about a given angle. You can imagine how useful this is if you want a plane to bank toward you and grow in size as it does so. Neither the BBC nore the Spectrum has these facilities but simulating rotation is not too difficult. It involves the realisation that, given any set of co-ordinates, X and Y, the new co-ordinates will be: New X = Old X * COS a - Y(old) * SIN a New Y = Old Y * SIN a + X(old) * COS a where a is the angle you are turning the shape through. Program S1 is an example written in Spectrum Basic but easily translatable to the BBC machine. Rotating shapes This draws a parallelogram in the centre of the screen, then draws various rotations of the shape on the left-hand side. Unfortunately, in Basic, this routine is rather slow compared to the Dragon's built-in features, and not much use in games involving motion of any appreciable speed. The simplest way to produce impressive graphics on the machines is to use some quirk of the way the machine does something. The one-line program for the Spectrum is a very good example of this. Another is the moiré kind of pattern that one can quite easily obtain on any of the three machines using their line-drawing facilities. Program S2 is a version for the Spectrum. A multi-coloured version of this can easily be created by adding INKs to the DRAW statements. Three-dimensional graphics are possible quite easily on each of the computers. An example for the Spectrum is shown in figure 1 but almost an identical program could be writ- ten for the other machines. You can try plotting different functions by changing line 60. In this example the function is: FN a(t)=30*SIN t/12, where t=SQR(x*x + y*y) You can also vary the resolution of the plot by changing the value of r in line 30. This can be anywhere between 2 and about 10. With r=10 the plot will take about 15 minu- tes, but at resolution 2 it can take several hours. The Dragon draws rather spiky-looking circles in one of its modes, but even this can be used to advantage. Program D3 makes a kind of lace pattern. A rather nice spiral cob- web is created by D4. The BBC machine has a unique facility in that by drawing a series of curves or lines in a variety of colours and changing each of them, in turn, into one other colour, an impression of movement can be given. Combining two ideas in the creative graphics package for the BBC by John Cownie it is possible to create a ball which appears to spin in mid-air. See figure 2. In fact, the three colours involved have all been desig- nated as blue, and then selected colours redesignated as white, and back to blue again, in sequence, to give the appearance of movement. Finally, this month's cover was drawn on the BBC. Essen- tially several partial ellipses were drawn turning left or right equally frequently, see figure 3. Line 40 contains all the parameters which are passed to the procedure for the large spray and line 80 passes the necessary data for the smaller spray. Lines 30 and 70, by the way, create the random colours involved.