8.0 Introduction
8.1 Our procedures
8.2 Procedures for enlarged, rotated writing
8.3 Using the procedures for large, rotated writing
8.4 Activities
8.5 Feeding data into programs
8.6 Storing data: arrays
8.7 The core procedures
8.8 Activities
8.9 Discussion of activities
We have now explained enough about the graphics facilities of the BBC Microcomputer for you to be able to program your own very elegant graphics displays. Nevertheless, if you were to try using these graphics facilities seriously, you would still find yourself putting in a great deal of time and effort. So we have done something which we regard as a special feature of this book - something which simplifies the programming for you and removes all the drudgery. In consequence, you become free to concentrate on designing the display that you want, with little thought to the programming. We hope and believe that you will produce better displays as a result.
The special feature is that we provide routines which perform all the essential, mundane parts of graphics for you. All you have to do is to type them in from Appendix I and save them ready to include them in programs as you require them. Or, if you prefer, you can buy them ready-recorded on a cassette. Because of how they are recorded, there is no problem with transferring them to disk. There are two advantages to buying them ready-recorded. Firstly, it saves you the effort of typing; and secondly you will not have the frustration of searching out the typing errors that you, like everyone else, will inevitably make.
In effect, our routines provide a sophisticated extension of BBC BASIC. Firstly, like BBC BASIC, they are instantly available - you merely call on them as procedures. Secondly, just as you do not need to know how the computer achieves such things as printing and listing from the PRINT and LIST instructions of BBC BASIC, neither do you need to know how our routines work although we do explain in Appendix 2, in case you are interested. The essential thing is that you should know how to use the procedures.
This chapter is concerned with introducing you to the procedures. We tell you what they can do, and illustrate with some of the simpler ones. You will see that you need very little programming expertise to use them.
The displays in all the following chapters rely heavily on our procedures. In order to use most of them, you will have to be able to feed data into a program and store it as arrays. We also discuss this later in the chapter.
We provide our routines as procedures which we believe that you will need to call over and over again in graphics programming. A complete list is in Appendix 1, and Appendix 2 explains how each one works.
We shall be saying more about these procedures as we come to them in the rest of the book. In the next section we illustrate the use of three of them.
Any graphics display is livened up with enlarged writing, set at an angle or going round a circle. Using our procedures, you can produce it for yourself, either character by character or as a complete message. It can be in a straight line or round the arc of a circle. You do not have to understand details of the programming. You merely have to call on one of three procedures: PROCmessage, PROCchr and PROCcurve,
These procedures are expressed in terms of:
S$ | which is the string holding whatever is to be printed. |
X,Y | which are the normal screen co-ordinates. They are in the range 0 - 1279 for X and 1023 for Y. |
SC | which is a scaling factor for whatever is to be printed. You select its magnitude through trial and error. |
AN | SA and FA which are angles in radians. Zero is straight up, and anti-clockwise is the positive direction. |
R | which is the radius of a circle in screen units around which text is printed. |
The procedures have to be used in a graphics mode. This is what they do:
PROCchr(X,Y,S$,AN,SC) prints the single character held in 5$ with the lower left-hand corner of the character at the position
PROCmessage(X,Y,S$,AN,5C) prints the message held in S$, starting at the position X, Y.
PROCcurve(X,Y,R,5A,F A,S$,SC) prints the message held in 5$, round the arc of a circle of radius R, starting at the position X, Y.
Once you have the appropriate procedures in memory, you can either call on them within a program or using the direct mode.
PROCmessage(600,0,"Straight up",0,10)
Figure 8.1. The result of a call to PROCmessage.
For example, Figure 8.1 shows the result of a call to PROCmessage in the following program:
10 MODE 4
20 PROCmessage(640,0,"Straight up",0,l0)
30 END
Line l0 gives the co-ordinate for the start of the message as the bottom of the screen (640,0). The angle is zero for straight up and the magnification is 10.
Figure 8.2, a message for all Australians, shows the result of the following call to PROCmessage in the direct mode:
PROCmessage(1200,540,"Wrong way up pommy",PI/2,8)
This message starts at the position 1200,540; the angle is exactly half of PI to give a quarter of a turn to the left, making the message upside-down. The scaling factor is 8.
PROCmessage(1200,540,"Wrong way up pommy",PI/2,8)
Figure 8.2. The result of a call to PROCmessage.
PROCmessage(600,950,"Down",PI,30)
Figure 8.3. The result of a call to PROCmessage.
A message printed straight down the screen requires AN to be PI, as shown in Figure 8.3. It is produced by the following
PROCmessage(640,1023,"down",PI,30)
A message printed across the screen diagonally from top left to bottom right is shown in Figure 8.4 and is produced by the following, where the co-ordinates of the top left-hand side of the 'd' are 60,920, the angle of the writing is just over PI for just over a half turn anticlockwise and SC is 20:
PROCmessage(60,920,"diagonal",PI*1.3,20)
PROCmessage(60,920,"diagonal",PI*l.3,20)
Figure 8.4. The result of a call to PROCmessage.
Screen Display 8.1 is a display along two curves, one bowing downwards from the top two corners and the other coming up from the bottom two corners. It shows the title of this book and our names, and is produced by the program of Listing 8.1.
Put the procedures PROCmessage, PROCchr and PROCcurve into memory. Either you can type them in from the listings in Appendix 1 and save them on cassette tape using the *EXEC format as described in Section 0.2, or you can use the ready-recorded cassette, which is available for purchase. We shall refer to the composite listing of these three procedures as TEXT.
i. Now use our procedures, as explained in the previous section, to write your name across the corners of the screen. You will need to experiment to get the writing in the right place and of a suitable size.
ii. Write a program to write out your name continuously along a sine wave. We give one possibility in Section 8.9.
Screen Display 8.1
Our other procedures, which form the backbone of the rest of the book, help to display information in various ways, for example as graphs, pie charts and histograms. This information has to be already available within the program for the procedure to use and you have to be able to put it in.
There are several ways of feeding data into programs, and we illustrate most of them in the rest of the book. At this stage, however, it is worth saying a few words about two: DATA statements and INPUT statements.
The INPUT statement allows you or any other user of the program to feed in data, while the program is running, in response to requests appearing on the screen. This dialogue between the user and the computer has the advantage of being user-friendly. So we shall use INPUT statements quite frequently.
When we demonstrate the programs relying on our procedures, we shall show the resulting dialogue as part of the screen display, and we shall underline everything that the user puts in, so as to distinguish it from the printout produced by the program.
DATA statements are better for feeding in data when you want to store the resulting display. You can feed in the data, look at the display and then experiment with the data to improve the display by editing the DA T A statements. The editing facilities of the BBC Microcomputer are excellent for this purpose.
Normally, all the data which our procedures require have to be taken from arrays - and your program must set them up. Since the procedures are concerned with graphical data, which normally consists of two co-ordinates, two arrays are generally required: arrays XO and YO. They can store any number of items. X(0) holds the number of the co-ordinates, ie the number of values stored in either XO or YO. Suppose, for the sake of illustration, that there are to be 20 co-ordinates. Then the following is a suitable program line to define the arrays:
10 DIM X(20), Y(20)
You can store data in the arrays by any of the following
statements:
50 X(I)=5 :Y(I)=4.8
or
50 X(I)=n :Y(I)=m
or
50 READ X(I),Y(I)
or
50 INPUT #A,X(I),Y(i)
or
50 INPUT "next values ",X(I), Y(I) etc.
The variable I could be replaced by a number, although a variable is more likely in such a situation.
When all of the values have been stored in the arrays and before any of the procedures are called, you must place the number of stored data pairs into X(0) by a line such as:
160 X(0)=I
or
160 X(0)=18 etc.
The following is a rather facile example of a minimum set of lines that would be required before any of our procedures are called:
10 DIM X(2),Y(2)
20 MODE 4
30 X(l)=1:Y(1)=1
40 X(2)=3:Y(2)=5
50 X(0)=2
Line 10 dimensions the array to set enough storage space aside for the number of values your program requires. As none of the procedures set the graphics mode, you must do this yourself in the program, and this is the purpose of line 20. Such a line is essential before any plotting. Otherwise the program remains in mode 7 and no graphics appears on the screen! Lines 30 and 40 merely place some co-ordinates into the arrays, while line 50 records the number of values stored in the array.
Although we have quite a sizeable collection of procedures, a few form the backbone of the graphics in the rest of the book. We refer to them as the core procedures. They are:
- PR0Cscale
- PROCaxes
- PR0Cgraduate
- PROCnumber
- PROCpoint
- PROCgraph
- PROCnamex
- PROCinamey
- PROCbstln
This is what these procedures do:
PROCscale scales your graphics for you, ensuring that the data that you supply always fits nicely onto the screen. This frees you from having to consider screen co-ordinates and addressable points, etc.
PROCaxes draws a pair of axes i.e. an x axis and a y axis. The axes do not necessarily cross at the origin, because PROCscale examines the range of the values supplied to it and accordingly sets where the axes should cross in order to make the best display. (Incidentally an axis always starts from zero if the smallest inputted co-ordinate is less than 1/3 of the largest although we show later how this can be altered.)
PROCgraduate marks off the axes into appropriate intervals: never less that three or more than 30 graduations along each axis. To make it easier to read values from the graphs, every fifth graduation is larger than the rest.
PROCnumber prints a number against the first and last scale division for each axis to indicate the scale.
PROCpoint marks a single + at the point specified when the procedure is called.
PROCgraph combines the above procedures, i.e. it causes a pair of suitably scaled, graduated and numbered axes to be pio t ted.
PROCnamex names the x axis by printing whatever wording you want along the x axis.
PROCnamey names the y axis by printing whatever wording you want along the y axis. You call PROCnamex and PROCnamey with lines like the following, which must include the words that you want printed:
100 PROCnamex("the x axis")
110 PROCnamey("the y axis")
PROCbstln draws the best straight line through a set of points whose co-ordinates you supply.
We advise you make sure, now, that these core procedures are available for you to use. You can either type them into the computer from Appendix I and save them, or you can buy the ready-recorded tape. We shall refer to the composite listing of these procedures as COMP (to stand for composite). If you have a Model A and get a 'Bad MODE' error message, it means that you do not have enough memory space for a!! our procedures. Nevertheless a Model A does not stop you from getting meaningful displays! You can normally still get them, as long as you do not mind missing out on such things as numbering, graduating and labelling the axes. You merely have to delete the lines of the less-important procedures (PROCnumber, PROCgraduate, PROCnamex and PROCnamey) and ail references to them. You should also delete PROCbstln and all but the most essential lines in your program.
The following activities give an introduction to what the core procedures can do - although we elaborate and extend in the rest of the book.
If you have not already done so, record at least PROCaxes, PROCgraduate, PROCnumber and PROCpoint using the *EXEC format as described in Section 0.2. We shall refer to this as COME' to stand for 'composite'. (If you have a Model A, there is only memory space for PROCscale and PROCaxis.)
i. Enter the following short program to call on PROCscale. Add all the procedures using the command *EXEC "COMP" and run the program, thereby activating the other graphics procedures:
10 MODE 4
20 DIM X(2),Y(2)
30 X(l)=l :Y(l)=l
40 X(2)=3 :Y(2)=3
50 X(0)=2
60 PROCscale
70 END
ii. We shall shortly be asking you to call on some of our procedures in the direct mode. In order to keep this apart from the resulting display, first enter the following, where VDU 28 defines a window in which text is confined:
CLS:VDU 28,10,2,30,0
Now enter the following in direct mode, so that you can see its effect:
PROCaxes
iii. Repeat with each of the following procedures in turn.
PROCgraduate
PROCnumber
PROCpoint(2,2)
PROCpoint(3,3)
Do you see that each axis has a scale running from 1 to 3, with larger divisions for 1, 1.5, 2, 2.5 and 3? This makes it easier to read values from the graph.
iv. Repeat with the following:
PRCpoint(-1)
Why does this seem to have no effect? We discuss this in Section 8.9.
v. Now change line 30 in the above program to:
30 X(1)=.9:Y(1)=.9
Run the program again, followed by the direct mode procedure calls. Note the new scales for the axes, which both now run from zero.
Activity 8.4 ii: Our program for writing your name continuously along a sine wave is given below:
10 MODE4
20 INPUT"Enter your name",A$
30 CLS
40 A$=A$+" "
50 P=0
60 FOR A=0 TO ?*PI STEP PI/l0
70 P=P+1:IF P>LEN(A$) THEN P=1
80 PROCchr(A*200,5l2+400*SIN(A),MID$(A$,P,1)
,0,10)
90 NEXT A
100 END
Activity 8.8 iv: PROCpoint(-1,-1) gives a point off the graph because the point is outside the range given in the call to PROCscale.