Bottom     Previous     Contents

2 Functions and Symmetry

BBC BASIC provides a large number of built-in functions which can greatly simplify many programs. The diversity and power of the functions available often make it possible to produce the same effect in many different ways. This allows the user to adopt the clearest and most natural construction to express his ideas, without being unduly inhibited by the limitations of the language. An analogy can be found in natural languages, which include a vast spectrum of expressive words making it possible to communicate ideas in many subtle and colourful ways.

Three ways of drawing a circle

The next three programs all draw a circle. They demonstrate that even a simple circle offers a wealth of opportunities for selecting the particular technique you feel happiest with.

1 Iterative method

This method can produce a good approximation to a circle very rapidly . I t does not call any trigonometrical functions, which tend to be rather slow. The disadvantages of this technique are that it can produce some unpredictable results, and that it is not immediately obvious how it works:

CIRCLE1

0 REM Iterative method

20 MODE 1

30 S%=20

40 VDU29,640;512;

50 X=500

60 Y=90

70 MOVEX,Y

80 REPEAT

90 DRAWX,Y

100 X=X+Y/S%

110 Y=Y-X/S%

120 UNTIL POINT(X,Y)<>0

130 END

Description of program

30	Determine the size of the steps around the circle.
50-70	Define (X,Y) as the starting point.
100-110	Calculate next point.
120	Carry on until the line bumps into itself.

2 Polar coordinate method

This method is based on the polar coordinate equation for a circle. A polar coordinate consists of an angle and a length. In this system a circle is described by specifying a fixed length and an angle. To draw a circle each of the polar points must be converted into an (x,y) coordinate. The polar point with angle A and length S Is the same as the point S*COS(A),S*SIN(A).

The method is very simple to program; the size of the circle is easily altered, and it is very clear what is going on. This method can easily be adapted to draw ellipses and forms the basis for many interesting patterns like lissajoux figures.

CIRCLE2

0 REM Polar method

20 MODE1

30 VDU29,640;512;

40 S%=400

50 MOVE0,S%

60 FORA=0 TO 2*PI STEP PI/30

70 DRAWS%*SIN(A),S%*COS(A)

80 NEXT

90 END

Description of program

40	Make the radius of the circle S%.
60	Take angle A (measured in radians), and step around the circle.
70	Draw a line to the next point on the circle.

3 Quadratic solution method

This method is based on the fact that the equation for a circle, centred about the origin, is X*X+Y*Y=S*S, where S is the radius of the circle.

By rearranging the equation we get Y=SQR(S*S-x*x), and using this we can substitute many values for X and obtain the corresponding Y-coordinate on the edge of the circle.

CIRCLE3

0 REM Quadratic method

20 MODE1

30 VDU29,640;512;

40 S%=400

50 FOR T%=0 TO 1

60 MOVE -S%,0

70 FOR X%=-S% TO S% STEP 8

80 Y%=SQR(S%*S%-X%*X%)

90 IF T%=1 THEN Y%=-Y%

100 DRAWX%,Y%

110 NEXT X%,T%

120 END

Description of program

50	Determine which half of the circle is being drawn.
70	Generate X-coordinates.
80	Calculate Y-coordinates.
90	Pick either positive or negative. square-root depending on T%.
100	Draw the next part of the circle.

Lissajoux figures

Lissajoux figures are fascinating patterns that can form the basis for many weird and wonderful programs. The method for drawing lissajoux figures is similar to the polar-coordinate method for drawing a circle. For the circle , the angle from which the x- and y-coordinates are derived is the same. Different lissajoux figures are obtained when these angles are out of phase. The following program draws a different lissajoux figure each time the Space Bar is pressed.

LISS1

0 REM Lissajoux figures

20 MODE1

30 VDU23;10,32;0;0;0;

40 VDU29,640;512;

50 FORF=0 TO 4 STEP 0.2

60 MOVE 0,400

70 A=0

80 REPEAT

90 A=A+0.1

100 DRAW 400*SIN(A),400*COS(A*F)

110 UNTIL INKEY(0)=32

120 CLS

130 NEXT

140 END

Description of program

