```LABTITLE=: 'TGSj: turtle graphics system'
LABAUTHOR=: 'Brian Schott'
The file tgsj.ijs must be in the directory '~addons'.
I received awesome help in developing this system from Fraser Jackson.
David Benn developed a similar system circa 1996.
This system highlights contolling multiple turtles in interactive mode.
A turtle state-change history dataset is generated using sparse arrays in SC.
State0 contains the initial turtle states.
)
LABFOCUS=: 1

NB. =========================================================
Lab Chapter Basic turtle movement
NB. =========================================================
Lab Section TGSj introduction
There are two Labs introducing aspects of Turtle Geometry in
J. They use two implementations which were designed to explore
different aspects of Turtle Geometry. One focuses on commands controlling multiple turtles using vector arguments, which is a strength of J, and the other on using paths as objects and matching the Logo Turtle syntax in J. Both are envisaged as first steps towards richer implementations for teaching purposes.

This Lab relates to the first approach above.

This turtle graphics system in J is called TGSj. Only part of a Logo-like syntax is implemented.
)

NB. =========================================================
Lab Section TGSj windows
There are two windows in this system:
an .ijx window where you see this lab and type commands
a graphics window showing the turtle(s) created

If you cannot see BOTH windows, then use the Windows|Forms...  menu to find BOTH and reposition them so that both are visible at all times. It is especially useful to resize the graphics window to be a little larger than its default size.

There is a red turtle facing up and a blue one facing east.
)
PREPARE
PREPARE

NB. =========================================================
Lab Section Turtle movement and control
The basic turtle movement verbs are provided: forward, back, right, and left (abbreviated as fd, bk, rt, and left, respectively). Pen control verbs are provided: penup (pu), pendown (pd), pencolor.

The first examples below show verbs with scalar, not vector, arguments, but produce results for the multiple globally initialized turtles.

Enter the commands recommended below now.

right 0
forward 0
lt 0
bk 0
pu ''
pendown ''
)

NB. =========================================================
Lab Section
Each of the commands print out a text result in the .ijx window. The text result gives the current turtles' states corresponding to the potentially altered feature of the turtle movement or control you selected.

In all the cases above, no change really occurred because of the particular arguments supplied to the verbs.

You can deduce from the current state results printed above that there are two turtles, the turtles are facing direction 0 and 90 degrees, and the center of the graphics screen is x-y postion 0 0. The turtles each have their pens down.
)
PREPARE
pendown ''
PREPARE

NB. =========================================================
Lab Section
Try the following non-trivial turtle movements now.

Notice that both turtles respond although only one argument is supplied to each command.

Each turtle step is currently 5 units so fd 20 produces position changes of 5*20=100 .

fd 20
rt 90
Position
)

NB. =========================================================
Lab Section Screen clearing
The turtles can be sent home and the screen cleared with the following commands.

home ''
cs ''     NB. clearscreen ''
rt 0
)

NB. =========================================================
Lab Chapter Programming repetition
NB. =========================================================
Lab Section repeats
"repeats" is a J conjunction that enables repetition somewhat like Logo's "repeat". The conjunction "repeats" takes two arguments itself, and the derived verb which is created can have either a monadic or dyadic form.

For example, consider the following command which repeats the monadic verb forward 4 times and each time the amount of forward movement is 10 steps (really 10*Stepsize, or 50). Notice when you type in this command, that the parentheses are not required in this monadic case.
(4 repeats forward) 10

On most computers the graphing will occur so swiftly that you will not see the repetition, and the results will appear like simply fd 40 .

4 repeats forward 10
)

NB. =========================================================
Lab Section
The J verb "fdrt" is dyadic.

NB. verb fdrt is already typed in
NB. a verb that combines J and turtle commands
fdrt =: forwardright =: dyad define
fd x
rt y
)

Because "fdrt" is dyadic, when it is used with "repeats", parentheses are required to inform the derived verb of the conjunction of its arguments.

Experiment by changing the number 20 to other numbers (negatives?).

cs ''  [ home ''
20 forwardright 90             NB. draws a corner and a side
cs ''  [ home ''
20 (4 repeats forwardright) 90 NB. draws a whole square
)

NB. =========================================================
Lab Section
Alone, accepting only one verb argument, "repeats" is rather limited. The following example, while producing an error-free turtle movement, repeats only the forward movement, not the turns. Furthermore, placing parentheses around the phrase "forward 10 [ right 90" does not produce a viable command either (because the conjunction has implicit parentheses only around "4 repeats forward").

4 repeats forward 10 [ right 90 [ clearscreen ''

Try it yourself.
)

NB. =========================================================
Lab Chapter Programming one-liners with "repeats"
NB. =========================================================
Lab Section "then" and "do"
"then" is a dyadic verb that takes text arguments and puts them into a column. It may be simpler to use "then" than to use the more primitive J verbs laminate (,:) and append (,).

"do" is the primitive ". .

Experiment with the following commands.

'a' , 'b' ,: 'c'
'a' then 'b' then 'c'
'fd 30' then 'rt 90'
do 'fd 30' then 'rt 90'
)

NB. =========================================================
Lab Section
"repeat" is a verb, not a conjunction, and is a synthesis of the ideas of David Benn, Brian Schott and Fraser Jackson, created by Fraser. "repeat" is monadic, but requires a 2-piece single argument. The first piece is the number or repetitions; the second piece is a text list describing the repeated turtle movements.

In this example the two boxed inputs are explicitly conjoined using J's primitive link (;). You will see that a square is produced. No text is output because of the prepended ''[

''[repeat 4 ; 'fd 30' then 'rt 90'
)

NB. =========================================================
Lab Section "rep" expands "repeat"
The verb "rep" is similar to "repeat", without a "do".

Try the following example.

rep 4; 'fd 30' then 'rt 90'
)

NB. =========================================================
Lab Section
Now we embed "rep" within a "repeat" to get a fancy one-liner of repeating boxes.

Definitely try this one.

''[repeat 24 ; (rep 4; 'fd 30' then 'rt 90') then 'rt 15'
)

NB. =========================================================
Lab Chapter Programming multiple turtles
NB. =========================================================
Lab Section Verb arguments
The main turtle movement verbs and control verbs constructed in Jlo are monadic and anticipate that multiple turtles may be employed. If only one argument is supplied to the turtle movement verbs, Jlo applies its value to ALL turtles. If more than one argument is supplied to the turtle movement verbs, Jlo applies them to the turtles in the order they are supplied. An error is signaled if (#argument)~:(#turtles) .

For example, try the commands below.

cs ''[ home ''
forward 15      NB. all turtles move forward 15 steps

NB. assuming there are 2 turtles,
forward 15 _10  NB. turtle 0 moves forward 15 steps; turtle 1, back 10
)

NB. =========================================================
Lab Section
Most of the turtle control verbs expect their argument to be a list of turtle numbers for which the control is to apply, or an empty argument ('') if it is to apply to all turtles. An error is signaled if an invalid turtle number is supplied. For example.

pendown ''      NB. all turtle pens are lowered

NB. the following will produce an error
pendown 2 1     NB. because there are only turtles 0 and 1
)

NB. =========================================================
Lab Section Initializing turtles
A slightly different rule applies to the monadic version of the main turtle initialization verb, iTS =: initTurtleState . iTS places a single turtle at the center of the graphic screen and multiple turtles on a circle around the center, turtle(s) facing northward.

Monadic iTS requires a singleton input which is a whole number. The magnitude of the singleton number indicates the number of turtles to be created. A postive singleton number places the turtles at regular intervals around the circle; a negative singleton number places the turtles at IRregular intervals around the circle.

Try the following examples.

Notice that the Heading's are given in radians with East considered 0 and anticlockwise turns being "positive." The noun "Heading" typed reports the turtle headings in degrees and clockwise turns are "positive."

iTS 3[cs ''
iTS 1[cs ''
iTS _3[cs ''
)

NB. =========================================================
Lab Section
Dyadic iTS takes turtle heading information as the left-hand, as well as the monadic iTS's argument. For example to produce a new 2-turtle system with turtles facing 30 and 60 degrees right of north, enter the following.

30 60 iTS 2[ cs ''
)

NB. =========================================================
Lab Chapter Programming repetition and recursion
NB. =========================================================
Lab Section spiro54
"spiro54" is created and executed by embedding the existing verb "forwardright" within an explicit J verb named spiro54 which contains a "for." loop control. The arguments for rt and fd in spiro54 are derived from dividing 360 degrees by 5*4 and 5, respectively . The example was suggested by Fraser Jackson's tutorial Lab for his similar system.

for. i. x do.
rt 18
y (5 repeats forwardright) 72
end.
)

The arguments of spiro54, 20 and 13, request 20 loops and sides of length 13. The 20 loops are chosen because 20=5*4 .

20 spiro54 13 [ cs ''
10 spiro54 13 25 [ cs ''
)
PREPARE
for. i. x do.
rt 18
y (5 repeats forwardright) 72
end.
)
PREPARE

NB. =========================================================
Lab Section polyspi
Recursive turtle graphics are also very straightforward by combining J verb definitions with turtle movement commands. "polyspi" is ubiquitous and various in Logo literature. Abelson and diSessa suggest the following recursive graphics.

x rightforward y
if. y > 25 do. return. end. NB. this line may not be needed
x polyspi y+1
)

On systems that have a "Break" feature, the indicated line may be unnecessary.

Notice that the two turtles are creating different polyspi's with the arguments 95 and 117 -- really  95,1  and 117,1 .

95 117 polyspi 1 [cs ''
)
PREPARE