# Version 1

"""
Provides a simple turtle that leaves a trace of its movements in the
form of a Povray file.  When rendered, the turtle's path will be
shown as connected cylinders.

The pen() method toggles the pen up or down (starts down).  A trace
is added to the Povray file only when the pen is down.
"""

from graphics2 import Vector, Povray # defined in earlier modules
import os  # used to check which operating system
from random import randint  # used for random wandering

class Turtle:

    def __init__(self,name):
        self.name = name
        self.outfile = Povray(name + ".pov")
        self.filepath = self.outfile.filepath
        self.direction = Vector(1,0,0)  # current orientation
        self.position = Vector(0,0,0)   # current position
        self.pendown = True

    def forward(self,n):
        """
        add direction vector to position, scale movment to n tenths
        """
        newpos = self.position + (self.direction * n * 0.1)
        if self.pendown:
            self.outfile.point(self.position)
            self.outfile.edge(self.position, newpos)
            self.outfile.point(newpos)
            
        self.position = newpos
        print "Current position: %s" % (self.position.xyz(),)

    def rotate(self, axis, degs):
        """
        change the direction vector by rotating around
        any of the three axes
        """
        if axis.upper()=='X':
            self.direction = self.direction.rotx(degs)            

        elif axis.upper()=='Y':
            self.direction = self.direction.roty(degs)
            
        elif axis.upper()=='Z':
            self.direction = self.direction.rotz(degs)            

        else:
            print 'huh?'

    def pen(self):
        """
        toggle the pen
        """
        if self.pendown:
            self.pendown = False
            print "Pen is up"
        else:
            self.pendown = True
            print "Pen is down"

    def showtrail(self):
        self.outfile.close()
        # only render automatically in linux -- Windows users
        # may use Povray's graphical user interface
        if os.name == 'linux2':
            pov.render(self.filepath + self.name + ".pov")

def test():
    """
    Draw a square
    """
    myturtle = Turtle("Frodo")
    myturtle.pen()
    myturtle.forward(10)
    myturtle.pen()
    myturtle.rotate('z',90)
    myturtle.forward(10)

    for i in range(3):
        myturtle.rotate('z',90)
        myturtle.forward(20)

    myturtle.rotate('z',90)
    myturtle.forward(10)
    myturtle.showtrail()

def test1(n = 20):
    """
    Wander randomly for n moves (default 20)
    """
    myturtle = Turtle("Wanderer")
    for move in range(n):
        myturtle.forward(randint(1,20))
        axis = ['x','y','z'][randint(0,2)]  # pick x,y or z at random
        degrees = 10*randint(1,35)          # 10,20,30...up to 350 degrees
        myturtle.rotate(axis, degrees)
    myturtle.showtrail()
    
        
