# Extending turtle graphics ( vector files)

## Purpose

By using sprite and other operations, and various system calls or graphics libraries you can draw repeated copies of a small figure wherever you like on-screen, rotated or scaled as you wish. But the programming is messy, and effects of scaling- jaggies, etc- spoil the result. And it can be slow...

Turtle graphics are a perhaps neglected use of JB/LB. They chime with students from a very early age, since they can relate to 'going forward' and 'turning'. They love patterns like 'squirals' of near-90 degree turns and changing length...

What I did was design a format for a descriptor-string which specifies shapes as a series of 'forwards' and 'turns'. To draw a square you go forward four equal distances, turning 90 degrees after each move. This I save as '4, 1, 90, 1, 90, 1, 90, 1, 90'. The initial 4 means there are four nodes defined by distance to reach them and angle to then turn..

This works nicely- a very simple LB program can read such a string and parse out the moves and turns. By writting it into a little rooutine you can call it with the shape\$ specified, to start at a desired screen position, and initial orientation, width of line and colour, and draw the shape, to scale. Then I realised that for more interesting shapes it woukld be stupid to draw a shape on paper and measure the distances/angles by hand. So instead I mouse-click the shape as a sries of positions on a window and derive the values from the mouse x/y coordinates. It's all a work-in-progress. There are various bits yet to implement or a bit ugly at present. I may add a marker for 'pen up' and 'pen down' to allow discontinuous shapes. Or many other ideas. And I'll be tidying up a lot before completing this webpage with a link to source LB code. But it already allows me to save any interesting shapes in a shape-library. And you can contribute your own!

## Some simple shapes

Data format is... 'number of data items, then the data in distance/degree_turn_clockwise form.'

### Pentangle

```10,
1,    144,     1,    144,     1,     144,    1,   144,    1,  144,   1,   144
```

### Dancer

```70,
0.10,  49.09,  0.26, -40.17,  0.14,  47.40,  0.20, -76.53,  0.32, -25.40,  0.18,  48.90,  0.04,
72.69,  0.13, 101.83,  0.24, -50.45,  0.10, -63.91,  0.13, -54.34,  0.15,  18.32,  0.12,  67.36,  0.11,
51.55,  0.08,  83.88,  0.09,  20.37,  0.07, -31.27,  0.06, -74.28,  0.09, -32.63,  0.19, -44.18,  0.32,
41.09,  0.03, 122.05,  0.23,-310.27,  0.22, 332.11,  0.18, -39.49,  0.17, -70.22,  0.27,
42.99,  0.09, -33.02,  0.14,-214.99,  0.25,  64.99,  0.06, -64.82,  0.15, 308.19,  0.21, -56.02,
0.17,  41.34,  0.03,-232.13
```

### Simple LB code calling a defined shape twice.

```
'   NB turtle commands work in  DEGREES....

nomainwin
WindowWidth  =830:     WindowHeight =650
graphicbox #w.gb, 10, 10, 800, 600

open "Display" for window as #w

#w "trapclose [quit]"

shape\$ ="10,  1,    144,     1,    144,     1,     144,    1,   144,    1,  144,   1,   144"    '   pentangle

#w.gb "home ; down ; fill 60 60 60"

radius =240: side =20

call display shape\$, 100, " 50 255 120", 500, 300,  0 '   here, a single call. Easily repeatedly called by program.
call display shape\$,  60, "255  50 120", 500, 400, 20

wait

[quit]
close #w
end

sub display shape\$, scale, col\$, x, y, angle
noOfTerms =val( word\$( shape\$, 1, ","))
#w.gb "color "; col\$
#w.gb "up ; goto "; x; " "; y; " ; down ; size 1"
#w.gb "north ; turn "; angle
for i =1 to noOfTerms step 2
vStep  =val( word\$( shape\$, i +1,    ","))
vAngle =val( word\$( shape\$, i +2, ","))
#w.gb "go ";    vStep *scale
#w.gb "turn ";  vAngle
print "go "; vStep *100; " ; turn "; vAngle
next i
end sub
```

### To be continued...

As can be seen from the initial graphic, my program currently allows defining new shapes by mouse, and drawing them in arrangements of your choice.