Bottom     Previous     Contents

Appendix 2
How the procedures work

This appendix explains how each of the procedures works. For simplicity, a horizontal and a vertical axis is referred to as an x and a y axis, respectively; and co-ordinates are referred to as x and y co-ordinates.


THE PROCEDURE PROCscale

PROCscale scales any display so that it squarely fills the screen, irrespective of the magnitude and range of the data. This frees you from having to consider such things as screen co-ordinates and addressable points, etc.
PROCscale requires X(0) to hold the number of points to be plotted, and the arrays XO and YO to hold the x and y co-ordinates for these points. There must be a minimum of two X values in X(l) and X(2) and two Y values in Y(l) and Y(2). Neither set of co-ordinates can cover a zero range. For example, although you could plot a graph consisting of a single vertical or horizontal line, the axes must cover a finite range.
PROCscale examines the numbers in the arrays and then sets up the scaling for the other procedures to follow. It must therefore be called before any of the following are called:

PROCaxes PRDCgraduate PROCnumber
PROCpoint PROCgraph PROCnamex
PROCnamey PR0Cbstsl PROChist
PROC3Dhisto

PR0Cscale gives values to the following variables:

i. £SX is set to the smallest x value, rounded to the nearest left-most graduation on any axes.
ii. £LX is set to the largest x value, rounded to the nearest right-most graduation on any axes.
iii. £SY, £LV set the y values in the same way as the x. values.
iv. £D sets the number of divisions along the x axis.
v. £ST is set, in the user's own co-ordinates, to the increment corresponding to one scale division along the x axis.

The operation of PROCscale: smallest and largest values

The first set of lines in PROCscale merely find the smallest and largest values available in the XO and YO arrays. Line 10100 defines the procedure name. Line 10110 sets the initial values for the smallest and largest X and Y values, and line 10120 sets up a FOR ,., NEXT loop. When these lines have been executed, the smallest and largest x values are held in £5*4 and £LX, and the smallest and largest y values are held in £5V and £LV. These lines are reproduced below so that you can easily refer to them.

10100 DEF PROCscale

10110 £SX=X(1) :£LX=£SX :£SY=Y(1) :£LY=£SY

10120 FOR £I=1 TO X(0)

10130 IF£SX>X(£I)THEN£SX=X(£I)

10140 IF£LX<X(£I)THEN£LX=X(£I)

10150 IF£SY>Y(£I)THEN£SY=Y(£I)

10160 IF£LY<Y(£I)THEN£LY=Y(£I)

10170 NEXT

The operation of PROCscale: co-ordinates for the ends of the axes,

The smallest and largest values, as determined by the lines 10120 to 10170, will not normally be ideal values for the ends of the axes, For example, if the smallest and largest value for one axis are 0.1 and 9.3 respectively, then the axis would clearly be best running from 0 to l0. PROCscale contains a sub-procedure, PROCsca1e2 (discussed later), which makes this sort of decision. Lines 10180 and 10190 call on it, to set the ideal end points for the axes, first for the y axis and then for the x. They work by assigning new values for £SX, £SY etc.

10180 PROCscale2(£SY,£LY) :£SY=£S :£LY=£L

:£STY=£ST :£CBY%=£CBN%

10190 PROCscale2(£SX,£LX) :£SX=£S :£LX=£L

:£STX=£ST :£CBX%=£CBN%

The sub-procedure also calculates the step size £ST and a quantity £CBN% which is used to locate every fifth scale division. This division is marked larger than the others by PROCgraduate, to make it easier to read from the axes.

The operation of PROCscale: conversion to screen co-ordinates

The final task of PROCscale is to work out some of the constants which will be required in order to calculate the screen co-ordinates from co-ordinates which are supplied by the user. For these calculations we set the screen area for graphics to a rectangle with 150,100 as the lower left-hand co-ordinate. This area extends 1000 screen co-ordinates horizontally and 800 vertically. These numbers appear in lines 10200 and 10210 which are reproduced below for easy reference. Altering these numbers, alters the area of the screen used for the graphics.
Line 10200 calculates the important scale factors £X and £Y. These are the factors by which the x and y values supplied by the user must be multiplied, so as to bring them into the range of the screen co-ordinates.
Line 10210 adds an offset to prevent the graphics extending down to the bottom left-hand corner of the screen.

10200 £X=1000/(£LX-£SX):£Y=800/(£LY-£SY)

10210 £CONVX=150-£SX*£X :£CONVY=100-£SY*£Y

10220 ENDPROC

The operation of PROCscale: the convert functions

The procedure sets up two important functions FN£CVX and FN£CVY which convert from the x and y values supplied by the user to those required by the computer's plotting routines. Lines 10240 and 10250 below define these functions, using the scale factors and offset set up by lines 10200 and 10210.

10240 DEF FN£CVX(G) =£X*G+£CONVX

10250 DEF FN£CVY(G) =£Y*G+£CONVY

The operation of PROCscale: PROCscale2

PROCscale2 is a part of PRDCscale. For convenience of programming, it was written as a separate procedure, but there would be no sense in calling it from within a program.
The following few sections explain how PRDCscale2 works.

