Programming by Example

 

A BB4W Compendium

freeman69@gmx.com

IDIC BBC_Owl2 M&P

Simple LOGO

LOGO is a computer language used for drawing patterns. The following BB4W program demonstrates the basic principles of LOGO. Example pattern programs are included below. The user must enter a column of instructions, using a text editor, and save the file as a text '.txt' file. Simple LOGO reads the text file specified and executes the commands listed.

 

BB4W is an interpreted language. This means that every line of a BB4W program is read and acted upon in real time. Most high level language programs need to be compiled into machine code before they can be run, making them very fast.

Simple LOGO interprets a list of instructions in real time, similar to the way that BB4W interprets a BB4W program.

 

  10 MODE 9

  20 ORIGIN 640,512

  30 DIM loops(99,1)

  40 loopfree=0:loopcurrent=-1

  50

  60 PRINT" Type in the name of the file to"

  70 PRINT" be drawn and press ENTER"

  80 INPUT" >"F$

  90 X=OPENIN(F$+".txt")

 100 IF X=0 PRINT"File not found":END

 110 CLS:OFF

 120

 130 tx=0:ty=0:ta=0:pendown=TRUE:speed=20

 140 GCOL 0,1:CIRCLEFILL 0,0,8:GCOL 0,7

 150

 160 REPEAT

 170   INPUT#X,L$

 180   IF ASC(LEFT$(L$,1))=10 L$=RIGHT$(L$,LEN(L$)-1)

 190   V=VAL(RIGHT$(L$,LEN(L$)-1))

 200   c$=CHR$(ASC(LEFT$(L$,1))AND223)

 210   CASE c$ OF

 220     WHEN "H" : tx=0:ty=0

 230     WHEN "P" : pendown=NOTpendown

 240     WHEN "C" : GCOL 0,V

 250     WHEN "L" : ta-=V:IF ta<0 ta+=360

 260     WHEN "R" : ta+=V:IF ta>=360 ta-=360

 270     WHEN "F" :

 280       dx=SIN(RAD(ta)):dy=COS(RAD(ta))

 290       FOR a=1 TO V

 300         tx+=dx:ty+=dy

 310         IF pendown THEN PLOT tx,ty

 320         speed-=1:IF speed=0 speed=20:WAIT 1

 330       NEXT

 340     WHEN "G" :

 350       loops(loopfree,0)=PTR#X

 360       loops(loopfree,1)=V

 370       loopfree+=1

 380       loopcurrent+=1

 390     WHEN "E" :

 400       IF loopcurrent>-1 THEN

 410         loops(loopcurrent,1)-=1

 420         IF loops(loopcurrent,1)=0 THEN

 430           loopfree-=1

 440           loopcurrent-=1

 450         ELSE

 460           PTR#X=loops(loopcurrent,0)

 470         ENDIF

 480       ENDIF

 490     OTHERWISE : IF L$<>"" COLOUR 1:PRINTL$:COLOUR 7

 500   ENDCASE

 510 UNTIL EOF#X

 520 CLOSE#X

 530 PRINT"End"

 540 END

Tunnel

Simple LOGO: Code explained...

 

  10 MODE 9

  20 ORIGIN 640,512

  30 DIM loops(99,1)

  40 loopfree=0:loopcurrent=-1

 

The graphics origin is set to the centre of the window. Any drawing will begin at this location.

'loops' is an array, acting as a 'stack' to store information about loops encountered in a LOGO program.

'loopfree' is a pointer to the next available row in 'loops'

'loopcurrent' is also a pointer to the 'loops' array, indicating the loop currently being executed.

All of the above loop information is to allow Simple LOGO to execute nested loops within a LOGO program.

 

  60 PRINT" Type in the name of the file to"

  70 PRINT" be drawn and press ENTER"

  80 INPUT" >"F$

 

The INPUT command allows the user to enter a string of characters. In this instance, we need the user to enter the name of the LOGO text file to be executed (run).

 

  90 X=OPENIN(F$+".txt")

 100 IF X=0 PRINT"File not found":END

 110 CLS:OFF

 

Line 90 attempts to open a LOGO text file using the name provided. The program exits if the file cannot be found. BB4W is expecting to find the named file in the same folder as the saved version of the Simple LOGO program.

 

 130 tx=0:ty=0:ta=0:pendown=TRUE:speed=20

 140 GCOL 0,1:CIRCLEFILL 0,0,8:GCOL 0,7

 

'tx', 'ty' and 'ta' hold the current co-ordinates of the LOGO 'pen'.

'pendown' indicates whether the pen is drawing or moving to a new location.

The 'speed' variable is used to slow the drawing process to allow the user to see how the patterns are created.

Line 140 draws a circle at the graphics origin to provide a visual reference.

 

 160 REPEAT

 170   INPUT#X,L$

 

The main loop of the program starts here.

