#!/usr/bin/perl5

use Turtle;

$length = 300;
$speed  =   0.5;

$a = Turtle->create; 
$m = $a->{'-main'};
$canvas = $a->{'-canvas'};
$b = Turtle->create(-main=>$m,-canvas=>$canvas); 
$c = Turtle->create(-main=>$m,-canvas=>$canvas); 
$d = Turtle->create(-main=>$m,-canvas=>$canvas); 

$a->x(0);$a->y(0); $a->color('red');
$b->x(100);$b->y(0); $b->color('green');
$c->x(100);$c->y(100); $c->color('yellow');
$d->x(10);$d->y(10); $d->color('blue');

$m->update;

while( abs($a->x - $b->x) > 1 || abs($a->y - $b->y) > 1) {
    &follow($a,$b);
    &follow($b,$c);
    &follow($c,$d);
    &follow($d,$a);
}

print "done\n";

$a->x(0);$a->y(0); $a->color('black');
$b->x($length);$b->y(0); $b->color('black');
$c->x($length);$c->y($length); $c->color('black');
$d->x(0);$d->y($length); $d->color('black');

$speed = $speed * 2;
# while( $a->x != $b->x) {
while( abs($a->x - $b->x) > 1 || abs($a->y - $b->y) > 1) {
    &follow($a,$b);
    &follow($b,$c);
    &follow($c,$d);
    &follow($d,$a);
}


$m->MainLoop();

exit;

sub follow {
    my ($a,$b) = @_;

    $dir = atan2(($b->y - $a->y) , ($b->x - $a->x))*360/3.1415926/2;
    $a->direction($dir);
    $a->forward($speed);
}

