Turtles and More

Turtle graphics. This to me resembles a Kina or Sea Urchin

My wife recently became interested in the Spirograph (™) system. Since her birthday was coming up, so did I, for obvious reasons. If you have never come across Spirograph (™) I can highly recommend it, as it enables the production of glorious swirls and spirals, using a system of toothed wheels and other shapes. When you use multicoloured pen, the results can be amazing.

Of course, I had to translate this interest into the computer sphere, and I immediately recalled “Turtle Graphics” which I have used before. It is possible to create graphics very similar to the Spirograph (™) designs very simply with Turtle Graphics.

This resembles the sort of things generated by Spirograph (TM)

Turtle Graphics have a long history, stretching back at least to the educational programming language Logo. Although variations of the original Logo language exist, they are fairly rare, but the concept of Turtle Graphics, where a cursor (sometimes shown as the image of a cartoon turtle) draws a line on a page, still exists. The turtle can be directed to move in a particular way, based on instructions by the programmer.

For instance the turtle can be instructed to move forward a certain distance, turn right through 90°, and repeat this process three times. The result is a small square. Or the turtle could be instructed to move forward and turn only 60°, repeating this 5 times to draw a hexagon. Using simple instructions like this allow the drawing of practically anything.

Square and Hexagonal spirals
Square and hexagonal spirals drawn by Turtle Graphics

I use an implementation of Turtle Graphics in the turtle module of the Python programming language but it is probably available for other programming languages. Python is probably an easy language to learn from scratch than Logo, and in addition Python can be used for many other things than Turtle Graphics. Python is available for Windows, OS/X, and Linux/Unix, and for several other older or less well known platforms.

Where things become interesting is when the looping abilities of Python are used to enhance a program. If the programmer gets the turtle to draw a square, then makes the turtle turn a little and repeats the process, the result is a circular pattern. Starting with a more interesting shape can produce some interesting patterns.

Rotated Square - Turtle graphics
Rotated Square – Turtle graphics

After a while, though, the patterns begin to seem very similar to one another. One way to add a bit of variation is to use the ability to make the turtle move to a specific position, drawing a line on the way. As an example, consider a stick hinged to another stick, much like a nunchaku. If one stick rotates as a constant speed and the second stick rotates at some multiple of that, then the end of the second stick traces out a complex curve.

Flower shape
Flower shape – turtle graphics

In Python this can be expressed like this:

x = int(a * math.sin(math.radians(c * i)) + b * math.sin(math.radians(d * i)))
y = int(a * math.cos(math.radians(c * i)) + b * math.cos(math.radians(d * i)))

where c and d are the rates of rotation of the two sticks and and b are the lengths of the stick. i is a counter that causes the two sticks to rotate. If the turtle is moved to the position x, y, a line is drawn from the previous position, and a curve is drawn.

The fun part is varying the various parameters, a, b, c, d, to see what effect that has. The type of curve that is created here is an epicycloid. For larger values of c and d the curves resemble the familiar shapes generated by Spirograph (™).


The equations above use the same constants in each equation. If the constant are different, some very interesting shapes appear, but I’m not going to go into that here. Suffice it to say, I got distracted from writing this post by playing around with those constants!

The above equations do tend to produce curves with radial symmetry, but there is another method that can be used to produce other curves, this time with rotational symmetry. For instance, a curve can be generated by moving to new point depending on the latest move. This process is then iterative.

Gravity Wave - turtle graphics
Gravity Wave turtle graphics

For instance, the next position could be determined by turning through an angle and move forward a little more than the last time. Something like this snippet of code would do that:

for i in range(1, 200):

a = a + 1
c = c + 10

This brings up a point of interest. If you run code like this, ensure that you don’t stop it too soon. This code causes the turtle to spin and draw in a small area for a while, and then fly off. However it quickly starts to spin again in a relatively small area before once more shooting off again. Evidently it repeats this process as it continues to move off in a particular direction.

