Vector.pm 0.05 - Simple vector stuff
use Vector;
$a = new Vector ( [3,4] );
$b = new Vector ( [1,0] );
$ax = $a->dot($b);
$c = $a->cross($b);
print $c->cross($b)->times(3)->as_string;
You can obtain Vector.pm from
http://fulcrum.org/projects/polyplus/Vector.pm
A small class for doing things with vectors. Mostly experimental--CPAN has much higher powered stuff in Math::Pari and PDL.
new( [1,0,0] ) class/object method
To create a new vector, send the ``new'' message to the Vector class, or call it against an existing vector. Initialize its elements with a reference to an array.
component( $index [, $value]) object method
If a $value is supplied, sets the $index-th component of the
vector to that value. Otherwise, returns the existing value. Will grow the
vector to accommodate if you assign off the end.
$a = new Vector( [1,0,2] );
$a_sub_x = $a->component(0);
$a->component(1,5);
length object method
Returns the length of the vector.
magnitude object method
Returns the magnitude of the vector (the square root of the sum of the squares of the components).
times( ) object method
Returns the vector resulting from scaling the current vector by the indicated amount.
$b = $a->times(3);
plus( $another_vector ) object method
Returns the (vector) sum of the current vector and the argument:
$sum = $a->plus($b);
dot( $another_vector ) object method
Returns the dot product of the current vector and the argument:
$square_mag = $a->dot($a);
projected_onto( $another_vector ) object method
Returns the vector ``shadow'' of the current vector projected onto the argument:
$x_part = $a->projected_onto([1,0,0]);
The argument, as in this example, can be an array reference rather than a vector.
cross( $another_vector ) object method
Returns the (vector) cross product of the object and the argument.
$k = $i->cross($j);
normalized object method
Returns a vector of magnitude one pointing in the same direction as the object:
$n = $b->normalized;
as_array object method
Returns the elements of the vector as an array:
@dir_cos = $n->as_array;
as_string object method
Returns a string of the form ``<1,2>''.
print $n->as_string;
print $a->cross($b)->normalized->times(3)->as_string;
rotate ($angle, $axis | [$x,$y,$z] ) object method
Returns the vector obtained by rotating the current vector through
$angle radians about the vector $axis (or, if an
array ref is given, a vector which goes through those points). The
$axis need not be normalized (likewise $x, $y, $z
can be any point).
$pi = 4 * atan2(1,1);
$a = new Vector ( [1,0,0] ) ;
print $a->rotate( 2*$pi/3, [1,1,1] )->as_string ;
See http://wwwinfo.cern.ch/asd/lhc++/clhep/manual/UserGuide/Vector/vector.html or http://freeabel.geom.umn.edu/docs/reference/CRC-formulas/node45.html for more information.
tilt($from_here, $to_here) object method
Take the given vector, apply the transformation to it which would rotate
the $from_here vector so that it would be aligned with $to_here, and return the result. (For example, if your camera is at $from_here, you can just tilt all your vectors with ($from_here, [0,0,1]) and then hand off the x- and y- components of those vectors to a renderer
to get the picture from that angle.)