The operation of PROCsca1e2: number formats

PROCsca1e2 relies on the BBC Microcomputer's ability to dictate the format of numbers. The format specification is made initially in line 10278 using @%=&0l0l090A. This dicatates not only the printed format of numbers but, more importantly for our purpose, the format of any number converted to a string using the STR$ function. Line 10290 is an example.

10270 DEF PROCscale2(L,H) :@%=&0101090A

The operation of PROCsca1e2: the choice of origin

It is invariably preferable for a display to include the origin, provided that the graph is not consequently cramped up on one side. The first decision which PROCscale2 makes is in line 10280 and concerns whether the origin should be included in the display. For a graph which shows only positive numbers, our condition is simply (and arbitrarily) that if the smallest number is less than 1/3 of the largest, then the origin should be included. Line 10280 expresses this condition. Without it, there would be no criterion for the inclusion of the origin.

10280 IF L>0 AND L<H/3 THEN L=0 ELSE IF H<0 AND

H>L/3 THEN H=0

The operation of PROCscaIe2: the number of graduations

Lines 10290, 10300, 10310 and 10320 take the smallest and largest numbers supplied to the procedure and convert them to a decimal fraction and an exponent. The decimal fraction is held in £LL and £LH white the exponent is held in £PL.

10290 £LL=VAL(LEFT$(STR$(L),INSTR(STR$(L),"E")-1))

10300 £LH=VAL(LEFT$(STR$(H),INSTR(STR$(H),"E")-1))

10310 £PL=VAL(MID$(STR$(L),INSTR(STR$(L),"E")+1))

10320 £LH=£LH*10^(VAL(MID$(STR$(H),INSTR(STR$(H),"E")+

1))-£PL)

It might be best to illustrate what is happening so far with an example. Suppose that the x axis is being considered and that the user's program has supplied the smallest and largest numbers as 0.01 and 0.0145. Lines 10290 to 10320 will convert these values to 1.0 E-2 and 1.45 E-2. The 1.0 and 1.45 will be held in the variables £LL and £LH respectively with -2 held in £PL.
The next few lines now make the decision as to how many divisions should be along the axis. The variable £D holds the number of divisions which are initially set to the difference between £LH and £LL.

10330 £D=£LH-£LL

Then the two lines 10340 and 10350 multiply up or divide down this difference until it is in the range 4 to 30 inclusive. When multiplying or dividing by ten, these lines also alter the exponent held in £PL to keep track of the magnitude of the number.

10340 IF £D<=3THENREPEAT£D=£D*10 :£PL=£PL-1 :£LL=£LL*10

:£LH=£LH*10 :UNTIL £D>3

10350 IF £D>30THENREPEAT£D=£D/10 :£PL=£PL+1 :£LL=£LL/10

:£LH=£LH/10 :UNTIL £D<30

Before these lines are executed, the £D in our example would have an initial value of 0.45. The condition in line 10340 would result in the value of £D being multiplied by ten and the other values being accordingly adjusted to give £LL=l0, £LH=l4.5 and £D=4.5.

The operation of PROCscale2: values for the ends of the axes

The next line sets the values of £LL and £LH to integer values rounding £LL down and £LH up. This is necessary in order for the scale along the axes to start and finish at sensible numbers.

10360 £LL=INT(£LL+.1) :£LH=INT(£LH+.5)

For our example numbers, this would set £LL to 10 and £LH to 15 for an eventual range along the axes from 0.01 to 0.015. This nicely encompasses the original range of numbers from 0.01 to 0.0145.

The operation of PROCscale2: the guide to every fifth graduation

