- PUBLIC METHODS
- MATHEMATICAL OPERATIONS
- COPYRIGHT AND LICENSE
Games::RailRoad::Vector - an opaque vector class.
my $v1 = Games::RailRoad::Vector->new( \%params );
This class abstracts basic vector manipulation. It lets you pass around one argument to your functions, do vector arithmetic and various string representation.
The x coordinate of the vector. Default to 0.
The y coordinate of the vector. Default to 0.
my $vec = GR::Vector->new( \%params );
Create and return a new vector. Accept a hash reference with the attribute values.
my $vec = GR::Vector->new_dir( $dir );
Create a new vector, from a given direction. The recognized directions are
my $vec = $v->copy;
Return a new GRV object, which has the same coordinates as
my $str = $vec->as_string;
Return the stringified form of
$vec. For instance, a Befunge vector might look like
This method is also applied to stringification, ie when one forces string context (
my $str = $vec->as_dir;
Return the cardinal direction (n, sw, etc.) of $vec if it's a unit vector (ok, (1,1) is not a unit vector but you see my point).
One can do some maths on the vectors. Addition and substraction work as expected:
my $v = $v1 + $v2; my $v = $v1 - $v2;
Either operation return a new GRV object, which is the result of
$v1 plus / minus
The inversion is also supported: my $v2 = -$v1;
$v1 from the origin, and effectively, gives the inverse of the original vector. The new vector is the same distance from the origin, in the opposite direction.
GRV objects also supports inplace mathematical operations:
$v1 += $v2; $v1 -= $v2;
effectively adds / substracts
$v2 to / from
$v1, and stores the result back into
Finally, GRV objects can be tested for equality, ie whether two vectors both point at the same spot.
print "same" if $v1 == $v2; print "differ" if $v1 != $v2;
COPYRIGHT AND LICENSE
This software is copyright (c) 2008 by Jerome Quelin.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.