cleanup path to curve
[perl-landing-airplanes.git] / trace-path.pl
index c3fae62..e58fe4f 100755 (executable)
@@ -45,15 +45,21 @@ our $black       = SDL::Color->new( 0x00, 0x00, 0x00 );
 my ( $last_x, $last_y ) = ( 0,0 );
 
 our @path;
+sub reset_path { @path = () }
 
 sub curve {
-       return unless $#path > 4;
-       my $curve = Math::CatmullRom->new( splice @path, 0, $#path + $#path / 2 );
+       if ( $#path < ( 4 * 2 - 1 ) ) { # less than 4 points
+               warn "path too short ", dump @path;
+               reset_path;
+               return;
+       }
+
+       my $curve = Math::CatmullRom->new( @path );
        my @curve = $curve->curve( $mouse_trashold * 10 );
        debug 'curve' => @curve;
 
        my $i = 0;
-       while ( $i < $#curve ) {
+       while ( $i < $#curve - 4 ) {
                my $from_x = int($curve[$i++]);
                my $from_y = int($curve[$i++]);
                my $to_x   = int($curve[$i++]);
@@ -67,6 +73,7 @@ sub curve {
                );
        }
        $app->sync;
+       reset_path;
 }
 
 sub handle_events {
@@ -90,7 +97,7 @@ sub handle_events {
                        if ( $key eq 's' ) { # XXX draw curve
                                curve;
                        } elsif ( $key eq 'backspace' ) { # XXX clean screen
-                               @path = ();
+                               reset_path;
                                my $rect = SDL::Rect->new( -x => 0, -y => 0, -w => $w, -h => $h );
                                $app->fill( $rect, $black );
                                $app->update( $rect );