30	Turn off the cursor.
40	Make (0,0) the centre of the screen.
50	Determine step size.
60-90	Start at the top left-hand corner of the screen, and increment A by 0.1 each time round the loop.
100	Draw the pattern.
110	Wait for the Space Bar to be pressed.
130	Start another pattern.

Lissajoux pattern

There are many ways in which the basic lissajoux patterns can be enhanced. Here is a program that uses straight lines to join the points that trace out two intermeshing figures. The pattern obtained depends on the random numbers chosen at lines 50 and 60. If you press ESCAPE at any stage a new pattern will begin.

LISS2

0 REM Lissajoux pattern

20 ON ERROR GOTO 30

30 MODE1

40 VDU5

50 B%=RND(5)

60 C%=RND(5)

70 VDU29,640;512;

80 GCOL0,RND(3)

90 FORA=0TO 1000 STEP PI/30

100 X%=250*COS(A)

110 MOVE X%,Y%

120 DRAW500*COS(A/B%),500*SIN(A/C%)

130 NEXT

Description of program

20	Jump to line 30 when ESCAPE is pressed.
50-60	B% and C% effect the shape of the outer figure.
100-110	Move the point back and forth along the line x=y
120	Draw a line to the point that runs around the lissajoux figure.

Ball of wool

Here is an example of the kind of pattern that can be created starting from the basic idea of lissajoux figures. A wealth of similar patterns can be explored simply by piling up different combinations of SIN and COS functions and seeing what comes out.

WOOLBAL

0 REM Ball of wool

20 MODE1

30 VDU5

40 GCOL0,RND(3)

50 S%=400

60 VDU29,640;512;

70 MOVE0,0

80 FORA=0 TO 125.7 STEP 0.1

90 DRAW S%*SIN(A),S%*COS(A)*SIN(A*0.95)

100 NEXT

110 REPEAT UNTIL FALSE

Description of program

50	S% is the radius of the ball.
80	The value 125.7 is about the angle at which the pattern starts repeating itself. 
90	Believe it or not this will trace out a ball.
110	Loop forever on this line once the pattern has been drawn.

Symmetry

By exploiting the symmetry of a pattern it is often possible to simplify and speed up the program used to generate it. In graphic applications much calculation can be avoided by using reflections in the axis to generate the symmetric parts of the object being drawn.

Apart from straightforward reflection like this, there is also the invaluable option on the BBC Microcomputer of moving the origin from one place to another during the program, and this used in con junction with reflection can produce some even more impressive designs.

Persian Carpet

This program produces a pattern similar to that of a Persian carpet. To achieve this effect the program draws a series of radial lines through the centre of a square. The symmetric nature of the carpet is exploited in line 90 where the x-- and y-coordinates of the previous line are interchanged.

The GCOL action Exclusive-OR causes lines that overlap either to cancel out or to produce a new colour.

Interference patterns of this kind are known as 'Moiré patterns'. The carpet is produced by superimposing many of these patterns, each of a different size.

CARPET

0 REM Carpet

20 MODE1

30 VDU5

40 VDU29,640;512;

50 FORS%=20TO500STEP40

60 GCOL3,RND(3)

70 VDU19,RND(3),RND(8)-1;0;

80 FORX%=-S%TOS%STEP 8

90 MOVE -S%,X%:DRAW S%,-X%

100 MOVE X%,-S%:DRAW -X%,S%

110 NEXT X%,S%

120 GOTO50

Description of program

20	4-colour mode.
30	Remove text cursor.
40	Put the origin in the centre of the screen.
50	S% is the size of the pattern being drawn. A new layer of pattern is drawn each time round this loop.
60	Select drawing action EOR and one of the three colours at random.
70	Change the palette so that any of the non-flashing colours can appear.
80-110	Draw the pattern.
120	Start again.

Possible changes

This program offers many opportunities for exploring variations on the original. Some interesting alterations are suggested below.

  1. Try running the program in MODE 0 or MODE 2.
  2. The step size at line 50 can be changed.
  3. The step size at line 80 has a critical effect on the type of pattern produced. Interesting effects can be produced with smaller, larger or random step sizes here.
  4. By moving the origin for each pattern you could fill the screen with lots of small carpets to produce a patchwork quilt.