Turtle graphics - a complex curve from a simple equation
Turtle graphics – a complex curve from a simple equation

Another use of turtle graphics is to draw graphs of functions, much like we learnt to do in school with pencil and squared paper. One such function is the cycloid function:

x = r(t – sine(t))

y = r(1 – cosine))

This function describes the motion of a wheel rolling along a level surface and can easily be translated into Python. More generally it is the equation of a radius of a circle rolling along a straight line. If a different point is picked, such a point on a radius inside the circle or a point outside the circle on the radius extended, a family of curves can be generated.

Cycloid curve - turtle graphics
Cycloid curve – turtle graphics

Finally, a really technical example. An equation like the following is called a dynamic equation. Each new ‘x’ is generated from the equation using the previous ‘x’. If this process is repeated many times, then depending on the value of ‘r’, the new value of ‘x’ may become ever closer to the previous value of ‘x’.

x(n+1) = rx(n)(1 – x(n))

If the value of ‘r’ is bigger than a certain value and less than another value, then ‘x’ flip-flops between two values. If the value of ‘r’ is bigger than the other value, and smaller than yet another value then ‘x’ rotates between 4 values. This doubling happens again and again in a “period doubling cascade“.

Turtle graphics - electron orbitals
Turtle graphics – electron orbitals

I’ve written a turtle program to demonstrate this. First a value for ‘r’ is chosen, then the equation is repeated applied 1,000 times, and the next 100 results are plotted, x against r. In the end result, the period doubling can easily be seen, although after a few doubling, the results become messy (which may be related to the accuracy and validity of my equations, and the various conversion between float and integer types).

Period doubling
The “fig tree” curve calculated in Python and plotted by Turtle Graphics.


English: Cumulus cloud above Lechtaler Alps, A...
English: Cumulus cloud above Lechtaler Alps, Austria. Español: Nube cumulus sobre los Alpes austriacos. Deutsch: Cumuluswolke über Lechtaler Alpen, Österreich. (Photo credit: Wikipedia)

Clouds are collections of water droplets suspended in the air. A cloud is formed as the water vapour in the air condenses onto particles of dust or other water droplets. The water in a cloud weighs tonnes! It’s a good job that the droplets don’t have time to coalesce into great balls of water before they reach the ground, but I suppose that to insects a droplet is a huge ball of water, and able to cause havoc.

As anyone who has flown in an aircraft is likely to know, clouds are not well defined, and in fact they could be described as nebulous or hazy. From a mathematical point of view they are fractal and the fractal dimension (a measure of their fuzziness) varies depending on the cloud.

Fractal plant curve, made using an L-system
Fractal plant curve, made using an L-system (Photo credit: Wikipedia)

A common pastime on a summer’s day is to imagine shapes in the clouds. That one may look vaguely like a car, that one like a dog, and so on. But really, the only shape that clouds have is “cloud-like“.

There are many types of cloud shape, depending on the conditions and the altitude where the cloud is forming, but the usual depiction of a cloud generally looks like a cumulus type. This type forms the usual shape like piles of cotton wool in the sky, with mountain, canyons, and even castles.

Embed from Getty Images

There is always water vapour in the air, even if it doesn’t form clouds, although we cannot see it. As I said above, clouds are formed when this water vapour condenses on small particles in the air (and other conditions are right). Sometimes there are attempts to make rain by “seeding” a cloud with small particles to increase the rate of condensation and thus increasing the size of the water droplets.

At a certain  size the droplets become to big to be buoyed up by the air and start to fall, picking up more moisture as they do. As I understand it, this cloud seeding process is limited in its success, but I may be wrong.

Cessna 210 (OE DSD), rebuilt for cloud seeding...
Cessna 210 (OE DSD), rebuilt for cloud seeding, with 2 silver iodide generators (Photo credit: Wikipedia)