The next line picks off the [east significant digit of £LL and stores it in £CBN%, as a guide to the graduation procedure when to draw larger graduations every fifth position. With our example figures, this sets £CBN% equal to 0. It also resets the format for presenting numbers.

10370 @%=&10 :£CBN%=SGN(£LL)*(VAL(RIGHT$(STR$(£LL),1))+.1)

The operation of PROCscale2: the smallest and largest values

The last two lines of this procedure then place the smallest and largest numbers represented by the recommended scale in £5 and £L.

10380 £S=VAL(STR$(£LL)+"E"+STR$(£PL))

10390 £L=VAL(STR$(£LH)+"E"+STR$(£PL)) :£ST=ABS(£L-£S)/

(£LH-£LL)

10400 ENDPROC


THE PROCEDURE PROCaxes

PROCaxes draws a pair of axes i.e. an x axis and a y axis.

The operation of PROCaxes: the origin

Axes do not necessarily cross at the origin. Lines 10440 and 10450 examine the range of the values along the axes and accordingly set where the axes should cross in order to make the best display.
Line 10460 draws the x axis and line 10470 draws the Y axis.

10420 DEF PROCaxes

10430 £X0=0 :£Y0=0

10440 IF £SX>0 THEN £X0=£SX ELSE IF £LX<0 THEN £X0=£LX

10450 IF £SY>0 THEN £Y0=£SY ELSE IF £LY<0 THEN £Y0=£LY

10460 MOVE FN£CVX(£SX),FN£CVY(£Y0) :DRAW FN£CVX(£LX),

FN£CVY(£Y0)

10470 MOVE FN£CVX(£X0),FN£CVY(£SY) :DRAW FN£CVX(£X0),

FN£CVY(£LY)

10480 ENDPROC


THE PROCEDURE PROCgraduate

PROCgraduate marks off the axes into appropriate intervals: never less than four or more than 30 graduations along each azis.

The operation of PROCgraduate: the graduations

To make it easier to read values from the graphs, every fifth graduation is made larger than the rest. PROCgraduate is in two parts, one for the x axis and one for the y. A FOR...NEXT loop steps along each axis from the smallest value to the largest with a step size £STX fixed by PROCsca1e. In lines 10510 and 10540 the end of the FOR...NEXT loop is set slightly larger than might seem necessary. Without this, there would be some occasions when one less graduation than expected would be drawn. As PRDCpoint actually draws a small + sign, it is equally suitable for plotting points and for marking both axes, So the call to PRDCpoint in lines 10530 and 10560 actually draws the graduations.

The operation of PROCgraduate: every fifth graduation

The length of the bars of the + sign is controlled by the constant DTA. Examination of £CBX% or £CBY% in lines 10520 and 10550 makes the bars extra large when every fifth division is reached.

10500 DEF PROCgraduate

10510 FOR x=£SX TO £LX+.1*£STX STEP £STX

10520 IF £CBX%MOD5=0 THEN DTA=16 ELSE DTA=8

10530 £CBX%=£CBX%+1 :PROCpoint(x,£Y0) :NEXT x

10540 FOR y=£SY TO £LY+.1*£STY STEP £STY

10550 IF £CBY%MOD5=0 THEN DTA=16 ELSE DTA=8

10560 £CBY%=£CBY%+1 :PROCpoint(£X0,y) :NEXT y

10570 ENDPROC


THE PROCEDURE PROCmmber

PROCnumber prints a number against the first and last scale division of each axis to indicate the scale.

The operation of PROCnumber: the location of the numbers

The numbers are located using the VDU5 statement to print at the graphics cursor. The MOVE statement in line 10600 moves the graphics cursor to a position sufficiently short of the end of the x axis to allow for the length of the string which is to be inserted. The co-ordinate conversion function is used in the MOVE statements to convert from the range of co-ordinates supplied by the user to the screen co-ordinates.

The operation of PROCnumber: numbering the origin

The condition in line 10620 checks to see if the graph includes both +ve and -ve values along both axes. If so, it only prints the numbers at the positive ends of the x and y axes.

10590 DEF PROCnumber :VDU5

10600 MOVE FN£CVX(£LX)-4*LEN(STR$(£LX)),FN£CVY(£Y0)-20

:PRINT;£LX

10610 MOVEFN£CVX(£X0)-150,FN£CVY(£LY)+28:PRINT;£LY

10620 IF £LX>0AND£SX<0AND£LY>0AND£SY<0THEN ENDPROC

10630 MOVE FN£CVX(£SX),FN£CVY(£Y0)-20:PRINT;£SX

10640 MOVEFN£CVX(£X0)-150,FN£CVY(£SY)+30:PRINT;£SY:VDU4

10650 ENDPROC


THE PROCEDURE PROCpoint

PROCpoint marks a single + at the point specified when the procedure is called.

The operation of PROCpoint: the length of the bars

The length of the bars of the + sign is set by the value held in DTA. For most purposes a value of 8 for DTA is suitable. As this routine is used by PROCgraduate, the size of the + is controlled from outside the procedure.

10670 DEF PROCpoint(A,B)

10680 MOVEFN£CVX(A)-DTA,FN£CVY(B):DRAWFN£CVX(A)+DTA,

FN£CVY(B)

10690 MOVEFN£CVX(A),FN£CVY(B)-DTA :DRAWFN£CVX(A),FN£CVY(B)

+DTA

10700 ENDPROC


THE PROCEDURE PROCgraph

PROCgraph displays a graph consisting of points and it draws axes, graduations and labels for the axes.
PROCgraph requires an array XO and YO. X(0) must contain the number of values available within the arrays. X(l), X(2), ,,. must contain the x co-ordinates and Y(1), Y(2), ... must contain the y co-ordinates,

The operation of PROCgraph

PRDCgraph is almost entirely made up from calls to other procedures PROCscaie, PROCaxes, PROCgraduate, PROClabel and PROCpoint. The only other type of program lines which it includes define a FOR...NEXT loop to call PROCpoint for each of the points of the graph stored in X() and Y().

10000 DEF PROCgraph

10010 PROCscale

10020 PROCaxes

10030 PROCgraduate

10040 PROCnumber

10050 FOR £J=1 TO X(0)

10060 DTA=8 :PROCpoint(X(£J),Y(£J))

10070 NEXT £J

10080 ENDPROC


THE PROCEDURE PROCbstln

PROCbstln calculates the straight line which will best represent the points whose co-ordinates are held in the XO and YO arrays.

The operation of PROCbstln

It is rather meaningless to explain the operation of PROCbst1n without also explaining the mathematics behind it - which is beyond the scope of this book. Suffice it to say that lines 10750 and 10760 calculate the mean of the x and y co-ordinates. The sums of the squares of the x and y co-ordinates and their product is then calculated by lines 10770 to 10840. The coefficients M and C are then calculated in line 10850 and 10860 for the equation: Y =MX+C
Line 10880 prints up the equation while line 10890 draws the best straight line.

10720 DEF PROCbstln

10730 LOCAL C,M,I,XX,YY,MEANX,MEANY,sumX,sumY,sumXY,sumYY,

sumXX,MINX,MAXX

10740 XX=0 :YY=0:

10750 FOR I=1 TO X(0) :sumX=sumX+X(I) :sumY=sumY+Y(I)

:NEXT I

10760 MEANX=sumX/X(0) :MEANY=sumY/X(0)

10770 MINX=X(1):MAXX=MINX:sumXX=0:sumYY=0:sumXY=0:XX=0:YY=0

10780 FOR I=1 TO X(0)

10790 sumXX=sumXX+X(I)*X(I) :sumYY=sumYY+Y(I)*Y(I)

10800 sumXY=sumXY+X(I)*Y(I)

10810 XX=XX+(X(I)-MEANX)^2 :YY=YY+(Y(I)-MEANY)^2 :XY=XY+

(X(I)-MEANX)*(Y(I)-MEANY)

10820 IF MINX>X(I) THEN MINX=X(I)

10830 IF MAXX<X(I) THEN MAXX=X(I)

10840 NEXT I

10850 M=(X(0)*sumXY-sumX*sumY)/(X(0)*sumXX-sumX*sumX)

10860 C=(sumY*sumXX-sumX*sumXY)/(X(0)*sumXX-sumX*sumX)

10870 @%=&20204 :VDU4

10880 PRINT TAB(10,0);"Y=";M;"*X+";C;TAB(10,1);"Cor.

coef. = ";XY/SQR(XX*YY)

10890 MOVE FN£CVX(£SX),FN£CVY(M*£SX+C) :DRAW FN£CVX(£LX),

FN£CVY(M*£LX+C)

10900 @%=&10 :ENDPROC


THE PROCEDURE PROCnamex

PROCnamex names the x axis by printing the required wording along it.

The operation of PROCnamex: positioning the name

The wording is positioned as far to the right as possible using the length of the string as a guide as to where to start printing. The procedure uses VDU5 for printing at the graphics cursor to position each character.

10980 DEF PROCnamex(£Hname$)

10990 MOVE FN£CVX(£LX)-32*LEN(£Hname$)-64,FN£CVY(£Y0)-52

:VDU5:PRINT£Hname$:VDU4

11000 ENDPROC


THE PROCEDURE PROCnamey

PROCnamey names the y axis by printing the required wording along it.

The operation of PROCnamey: positioning the name

The procedure does not check whether the name is short enough to fit in the available space and so there may be some odd effects if the name is too long. The procedure uses VDU5 for printing at the graphics cursor to position each character. Hence there is a FOR---NEXT loop set up in line 10930, which steps from 1 to the length of the string. Line 10940 picks off the letters of the name, one by one.

10920 DEF PROCnamey(£Vname$):VDU5

10930 FOR £A=1 TO LEN(£Vname$)

10940 MOVE FN£CVX(£X0)-60,FN£CVY(£LY)-£A*32-32:

PRINTMID$(£Vname$,£A,1)

10950 NEXT £A:VDU4

10960 ENDPROC


THE PROCEDURE PROCchr

PROCchr draws a single character at the point specified. It is called by a line such as:

100 PROCchr(X,Y,S$,AN,SC)

where:

X,Y are the co-ordinates of the bottom left-hand corner of the final character when viewed the normal way up.
S$ is the character to be drawn.
SC is how many times larger than normal the character is to be drawn.
AN is the angle at which the character is to be written. 0 corresponds to writing across the screen, while a positive angle measured in radians corresponds to the character being rotated around anticlockwise.
The first numbers representing the x and y co-ordinates must be in screen co-ordinates. If PROCscale has already been called, you can use x in your own co-ordinates, provided you replace x by FN£CVX(x), and similarly for y.

The operation of PROCchr: copying the pixels

The enlarged writing clearly shows the pixels from which the normal character set is made up. This is because the writing is produced by copying the normal character set, together with scaling and rotation as required.
The definitions for all the printable characters are stored in memory as eight numbers per character. These numbers hold the character definitions in precisely the same form as for the programmable characters (see Chapter 4). The call to &FFF1 in line 9050 instructs the operating system to write a copy of the character definition into memory at position M. M is the address of 8 bytes of memory, reserved at the end of the program by line 9040. The FOR ... NEXT loops between lines 9040 and 9080 examine the character definition and write an enlarged pixel wherever it should be lit.

9000 DEF PROCchr(X,Y,S$,AN,SC)

9010 LOCAL CO1,CO2,SI1,SI2,LX,LY,RX,RY,XX,YY

9020 CO1=COS(AN):SI1=-SIN(AN)

9030 CO2=COS(PI/2-AN):SI2=SIN(AN+PI/2)

9040 £F=£F+1:IF £F=1 THEN DIM M 8 ELSE £F=2

9050 A%=10:X%=M MOD256:Y%=M DIV256:?M=ASC(S$):CALL(&FFF1)

9060 FOR XX=0TO7 :FOR YY=0TO7

9070 IF ?(M+8-YY) AND 2^(7-XX) THEN PROCpixel

9080 NEXT YY,XX

9090 ENDPROC

9100 DEF PROCpixel

9110 LX=XX-.5:RX=XX+.5:LY=YY-.5:RY=YY+.5

9120 MOVE X+SC*(LX*CO1+LY*SI1),Y+SC*(LY*SI2+LX*CO2)

9130 MOVE X+SC*(RX*CO1+LY*SI1),Y+SC*(LY*SI2+RX*CO2)

9140 PLOT 85,X+SC*(LX*CO1+RY*SI1),Y+SC*(RY*SI2+LX*CO2)

9150 PLOT 85,X+SC*(RX*CO1+RY*SI1),Y+SC*(RY*SI2+RX*CO2)

9160 ENDPROC


THE PROCEDURE PROCmessage

PROCmessage writes a message on the screen.
It is called by a line such as:

PROCmessage(X, Y ,S$, AN, SC)

X, Y are the co-ordinates of the bottom left-hand corner of the first character when viewed the normal way up.
S$ is the message to be drawn.
AN is the angle at which the message is to be written, It is slightly unusual in that 0 corresponds to writing straight up the screen, while a positive angle measured in radians corresponds to the message being rotated around anticlockwise.
SC is how many times larger than normal the character is to be drawn.

The operation of PROCmessage: printing the characters

Lines 9220 and 9230 calculate the position at which the character is to be drawn. A call to PROCchr in line 9240 then displays the next character.

9180 DEFPROCmessage(X,Y,S$,AN,SC)

9190 LOCAL I,XP,YP

9200 AN=AN+PI/2

9210 FOR I=1 TO LEN(S$)

9220 XP=SC*(COS(AN)*8*(I-1))

9230 YP=SC*(SIN(AN)*8*(I-1))

9240 PROCchr(X+XP,Y+YP,MID$(S$,I,1),AN,SC)

9250 NEXT I

9260ENDPROC


THE PROCEDURE PROCcurve

PROCcurve prints a message on the screen round the arc of a circle.
It is called by a line such as:

PR0Ccurve(X,Y,R,SA,FA,S$,SC)

where:

X, Y are the co-ordinates of the bottom left-hand corner of the final character when viewed normal way up.
R is the radius of the circle round which the writing is to appear.
SA is the angle round the circle at which writing is to begin. This is slightly unusual in that 0 corresponds to a position straight above the centre of the circle, while a positive angle measured in radians corresponds to the starting position rotating around anticlockwise.
FA is the angle round the circle at which writing is to finish.
S$ is the message to be drawn.
SC is how many times larger than normal the character is to be drawn.

The operation of PROCcurve

Lines 9320 and 9330 calculate the position for the next character to be drawn, while a call to PROCchr in line 9340 draws it on the screen.

9280 DEFPROCcurve(X,Y,R,SA,FA,S$,SC)

9290 LOCAL I,XP,YP

9300 SA=SA+PI/2:FA=FA+PI/2

9310 FOR I=1 TO LEN(S$)

9320 XP=X+R*COS(SA-(SA-FA)*(I-1)/(LEN(S$)+1))

9330 YP=Y+R*SIN(SA-(SA-FA)*(I-1)/(LEN(S$)+1))

9340 PROCchr(XP,YP,MID$(S$,I,1),SA-(SA-FA)*(I-1)/

(LEN(S$)+1)-PI/2,SC)

9350 NEXT I

9360 ENDPROC


THE PROCEDURE PROChisto

PROChisto draws bars of a histogram of height equal to the values held in the YO array when converted to screen co-ordinates. So PROCscale has to be called before PR0Chisto.

The operation of PROChisto: the bar position

The values held in the X() array are taken as the co-ordinates of the right-hand side of the bars. The width of the bars is made equal to the step size along the x axis which is held in the variable £ST, after PROCsca[e has been called. Thus the x co-ordinates to the left and right of the bars and the y co-ordinate of the top and bottom of the bars are worked out in lines 12040 to 12060. Then lines 12100 and 12110 draw up the bars. The colour for each bar is alternately I and 2 as set by fine 12030.

12000 DEF PROChisto

12010 £C=1

12020 FOR £A=1 TO X(0)-1

12030 GCOL0,£C :£C=£C+1 :IF £C>2 THEN £C=1

12040 £XL=FN£CVX(X(£A)-£IN) :£XR=FN£CVX(X(£A))

12050 £YH=FN£CVY(Y(£A))

12060 £YB=FN£CVY(0)

12070 MOVE £XL,£YB :MOVE £XL,£YH

12080 PLOT 85,£XR,£YB :PLOT 85,£XR,£YH

12090 GCOL 0,3

12100 MOVE £XL,£YB :DRAW £XR,£YB

12110 DRAW £XR,£YH :DRAW £XL,£YH :DRAW £XL,£YB

12120 NEXT £A

12130 ENDPROC


THE PROCEDURE PROC3Dhisto

PROC3Dhisto draws up solid-looking bars for a histogram. This is particularly useful when two histograms need to be superimposed, one in front of the other.

The operation of PROC3Dhisto

The principle is very similar to that for PROChisto in the use of the XQ array to hold the right hand co-ordinate of the bars and the YO array to hold the bar heights. The front of the bar is plotted by lines 12200 and 12210, as two triangles to give a solid block of colour; the side is produced by lines 12230 and 12240; and the top is filled in by lines 12260 and 12270. The set of DRA W statements in lines 12290 to 12340 then mark out the edges of the blocks to make the picture more distinct.

12150 DEF PROC3Dhisto(C)

12160 FOR £A=1 TO X(0)-1

12170 £XL=FN£CVX(X(£A)-£IN) :£XR=FN£CVX(X(£A))

12180 £YB=FN£CVY(0) :£YH=FN£CVY(Y(£A))

12190 GCOL 0,C

12200 MOVE £XL,£YB :MOVE £XL,£YH

12210 PLOT 85,£XR,£YB :PLOT 85,£XR,£YH

12220 GCOL 0,3-C

12230 PLOT 85,£XR+64,£YB+32

12240 PLOT 85,£XR+64,£YH+32

12250 MOVE £XR,£YH

12260 PLOT 85,£XL+64,£YH+32

12270 PLOT 85,£XL,£YH

12280 GCOL 0,3

12290 MOVE £XL,£YH:DRAW £XL+64,£YH+32

12300 DRAW £XR+64,£YH+32

12310 DRAW £XR,£YH:DRAW £XL,£YH

12320 DRAW £XL,£YB:DRAW £XR,£YB

12330 DRAW £XR,£YH:DRAW £XR+64,£YH+32

12340 DRAW £XR+64,£YB+32:DRAW£XR,£YB

12350 NEXT £A

12360 ENDPROC


THE PROCEDURE PROCpie

PROCpie draws up a pie chart from data stored in the arrays £SO and £N$(). PROCpie is quite different from the rest and so we chose different array names to help emphasize this point.

The operation of PROCpie

The relative size of each sector of the pie-chart is held in £SO while the total sum of these relative sizes is held in T. The first part of the procedure steps through all the sectors (the FOR..NEXT loop between lines 13030 and 13090). It calls on the sub-procedure PR0Csector (see later) to draw each sector while supplying this sub-procedure with the starting and ending fraction of the whole that the sector should occupy,
Only three colours are used for the pie chart, two being used for alternate sectors with the last sector always being in the third colour. This ensures that even for an even number of sectors, no two sectors have the same colour (see lines 13050 and 13060.)

13000 DEF PROCpie(R%,X%,Y%,T)

13010 LOCAL L%,S,W

13020 W=0:S=0:C%=0

13030 FOR L%=1 TO £NS

13040 W=W+£S(L%)

13050 C%=C%+1:IFC%>2THENC%=1

13060 IFL%=£NS THENC%=3

13070 PROCsector(C%,S/T,W/T,R%,X%,Y%)

13080 S=W

13090 NEXTL%

13100 PROClabel(R%,X%,Y%,T)

13110 ENDPROC

The PROCEDURE PROCsector

PROCsector is a subsidiary part of PROCpie and draws an individual sector.

The operation at PROCsector

PROCsector sets the colour for the sector in line 13150. Then it does an initial MOVE to the outside radius at the start of the sector. Line 13160 calculates this initial position by adding the projection of the radius in first the x and then the y direction to the co-ordinates for the centre. This first MOVE, followed by a second one to the centre of the pie chart in line 13180, is necessary as extensive use is made of the triangular form of the PLOT statement. This occurs in line 13190 which is now within a FOR...NEXT loop which moves the x and y co-ordinates of the PLOT85 statement in line 13190 round the circumference of the pie chart. This results in many calls to the triangular fill form of the PLOT statement and causes the sector to be filled in the chosen colour.

13130 DEF PROCsector(C%,S,F,R%,X%,Y%)

13140 LOCAL L

13150 GCOL0,C%

13160 MOVE COS(2*PI*S)*R%+X%,SIN(2*PI*S)*R%+Y%

13170 FOR L= 2*PI*S TO 2*PI*F STEP 0.1

13180 MOVE X%,Y%

13190 PLOT 85,COS(L)*R%+X%,SIN(L)*R%+Y%

13200 NEXTL

13210 PLOT85,COS(2*PI*F)*R%+X%,SIN(2*PI*F)*R%+Y%

13220 ENDPROC


THE PROCEDURE PROClabel

PROClabel is solely for labelling pie-charts. It is for this reason that we have chosen it to use the same variation of arrays £S() and £N$() as for PROCpie.

The operation of PROClabel

PROClabel positions each label to start or end at a position just outside the pie chart and off the centre of the particular sector referred to. This means that the main calculation, which occurs in line 13290, has to calculate the projection of a length slightly longer than the radius of the pie-chart. It then adds this to the co-ordinates of the centre of the pie-chart to find the position for the label. The radial position chosen for the label corresponds to the centre of that particular sector, as determined in line 13270. Here B represents the fraction of the pie-chart occupied by previous sectors, and is updated in line 13310 after each label has been printed.
Since each label is printed by a normal PRINT statement, it is necessary to make sure that for labels on the left of the pie-chart that the printing starts at a position displaced to the left according to the length of the label. This offset leftwards is calculated in line 13280.

13240 DEF PROClabel(R%,X%,Y%,T)

13250 VDU5:B=0:R%=R%+64

13260 FOR A=1 TO £NS

13270 OX=0:H=(B+£S(A)/2)*2*PI/T

13280 IFH<PI*1.5 ANDH>PI*.5 THENOX=-(LEN(£N$(A))*32)

13290 MOVE COS(H)*R%+X%+OX,SIN(H)*R%+Y%

13300 PRINT£N$(A)

13310 B=B+£S(A)

13320 NEXTA

13330 VDU4

13340 ENDPROC


THE TELETEXT PROCEDURE PROCplot

Teletext graphics is so much more crude than the other graphics modes that we decided to plot individual points on the screen as single dots rather than as the + signs used in the ordinary graphics procedures. This point plotting is achieved by a PROCplot which is unique to Teletext. It is in place of PROCpoint for the normal graphics modes. There is still the equivalent of PROCpoint for drawing a small + sign as this was required when graduating the axes (see later). If you should wish to plot a graph with a + sign to represent each point then use PROCpoint in line 10060.

11200 DEFPROCplot(X,Y)

11210 IF X>73 OR X<0 THEN ENDPROC

11220 IF Y>74 OR Y<0 THEN ENDPROC

11230 LOCAL A%,C%

11240 PRINT TAB(X DIV 2,24-Y DIV 3);

11250 C%=(X AND1)+(Y MOD3)*2

11260 C%=VAL(MID$("166404080102",C%*2+1,2))

11270 A%=135

11280 VDU (USR &FFF4 AND &FF00) DIV 256 OR C% OR 128

11290 ENDPROC

THE TELETEXT PROCEDURE PROCdraw

This procedure is unique to Teletext, and is required to do the equivalent of the MOVE and DRAW statements of ordinary graphics. It relies on Pythagoras's theorem to work a way between the two points whose co-ordinates are given in the procedure call.

11310 DEFPROCdraw(X1,Y1,X2,Y2)

11320 PROCplot(X1,Y1) :PROCplot(X2,Y2)

11330 LOCAL X,Y,L,A

11340 X=(X2-X1) :Y=(Y2-Y1)

11350 L=SQR((X1-X2)^2+(Y1-Y2)^2)

11360 FOR A=1 TO L

11370 IF L<>0 THEN PROCplot(X1+A*X/L,Y1+A*Y/L)

11380 NEXT A

11390 ENDPROC


THE TELETEXT PROCEDURE PROCclg

This procedure is unique to Teletext. Block graphics may only be displayed after certain control codes to the left of any line. This procedure writes these control codes all the way down the left-hand side of screen.

11050 DEFPROCclg(C,G)

11060 VDU 28,0,24,39,0,23,0,10,32;0;0;0;

11070 LOCAL CH,GH,Y

11080 CH=128+C : CLS

11090 IF C<1 OR C>7 THEN CH=132

11100 GH=144+G

11110 IF G<1 OR G>7 THEN GH=156

11120 FOR Y=0 TO 24

11130 PRINT TAB(0,Y);

11140 VDU CH,157,GH

11150 NEXT Y

11160 VDU 28,3,24,39,0

11170 PRINT TAB(0,0);

11180 ENDPROC


THE TELETEXT PROCEDURE PROCscale

The Teletext version of PROCscale is very similar to the ordinary version. The differences are in lines 10240 and 10250 which define the conversion function from numbers supplied by the user's program to screen co-ordinates.


THE TELETEXT PROCEDURE PROCgraph

The Teletext version of this procedure is identical to the ordinary version, except that a call to PROCplot in line 1060 replaces the call to PROCpoint.

10000 DEF PROCgraph

10010 PROCscale

10020 PROCaxes

10030 PROCgraduate

10040 PROCnumber

10050 FOR £J=1 TO X(0)

10060 PROCplot(FN£CVX(X(£J)),FN£CVY(Y(£J)))

10070 NEXT £J

10080 ENDPROC

THE TELETEXT PROCEDURE PROCaxes

The Teletext version of PRDCaxes is different from the ordinary version because it involves writing on the screen. In place of the MOVE and DRAW statements of the normal graphics procedures, it calls upon a specially written PROCdraw procedure in lines 10460 and 10470.

10420 DEF PROCaxes

10430 £X0=0 :£Y0=0

10440 IF £SX>0 THEN £X0=£SX ELSE IF £LX<0 THEN £X0=£LX

10450 IF £SY>0 THEN £Y0=£SY ELSE IF £LY<0 THEN £Y0=£LY

10460 PROCdraw(FN£CVX(£SX),FN£CVY(£Y0),FN£CVX(£LX),FN£CVY(£Y0))

10470 PROCdraw(FN£CVX(£X0),FN£CVY(£SY),FN£CVX(£X0),FN£CVY(£LY))

10480 ENDPROC


THE TELETEXT PROCEDURE PROCgraduate

The Teletext version of PROCgraduate is identical to the ordinary version. The PROCpoint which it calls upon in line 10530 and 10560 must, however, be the special Teletext one.

10500 DEF PROCgraduate

...

...

10560 £CBY%=£CBY%+1 :PROCpoint(£X0,y) :NEXT y


THE TELETEXT PROCEDURE PROCnumber

Since PROCnumber writes on the Teletext screen, it is completely different from the ordinary version. The Teletext screen is set up with the assumption that the whole screen will contain only graphics. To write text at a particular position, PROCplot (in line 10600 and elsewhere) positions where to write. This procedure also writes a single graphics dot on the screen which is erased by printing code 127 to delete the last character written. Code 134, in Teletext, switches on cyan coloured writing. Next comes whatever number is required for the axes, followed by code 147 to turn the graphics back on.
Each code to turn from graphics to normal writing or from normal writing to graphics occupies one character position on the screen. As a result it is impossible to write long numbers against the y axis without sometimes overwriting part of the axis itself.
This is yet another manifestation of the insuperable problems of the poor resolution which is available with Teletext.

10590 DEF PROCnumber

10600 PROCplot(FN£CVX(£LX)-LEN(STR$(£LX)),FN£CVY(£Y0)-3)

:PRINTCHR$127;CHR$134;£LX;CHR$147;

10610 PROCplot(FN£CVX(£X0)-7,FN£CVY(£LY)+4):

PRINTCHR$127;CHR$134;£LY;CHR$147;

10620 IF £LX>0AND£SX<0AND£LY>0AND£SY<0 THEN ENDPROC

10630 PROCplot(FN£CVX(£SX)-7,FN£CVY(£Y0)-3):

PRINTCHR$127;CHR$134;£SX;CHR$147;

10640 PROCplot(FN£CVX(£X0)-7,FN£CVY(£SY)):

PRINTCHR$127;CHR$134;£SY;CHR$147;

10650 ENDPROC


THE TELETEXT PROCEDURE PROCpoint

The Teletext version of PROCpoint is different from the ordinary Version. It makes considerable use of the Teletext procedure PROCdraw (see later), referred to in lines 10680 and 10690, which draws a line between two points.

11310 DEFPROCdraw(X1,Y1,X2,Y2)

11320 PROCplot(X1,Y1) :PROCplot(X2,Y2)

11330 LOCAL X,Y,L,A

11340 X=(X2-X1) :Y=(Y2-Y1)

11350 L=SQR((X1-X2)^2+(Y1-Y2)^2)

11360 FOR A=1 TO L

11370 IF L<>0 THEN PROCplot(X1+A*X/L,Y1+A*Y/L)

11380 NEXT A

11390 ENDPROC


THE TELETEXT PROCEDURE PROCbstln

The Teletext version of PROCbstln is identical to the ordinary version except for the line 10910 which prints on the screen and line 10920 which calls on PROCdraw to draw the best straight line.

10910 PRINT TAB(9,0);CHR$135;"Y=";M;"*X+";C;CHR$147;TAB(

9,1);CHR$135;"Cor. coef. = ";XY/SQR(XX*YY);CHR$147

10920 PROCdraw(FN£CVX(£SX),FN£CVY(M*£SX+C),FN£CVX(£LX),

FN£CVY(M*£LX+C))


THE TELETEXT PROCEDURE PROCnamey

The Teletext version of PROCnamey is different from the ordinary version. It writes the name of the y axis one character at a time in a vertical column. PR0Cplot positions the writing. This also writes a single graphics point, which is rubbed out by printing character code 127, followed by code 135 , for white writing, followed in turn by the single letter required. The graphics is then turned back again for the rest of the line with code 147 for yellow graphics.

10950 DEF PROCnamey(£Vname$)

10960 FOR £A=1 TO LEN(£Vname$)

10970 PROCplot(FN£CVX(£X0)-8,FN£CVY(£LY)-£A*3+3):PRINT

CHR$127;CHR$135;MID$(£Vname$,£A,1);CHR$147;

10980 NEXT £A

10990 ENDPROC


THE TELETEXT PROCEDURE PROCnamex

The Teletext version of PROCnamex is different from the ordinary version. It is also simpler in that the whole name is written on one line. This means that the process can be achieved in the one line 11210 by first calling on PROCplot to get the position to start printing. The graphics spot so produced is then rubbed out using code 127, followed by code 135 for white writing. The name is then merely printed next using a PRINT statement. This is followed by code 147 to turn yellow graphics back on again. The position at which to begin the writing is calculated by subtracting the length of the string to be written from the co-ordinates of the end of the axis. Before doing this subtraction in line 11020, the length of the string is scaled to allow for the two graphics spots which occur in the length occupied by one letter.

11010 DEF PROCnamex(£Hname$)

11020 PROCplot(FN£CVX(£LX)-2*LEN(£Hname$)-3,

FN£CVY(£Y0)-6):VDU127,135:PRINT£Hname$;CHR$147;

11030 ENDPROC



Next     Top