X-Git-Url: http://git.rot13.org/?p=perl-landing-airplanes.git;a=blobdiff_plain;f=trace-path.pl;h=b97779ca9fa014b072c3958f0c8aa8984c1abb25;hp=0fe935399bf65993b53db1425daf167d0e4c565f;hb=a8d7e58705cae0a43cbbc3b996630a1877c81f26;hpb=33b71a0a0de167361a539859460c1602fbb60954 diff --git a/trace-path.pl b/trace-path.pl index 0fe9353..b97779c 100755 --- a/trace-path.pl +++ b/trace-path.pl @@ -10,6 +10,7 @@ use SDL::Constants; use SDL::Event; use Math::CatmullRom; use Algorithm::Line::Bresenham qw(line); +use Math::Bezier; use Carp qw(cluck); use Data::Dump qw(dump); @@ -124,9 +125,7 @@ my ( $last_x, $last_y ) = ( 0,0 ); our @path; sub reset_path { @path = () } -sub curve { - - push @path, lading_points( $path[-2], $path[-1] ) if $#path > 1; +sub curve_catmull_rom { if ( $#path < ( 4 * 2 - 1 ) ) { # less than 4 points warn "path too short ", dump @path; @@ -136,7 +135,23 @@ sub curve { my $curve = Math::CatmullRom->new( @path ); my $points = $#path + 1 - 4; # remove start/end points - my @curve = $curve->curve( $points ); + return $curve->curve( $points ); +} + +sub curve_bezier { + my $curve = Math::Bezier->new( @path ); + return $curve->curve( $#path + 1 ); +} + +our $curve_type = 0; + +sub curve { + # add landing path points + push @path, lading_points( $path[-2], $path[-1] ) if $#path > 1; + + my $type = 1; + + my @curve = $curve_type ? curve_catmull_rom : curve_bezier; debug 'curve' => @curve; my $i = 0; @@ -149,9 +164,10 @@ sub curve { sub { $app->pixel( @_, $path_color ) } ); } + $app->sync; + push @airplanes, Airplane->new( $app ); $airplanes[-1]->set_path( @path ); - $app->sync; reset_path; } @@ -180,7 +196,6 @@ sub handle_events { } elsif ( $type == SDL_MOUSEBUTTONUP() ) { debug 'mouse up', $event->button_x, $event->button_y; $mouse_down = 0; - curve; } elsif ( $type == SDL_QUIT() ) { exit; @@ -199,6 +214,8 @@ sub handle_events { $current_mask++; $current_mask = 0 if $current_mask > $#masks; clear_screen $current_mask; + } elsif ( $key eq 't' ) { # XXX curve type + $curve_type = ! $curve_type; } else { warn "unknown key $key"; }