Math-Zap

 view release on metacpan or  search on metacpan

lib/Math/Zap/Vector2.pm  view on Meta::CPAN

=cut


sub new($$)
 {return bless {x=>$_[0], y=>$_[1]} unless debug;
  my ($x, $y) = @_; 
  round(bless({x=>$x, y=>$y})); 
 }


=head3 vector2

Create a vector from numbers - synonym for L</new>

=cut


sub vector2($$) {new($_[0],$_[1])}


=head3 units

Unit vectors                                        

=cut


$x = new(1,0);
$y = new(0,1);

sub units() {($x, $y)}


=head2 Methods


=head3 check

Check its a vector

=cut


sub check(@)
 {if (debug)
   {for my $v(@_)
     {confess "$v is not a vector2" unless ref($v) eq __PACKAGE__;
     }
   }
  return (@_)
 }


=head3 is

Test its a vector

=cut


sub is(@)
 {for my $v(@_)
   {return 0 unless ref($v) eq __PACKAGE__;
   }
  1;
 }


=head3 accuracy

Get/Set accuracy for comparisons

=cut


my $accuracy = 1e-10;

sub accuracy
 {return $accuracy unless scalar(@_);
  $accuracy = shift();
 }


=head3 round

Round: round to nearest integer if within accuracy of that integer 

=cut


sub round($)
 {unless (debug)
   {return $_[0];
   }
  else
   {my ($a) = @_;
    for my $k(keys(%$a))
     {my $n = $a->{$k};
      my $N = int($n);
      $a->{$k} = $N if abs($n-$N) < $accuracy;
     }
    return $a;
   }
 }


=head3 components

x,y components of vector

=cut


sub x($) {check(@_) if debug; $_[0]->{x}}
sub y($) {check(@_) if debug; $_[0]->{y}}


=head3 clone

Create a vector from another vector



( run in 0.800 second using v1.01-cache-2.11-cpan-524268b4103 )