draw curve automatically
authorDobrica Pavlinusic <dpavlin@rot13.org>
Wed, 19 May 2010 13:37:16 +0000 (15:37 +0200)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Wed, 19 May 2010 13:37:16 +0000 (15:37 +0200)
trace-path.pl

index 5884405..61a4066 100755 (executable)
@@ -15,6 +15,7 @@ use Data::Dump qw/dump/;
 
 my ( $w, $h ) = ( 800, 480 );
 my $mouse_trashold = 10;
+my $max_path_length = 200;
 
 our $app = SDL::App->new(
        -width  => $w,
@@ -34,12 +35,12 @@ our $black = SDL::Color->new( 0x00, 0x00, 0x00 );
 
 my ( $last_x, $last_y ) = ( 0,0 );
 
-my $max_path_length = 100;
 my @path;
 
 sub curve {
+       return unless $#path > 4;
        my $curve = Math::CatmullRom->new( splice @path, 0, $#path + $#path / 2 );
-       my @curve = $curve->curve( $mouse_trashold * $max_path_length );
+       my @curve = $curve->curve( $mouse_trashold * $max_path_length / 2 );
        warn "curve ", dump @curve;
 
        my $i = 0;
@@ -61,6 +62,7 @@ sub handle_events {
                } elsif ( $type == SDL_MOUSEBUTTONUP() ) {
                        warn "mouse up ", $event->button_x, ' ', $event->button_y;
                        $mouse_down = 0;
+                       curve;
                } elsif ( $type == SDL_QUIT() ) {
                        exit;
                } elsif ( $type == SDL_KEYDOWN() ) {
@@ -93,6 +95,7 @@ sub handle_events {
                                        $last_y = $y;
                                } else {
                                        $mouse_down = 0;
+                                       curve;
                                }
                        }
                } else {