pySVG meets Trundle, the turtle

My python pySVG just got a mate named Trundle the turtle (named after a turtle in my kids english school book). For a while now I haven’t coded much for pySVG. I had a lot of requests and emails though and one of those involves resurrecting parts of my old abandoned lib for representing natural phenomena.

I can’t (yet) write much about the project yet but i thought the addition of some turtle graphics might be helpful for some people using pySVG.

Basically Trundle supports the normal turtle commands (via method calls) like - forward(distance) - backward(distance) - right(angle) - left(angle) - moveTo(vector) - penDown - penUp

So you just initialize an instance of the Turtle class, along with the initial position, orientation and the style for drawing (filling, stroke, strokewidth). You move it to the position you want to start drawing in. Lower the pen. Draw.

Whenever you lower or raise the pen the path so far is stored into a polyline object and a new one will be instantiated. All such polylines are added to a list. The only thing you have to keep in mind is that you need to call finish() when you completed drawing or else the last path won’t be added. And if you create an instance of svg and call addTurtlePathToSVG you will have the turtles drawings nicely added into the svg container which you can save as usual.

I haven’t finished or polished the api yet, so you need to get it from here (login might be required).

I just give you some example code and the result ok?

from pysvg.turtle import Turtle, Vector
from pysvg.structure import svg

def testLindenMayer():
    s=svg(0, 0, 2000, 2000)
    commands='F+F-F-FF+F+F-F+F+F-F-FF+F+F-F+F+F-F-FF+F+F-F+F+F-F-FF+F+F-F'
    t=Turtle()
    t.moveTo(Vector(500,250))
    t.penDown()
    angle=90
    distance=40
    for cmd in commands:
        print(cmd)
        if cmd=='F':
            t.forward(distance)
        elif cmd=='+':
            t.right(angle)
        elif cmd=='-':
            t.left(angle)
        print(t.getPosition())
    t.penDown()
    print (t.getXML())
    s=t.addTurtlePathToSVG(s)
    s.save('./testoutput/testTurtle.svg')


if __name__ == '__main__': 
    testLindenMayer()

This produces the following image:

SimpleTurtleExample