Sniff is a "Scratch-like" programming language that's designed to help Scratchers move gently from Scratch to more conventional languages. They can start writing programs, without having to learn a new language because Sniff is based on Scratch. They learn a little more about variables, compiling, syntax errors (!), and they can have fun controlling real hardware while they're doing it.

Saturday, 3 January 2015

Turtle Graphics (part 1)

One of the key differences when moving from Scratch to Sniff is that Sniff doesn't have any built in graphics, or sprites the way Scratch does.In fact Scratch is so centred around sprites that it becomes hard to consider writing code that doesn't move the cat around. By  not having sprites, its possible to think about all the other things you can do in Sniff!

However turtle graphics are a fun way of exploring geometry, and as Sniff has developed, its become sufficiently powerful that making your own turtle routines is pretty trivial. In fact all you need is:

make display window device
make displayX number
make displayY number
make displayColor number

make direction number
make distance number
make penDown boolean

when move
.change displayX by distance * cos of direction
.change displayY by distance * sin of direction
.if penDown
..tell display to "draw"
..tell display to "move"

We create a display device, which in this case is a window (which works on Linux (Pi) or Windows). If you want to try this on Arduino or even EV3, then switch the device for something else, and it should work more or less unchanged. We then set up the usual display parameters.

The important code lives in the "move" script. The movement is defined by a direction, a distance, and penDown. The code is pretty self evident if you're familiar with how sin and cos work. If you're working with younger kids then you can treat this as a black box, or even take the opportunity to introduce the concepts in a practical and useful way.

when start
.set displayX to displayX/2
.set displayY to displayY/2
.tell display to "move"
.set direction to 0
.set distance to 100
.set displayColor to 777
.set penDown to yes

When we start we set the turtles position to the centre of the screen, facing "east", with a step size of 100 pixels. We also tell it to draw in white.

After that we can draw whatever we like but lets start with a hexagon:

.repeat 6
..broadcast move and wait
..change direction by 60

Easy! Now lets make a Spiral:

.set distance to 1
..broadcast move and wait
..change direction by 9.5
..set distance to 1.01 * distance
..set penDown to not penDown

We set the initial distance to 1 so the spiral starts out small, and each time we turn by a fixed amount. We also increase the distance of the step a little each time. Finally just to make it more fun, we turn penDown on and off, so that only alternate steps are drawn. This gives us a dotted line. Experiment with the different values!

.set distance to 2
..broadcast move and wait
..change direction by pick random -40 to 40
..set penDown to not penDown

Here we choose a small distance again, and then at each step we turn randomly. The result is a "random walk". Random walks turn up everywhere, but one common example is Brownian motion.

They have lots of interesting mathematical properties. For example it doesn't really matter what values we pick for distance, and the angle we turn through - it just affects the scale, but the behaviour is the same (after a moderate number of steps the original direction is statistically irrelevant to the current direction). Also the average distance traveled is proportional to the square root of the number of steps.

There's still lots more to do with Turtles, but that's a good start for one post...

No comments:

Post a Comment