bendun.cc

Python turtles

Written , a 2 minute read

While I'm always scared of graphics standardization attempts in C++, Python has turtle graphics library for over 26 years!

$ git clone https://github.com/python/cpython
$ cd cpython
$ git log --diff-filter=A -- Lib/turtle.py
commit b241b67b8954b0679377af00d668e3dc92f4c858
Author: Guido van Rossum <guido@python.org>
Date:   Fri Dec 4 16:42:46 1998 +0000

    Turtle graphics

This was the time of 1.5 release which is so old that it even contains instructions on how to build Python for BeOS, the system that today is sadly forgotten (see Haiku).

As mention in release notes of 1.5.2:

turtle.py is a new module for simple turtle graphics. I'm still working on it; let me know if you use this to teach Python to children or other novices without prior programming experience.

Lib/turtle.py was affected by 70 commits, with major improvements in 2.6 release. Let's see some pics!

Hexagons created using turtle code above
Code
from turtle import *

D, N = 100, 6

def hexagon():
    for i in range(N):
        forward(D)
        left(360 / N)

pencolor('#fbf1c7')
hexagon(); right(2 * 360 / N)
hexagon(); right(2 * 360 / N)
hexagon()

getscreen().getcanvas().postscript(file='turtle.eps')

Then convert turtle.{eps,png}

Hexagons overlapping forming a circle
Code
from turtle import *
from math import sqrt

w, h = screensize()

D = min(w, h)
N = 6

speed('fastest')

pencolor('#fbf1c7')
penup()
backward(sqrt(3) / 4 * D)
right(90)
forward(sqrt(3) / 2 * D)
left(90)
pendown()

for i in range(180):
    forward(D)
    left(360 / N + 1.0)

getscreen().getcanvas().postscript(file='turtle.eps')

Soo, then I got distracted by this amazing tool and some new notation that I haven't seen before. Check it out or wait for a next part of this post sometime in a future.

That's it!

Well this is more a microblogging or small note then a post. To say something interesting, this really motivates me to move from blog to a digital garden. The reverse chronological order of posts really doesn't suit this entry - a better one would be a page collecting various Python quirks, with this as a subpage. For now I stick with the current infrastructure, to get habit of writing going and not do distract myself with writing yet another generator or throwing yet another site reconstruction.

Since we are in a topic of gardening - in permaculture terms I'm in a state of observing and interacting. I want to really get a feeling for this writing form and information layout and then start applying thoughtful changes.