use Math::CatmullRom to draw spline through points
authorDobrica Pavlinusic <dpavlin@rot13.org>
Wed, 19 May 2010 13:29:29 +0000 (15:29 +0200)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Wed, 19 May 2010 13:29:29 +0000 (15:29 +0200)
trace-path.pl

index 9a27b35..5884405 100755 (executable)
@@ -8,6 +8,7 @@ use SDL::Rect;
 use SDL::Color;
 use SDL::Constants;
 use SDL::Event;
 use SDL::Color;
 use SDL::Constants;
 use SDL::Event;
+use Math::CatmullRom;
 
 use Carp qw/confess/;
 use Data::Dump qw/dump/;
 
 use Carp qw/confess/;
 use Data::Dump qw/dump/;
@@ -28,9 +29,27 @@ our $event = SDL::Event->new;
 our $mouse_down = 0;
 
 our $white = SDL::Color->new( 0xff, 0xff, 0xff );
 our $mouse_down = 0;
 
 our $white = SDL::Color->new( 0xff, 0xff, 0xff );
+our $red   = SDL::Color->new( 0xff, 0x00, 0x00 );
+our $black = SDL::Color->new( 0x00, 0x00, 0x00 );
 
 my ( $last_x, $last_y ) = ( 0,0 );
 
 
 my ( $last_x, $last_y ) = ( 0,0 );
 
+my $max_path_length = 100;
+my @path;
+
+sub curve {
+       my $curve = Math::CatmullRom->new( splice @path, 0, $#path + $#path / 2 );
+       my @curve = $curve->curve( $mouse_trashold * $max_path_length );
+       warn "curve ", dump @curve;
+
+       my $i = 0;
+       while ( $i < $#curve ) {
+               my $rect = SDL::Rect->new( -x => int($curve[$i++]), -y => int($curve[$i++]), -w => 1, -h => 1 );
+               $app->fill( $rect, $red );
+               $app->update( $rect );
+       }
+}
+
 sub handle_events {
 
        while ( $event->wait ) {
 sub handle_events {
 
        while ( $event->wait ) {
@@ -45,8 +64,17 @@ sub handle_events {
                } elsif ( $type == SDL_QUIT() ) {
                        exit;
                } elsif ( $type == SDL_KEYDOWN() ) {
                } elsif ( $type == SDL_QUIT() ) {
                        exit;
                } elsif ( $type == SDL_KEYDOWN() ) {
-                       warn "key down ", $event->key_name,$/;
-                       exit if $event->key_name =~ m/^[xq]$/;
+                       my $key = $event->key_name;
+                       warn "key down $key\n";
+                       exit if $key =~ m/^[xq]$/;
+                       if ( $key eq 's' ) {
+                               curve;
+                       } elsif ( $key eq 'd' ) {
+                               @path = ();
+                               my $rect = SDL::Rect->new( -x => 0, -y => 0, -w => $w, -h => $h );
+                               $app->fill( $rect, $black );
+                               $app->update( $rect );
+                       }
                } elsif ( $type == SDL_KEYUP() ) {
                        warn "key up ", $event->key_name,$/;
                } elsif ( $type == SDL_MOUSEMOTION() ) {
                } elsif ( $type == SDL_KEYUP() ) {
                        warn "key up ", $event->key_name,$/;
                } elsif ( $type == SDL_MOUSEMOTION() ) {
@@ -56,11 +84,16 @@ sub handle_events {
                        my $dx = abs( $last_x - $x );
                        my $dy = abs( $last_y - $y );
                        if ( $mouse_down && ( $dx > $mouse_trashold || $dy > $mouse_trashold ) ) {
                        my $dx = abs( $last_x - $x );
                        my $dy = abs( $last_y - $y );
                        if ( $mouse_down && ( $dx > $mouse_trashold || $dy > $mouse_trashold ) ) {
-                               my $rect = SDL::Rect->new( -x => $event->motion_x, -y => $event->motion_y, -w => 3, -h => 3 );
-                               $app->fill( $rect, $white );
-                               $app->update( $rect );
-                               $last_x = $x;
-                               $last_y = $y;
+                               if ( $#path < $max_path_length ) {
+                                       push @path, $x, $y;
+                                       my $rect = SDL::Rect->new( -x => $event->motion_x, -y => $event->motion_y, -w => 3, -h => 3 );
+                                       $app->fill( $rect, $white );
+                                       $app->update( $rect );
+                                       $last_x = $x;
+                                       $last_y = $y;
+                               } else {
+                                       $mouse_down = 0;
+                               }
                        }
                } else {
                        warn "unknown $type\n";
                        }
                } else {
                        warn "unknown $type\n";