Multicoloured pattern

This program draws a pattern that is constructed from a series of diverging lines running down the screen. The program produces a fascinating combination of colours, and shows how the appearance of a colour can be affected by the colours next to it.

PATTERN

0 REM Multicoloured pattern

20 MODE1

30 VDU5

40 VDU29,640;0;

50 FORP%=0 TO 640

60 GCOL0,P%AND3

70 MOVEP%*4,0:DRAWP%,1024

80 MOVE-P%*4,0:DRAW-P%,1024

90 NEXT

100 REPEAT

110 VDU19,RND(3),RND(7);0;

120 A=INKEY(60)

130 UNTIL FALSE

Description of program

60	Select a logical colour between 0 and 3.
70-80	Draw symmetric lines to the left and right
of the y-axis.
100	Loop around changing the palette forever.
120	Wait for 0.6 of a second.

Flag

This program uses a procedure which will draw rectangular Union Jacks. The procedure allows the flag to be any size or shape of rectangle, and it be can positioned anywhere on the screen. The drawing method exploits many of the symmetrical elements in the flag. (Actually the 'real' flag has some subtle asymmetric bands but this procedure should produce a flag that satisfies all but the most pedantic readers.)

FLAG

0 REM Flag

20 MODE1

30 VDU23;10,32;0;0;0;

40 PROCJACK(640,512,600,500)

50 REPEAT UNTIL FALSE

Description of program

30	Turn off the blinking text cursor.
40	Draw a flag centred about the point (640,512) with sides of length 1200 and 1000.
50	Do nothing forever.

PROCJACK

This procedure draws a flag centred about (X%,Y%) with sides of length 2*SX% and 2*8*1%.

60 DEFPROCJACK(X%,Y%,SX%,SY%)

70 VDU24,X%-SX%;Y%-SY%;X%+SX%;Y%+SY%;

80 VDU29,X%;Y%;

90 VDU19,2,4;0;

100 GCOL0,130

110 CLG

120 GCOL0,2

130 FORJ%=5 TO 8 STEP 3

140 Y1%=SY%+SY%/J%:Y2%=SY%-SY%/J%

150 IF J%=5 THEN GCOL0,3ELSE GCOL0,1

160 FORI%=0 TO 1

170 MOVE-SX%,-Y1%:MOVE-SX%,-Y2%:PLOT85,SX%,Y2%

180 MOVESX%,Y1%:PLOT85,-SX%,-Y2%

190 Y1%=-Y1%:Y2%=-Y2%

200 NEXT I%,J%

210 FORJ%=3 TO 5 STEP 2

220 IF J%=3 THEN GCOL0,131 ELSE GCOL0,129

230 VDU24,-SX%/J%;-SY%;SX%/J%;SY%;

240 CLG

250 VDU24,-SX%;-SY%/J%;SX%;SY%/J%;

260 CLG

270 NEXT

280 VDU26

290 ENDPROC

Description of PROCJACK

70	Define a graphics window around the whole flag.
80	Put the origin at the centre of the flag.
90	ake logical colour 2 appear as dark blue.
100	Select the graphics background colour to be logical colour 2.
120	Fill the graphics window with the selected graphics background colour. This method of filling a rectangle will not work if the rectangle is partially off the screen.
130-200	Draw the diagonal bands.
210-270	Draw the horizontal bands.
280	Restore the default text and graphics windows.

Sketch Pad

This program allows you to draw lines on the screen, using the keyboard to control the pattern produced.

The cursor starts in the middle of the screen and leaves a trail behind it as you move it about the screen. The control keys are shown below:

0 REM Sketch Pad

20 MODE1

30 VDU5

40 MOVE 640,512

50 REPEAT

60 PLOT1, 4*(INKEY(-98)-INKEY(-67)),

4*(INKEY(-105)-INKEY (-73))

70 UNTIL FALSE

Description of program

30	Remove text cursor.
60	INKEY(--98) returns -1 if the 'Z' key is depressed; otherwise it returns zero. The other INKEY functions on this line perform similar functions for the keys 'X', '/' and ':' .PLOT 1,x,y draws a line relative to the current cursor positon.

Next     Top