The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Games::RailRoad::Vector - an opaque vector class.

SYNOPSIS

    my $v1 = Games::RailRoad::Vector->new(...);

DESCRIPTION

This class abstracts basic vector manipulation. It lets you pass around one argument to your functions, do vector arithmetic and various string representation.

CONSTRUCTORS

my $vec = GR::Vector->new( $x, $y );

Create a new vector. The arguments are the x and y coordinates.

my $vec = GR::Vector->new_dir( $dir );

Create a new vector, from a given direction. The recognized directions are e, n, ne, nw, s, se, sw, w.

my $vec = $v->copy;

Return a new GRV object, which has the same coordinates as $v.

PUBLIC METHODS

my $str = $vec->as_string;

Return the stringified form of $vec. For instance, a Befunge vector might look like (1,2).

This method is also applied to stringification, ie when one forces string context ("$vec").

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).

MATHEMATICAL OPERATIONS

Standard operations

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 $v2.

The inversion is also supported: my $v2 = -$v1;

will subtracts $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.

Inplace operations

GRV objects also supports inplace mathematical operations:

    $v1 += $v2;
    $v1 -= $v2;

effectively adds / substracts $v2 to / from $v1, and stores the result back into $v1.

Comparison

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;

SEE ALSO

Games::RailRoad

AUTHOR

Jerome Quelin, <jquelin@cpan.org>

COPYRIGHT & LICENSE

Copyright (c) 2001-2008 Jerome Quelin, all rights reserved.

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.