X-Git-Url: http://git.rot13.org/?p=perl-landing-airplanes.git;a=blobdiff_plain;f=trace-path.pl;fp=trace-path.pl;h=e58fe4f6be9023bd5952b830fa5a9b4c6156d773;hp=c3fae62d0115e469b7cc136ff933508b94457063;hb=9dc60040e1fc532aafc0739c0caeb61a74cd4079;hpb=b09062755734164dd3dee0529c8511ec20017adc diff --git a/trace-path.pl b/trace-path.pl index c3fae62..e58fe4f 100755 --- a/trace-path.pl +++ b/trace-path.pl @@ -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 );