Clouds sometimes form at ground level, if the conditions are right, and then we call them fogs or mists. This often happens when light rain is falling and there is a lot of moisture in the air, but it can happen simply because the conditions are right.

Living where I do, I occasionally have reason to visit the local airport in Wellington. The airport is situation on a section of land that was brought up by a an earthquake, so that it is on a narrow stretch of land between two sets of hills. Over the hills to the East of the airport is the entrance to the Wellington Harbour.

English: Aerial view of the Miramar Peninsula,...
English: Aerial view of the Miramar Peninsula, Wellington, New Zealand. Wellington International Airport is visible and the beach just above the left-hand end of the runway is Lyall Bay. Downtown Wellington city, the harbour and port can be seen in the distance. (Photo credit: Wikipedia)

On several occasions I have seen sea mist roll in from Cooks Strait to the South and extends tongues of thick mist over the airport and the Harbour entrance. This causes the airport to shut down until the conditions have cleared, spoiling the travel plans of hundreds of people.

Other clouds which are familiar to many are the stratus clouds. These clouds are layers which cover all or most of the sky under some conditions. They often presage rain or other forms of precipitation. Stratus clouds range from light to dark and in many cases might cause a drop in one’s spirits.

English: Stratus undulatus clouds. I took this...
English: Stratus undulatus clouds. I took this picture out the car window on the way to Vancouver. (Photo credit: Wikipedia)

Certainly the dark stratus has that effect on me, and there is little that is more spectacular than breaking through a layer of cloud in a plane. The tops of the clouds will be brightly lit by the sun, and sometimes whorls or rivers of cloud can be seen from above.

The tops of the clouds can be quite lumpy and cumulus-like, and descending into the clouds is like descending into mountains and canyons and the lumps and bumps of the cloud can whizz past like scenery on a train, until the plane finally breaks through the greyer, darker ceiling of the cloud layer.

English: "The two main cloud types are St...
English: “The two main cloud types are Stratocumulus mixing with Cumulus in the foreground with Cumulus beyond” ~ Identified by http://www.cloudappreciationsociety.org/ (Photo credit: Wikipedia)

However, broken stratus clouds are the clouds which produce amazing sunsets as the sun drops through the layers and gaps in the clouds. Very often a beam of sun breaks through a stratus layer and lights up the water droplets or dust producing what looks like a column of light. These rays are known as crepuscular rays.

Add to that the amazing colours that result from the breakthrough sun beams and the dust and water droplets and sunsets can be very beautiful, even if the sun light is in fact refracting or reflecting from pollution in the air.

Crepuscular Rays and over
Crepuscular Rays and over (Photo credit: Wikipedia)

When the sun has gone below the horizon, it can still illuminate clouds above the horizon causing them to glow with an orange light, as the blue light is absorbed by the thick layer of atmosphere these rays which are almost tangential to the earth’s surface have to pass through.

Cumulus clouds are often sought out by glider pilots, since they are often formed by an up welling of air over a particularly warm piece of land. The up welling of air provides the glider pilot with extra lift, which allows them to travel vast distances, but a downside is that some clouds can be chaotic and turbulent. Birds will often guide a pilot to the up draughts there is no cloud.

Embed from Getty Images

Another totally different sort of cloud has appeared over recent years, and that is the Internet cloud. The Internet cloud is also somewhat nebulous, and allows us to take a photograph on one device (computer phone or tablet) and view it almost immediately on another device.

The cloud (often the Cloud) also allows for automatic backups for devices – if your device implodes or is lost or stolen, your data is safe. Mostly. For if you sync (synchronise) your device with the Cloud, and then delete a photograph, it will shortly be removed from the Cloud and lost.

Embed from Getty Images

To prevent data loss, you can backup to somewhere else on the cloud, so there are two (or more) cloud copies, or you can backup to a local computer or local storage, so that if you delete something by mistake you can always get it back. As anyone in the computer business will tell you, one backup is never enough!

Embed from Getty Images