#!/usr/local/bin/perl5

#       +----20+10-------+
#       |
#      20 
#       |     ---10-+
#       |           |
#       |          10
#       +----20-----+
           
# forward 10             $x += $length;
# right                  $y += $length;
# forward 10             $x -= $length;
# right                  $y -= $length;
# forward 20
# right 
# forward 20
#  .....

use Turtle;

$x = 0;
$y = 0;
$dir = 0;
$count = $ARGV[0];



$turtle = Turtle->create();

$turtle->color('red');
$turtle->width(3);

$length = 10;
for($i=0;$i<$count;$i++) {
    &forward($length);
    &right();
    &forward($length);
    &right();
    $length += 10;
}


$turtle->MainLoop();

sub forward {			# 
    my ($length) = @_;

    if ($dir == 0) {
	$turtle->move($length,0);
#	$x += $length;
#	print "forwad $legnth (X = $x,Y = $y)\n";
    } elsif ($dir==90) {
#	$y += $length;
#	print "forwad $legnth (X = $x,Y = $y)\n";
	$turtle->move(0,$length);
    } elsif ($dir==180) {
#	$x -= $length;
#       print "forwad $legnth (X = $x,Y = $y)\n";
	$turtle->move(-$length,0);
    } elsif ($dir==270) {
#	$y -= $length;
#	print "forwad $legnth (X = $x,Y = $y)\n";
	$turtle->move(0,-$length);
    }
}

sub right {
    $dir += 90;
    $dir %= 360;
}

