#!/usr/bin/env python

import Image, ImageDraw
import sys
import math

output = Image.new('RGB', (1024, 1024), (255, 255, 255))
draw = ImageDraw.Draw(output)
xy = (512, 512)

input = open(sys.argv[1]).read().strip()

colour = (0, 0, 0)
pen_down = False
angle = 180.0
n = ''
while input:
    b = input[0]
    input = input[1:]
    if b in '-0123456789':
        n += b
    else:
        if n:
            num = int(n)
        else:
            num = 0
        n = ''

        if b == 'T':
            # turn command
            angle += float(num)
        elif b == 'M':
            # move command
            dx = float(num) * math.sin(math.radians(angle))
            dy = float(num) * math.cos(math.radians(angle))
            nxy = (xy[0] + dx, xy[1] + dy)
            if pen_down:
                draw.line([xy, nxy], fill=colour)
            xy = nxy
        elif b == 'U':
            pen_down = False
        elif b == 'D':
            pen_down = True

del draw
output.save(sys.argv[2])
