NAME

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

VERSION

version 1.101330

SYNOPSIS

my $v1 = Games::RailRoad::Vector->new( \%params );

DESCRIPTION

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

ATTRIBUTES

posx

The x coordinate of the vector. Default to 0.

posy

The y coordinate of the vector. Default to 0.

METHODS

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 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;

AUTHOR

Jerome Quelin

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.