exact distance for mouse, improved dashes
[perl-landing-airplanes.git] / trace-path.pl
index c3fae62..c0d695d 100755 (executable)
@@ -38,26 +38,29 @@ our $event = SDL::Event->new;
 
 our $mouse_down = 0;
 
-our $mouse_color = SDL::Color->new( 0x00, 0x80, 0x00 );
-our $path_color  = SDL::Color->new( 0xff, 0x00, 0x00 );
+our $mouse_color = SDL::Color->new( 0x00, 0x00, 0x80 );
+our $path_color  = SDL::Color->new( 0xff, 0xff, 0x80 );
 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 );
-       my @curve = $curve->curve( $mouse_trashold * 10 );
+       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 $points = $#path + 1 - 4; # remove start/end points
+       my @curve = $curve->curve( $points );
        debug 'curve' => @curve;
 
        my $i = 0;
-       while ( $i < $#curve ) {
-               my $from_x = int($curve[$i++]);
-               my $from_y = int($curve[$i++]);
-               my $to_x   = int($curve[$i++]);
-               my $to_y   = int($curve[$i++]);
+       while ( $i <= $#curve - 4 ) {
                line(
                        int($curve[$i++]),
                        int($curve[$i++]),
@@ -67,6 +70,7 @@ sub curve {
                );
        }
        $app->sync;
+       reset_path;
 }
 
 sub handle_events {
@@ -90,7 +94,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 );
@@ -105,9 +109,8 @@ sub handle_events {
                } elsif ( $type == SDL_MOUSEMOTION() ) {
                        my ( $x, $y ) = ( $event->motion_x, $event->motion_y );
                        #debug 'mouse', $mouse_down, $x, $y;
-                       my $dx = abs( $last_x - $x );
-                       my $dy = abs( $last_y - $y );
-                       if ( $mouse_down && ( $dx > $mouse_trashold || $dy > $mouse_trashold ) ) {
+                       my $d = sqrt( ( $x - $last_x ) ** 2 + ( $y - $last_y ) ** 2 );
+                       if ( $mouse_down && $d > $mouse_trashold ) {
                                if ( $#path < $max_path_length ) {
                                        push @path, $x, $y;
                                        my $rect = SDL::Rect->new( -x => $event->motion_x - 1, -y => $event->motion_y -1 , -w => 3, -h => 3 );