From: Dobrica Pavlinusic Date: Wed, 19 May 2010 18:09:42 +0000 (+0200) Subject: cleanup path to curve X-Git-Url: http://git.rot13.org/?p=perl-landing-airplanes.git;a=commitdiff_plain;h=9dc60040e1fc532aafc0739c0caeb61a74cd4079 cleanup path to curve --- 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 );