Line 170 reads the next line of text from the opened file, placing the line into 'L$'.

 

 180   IF ASC(LEFT$(L$,1))=10 L$=RIGHT$(L$,LEN(L$)-1)

 

Notepad saves text with two hidden control characters at the end of each line: carriage return and line feed (ASCII codes 13 and 10). BB4W only expects to see carriage return, so we must strip out the superfluous line feed characters.

RIGHT$(L$,LEN(L$)-1) returns all but the first character of L$.

 

 190   V=VAL(RIGHT$(L$,LEN(L$)-1))

 200   c$=CHR$(ASC(LEFT$(L$,1))AND223)

 

Our Simple LOGO commands are a combination of a single character instruction followed by a numeric value. Line 190 places the numeric value into variable 'V'. Line 200 places the instruction character into variable 'c$'. By ANDing the ASCII value of the instruction character with 223, we convert any lower case characters to upper case.

 

 210   CASE c$ OF

 220     WHEN "H" : tx=0:ty=0

 230     WHEN "P" : pendown=NOTpendown

 240     WHEN "C" : GCOL 0,V

 250     WHEN "L" : ta-=V:IF ta<0 ta+=360

 260     WHEN "R" : ta+=V:IF ta>=360 ta-=360

 

Our set of Simple LOGO instructions are limited:

H returns the pen to the origin without drawing.

P toggles the pen up and down.

C allows the user to select a colour.

L turns the direction of the pen left by the number of degrees specified.

R turns the direction of the pen right by the number of degrees specified.

 

 270     WHEN "F" :

 280       dx=SIN(RAD(ta)):dy=COS(RAD(ta))

 290       FOR a=1 TO V

 300         tx+=dx:ty+=dy

 310         IF pendown THEN PLOT tx,ty

 320         speed-=1:IF speed=0 speed=20:WAIT 1

 330       NEXT

 

F moves the pen forward by the number of units specified; either drawing or not, depending on the status of the pen. The direction of movement is derived from the current direction (angle) of the pen.

This is the first instance of SIN, COS and RAD within these program listings. The next program provides more information on these useful commands.

The direction (angle) of the pen is stored in units of degrees. Degrees are convenient for humans to use, but computers tend to process angles in radians. The actual difference is minimal and the conversion process is very simple. RAD converts degrees to radians and DEG converts radians to degrees.

To move in any direction we need to know how many pixels to move by: left or right and up or down. In this instance, SIN calculates the number of pixels to move left or right (returning either a negative or positive value) and COS calculates the number of pixels to move up or down.

 

 340     WHEN "G" :

 350       loops(loopfree,0)=PTR#X

 360       loops(loopfree,1)=V

 370       loopfree+=1

 380       loopcurrent+=1

 

G indicates the start of a new loop. Loops can be 'nested' i.e. a new and totally different loop can begin before we reach the 'end' of the previous loop; loops within loops.

Line 350 makes a record of where we are in the text file, just like using a bookmark. Whenever we encounter the relevant 'end' of a loop, we decrement the count and return to the bookmarked place in the file marking the start of the loop, but only if another loop is required.

 

 390     WHEN "E" :

 400       IF loopcurrent>-1 THEN

 410         loops(loopcurrent,1)-=1

 420         IF loops(loopcurrent,1)=0 THEN

 430           loopfree-=1

 440           loopcurrent-=1

 450         ELSE

 460           PTR#X=loops(loopcurrent,0)

 470         ENDIF

 480       ENDIF

 

E indicates the end of a loop.

Line 410 decrements the current loop count. If no more loops are required then 'loopfree' and 'loopcurrent' are decremented, ready to encounter the next 'end' of a different loop. This process  depends upon the user specifying loops correctly i.e. for every 'G' instruction there needs to be one and only one 'E' instruction.

 

 490     OTHERWISE : COLOUR 1:PRINTL$:COLOUR 7

 

If the instruction character isn't recognised then the line is printed on the screen in red to indicate an error in the code.

 

 500   ENDCASE

 510 UNTIL EOF#X

 520 CLOSE#X

 530 PRINT"End"

 540 END

 

The file is closed and our program ends when the last instruction (or the last loop) has been executed.

EOF#X indicates when the end of the file has been reached.

Always CLOSE files when finished.

The following are example Simple LOGO programs. Each column is a separate program. Use a text editor to enter and save the instructions as a '.txt' file. The name of the program should not be part of the text, but used as the name of the file.

Make sure the last line of text is entered with a carriage return at the end i.e. press ENTER at least once after the final instruction, otherwise Simple LOGO may not see the last instruction.

 

 

Simple LOGO programs:

Tunnel

 

P

F440

R159

P

G60

F822

R138

E

Magic

 

P

F400

R162

P

G5

F761

R144

E

L72

G360

F7

R1

E

Hexabloom

 

C3

G6

P

F200

P

G6

G6

G6

F15

L60

E

F50

R60

E

F100

R60

E

H

R60

E

Arrow black large Arrow black large