Jerome Quelin >
Games-RailRoad-1.101330 >
Games::RailRoad::Vector

Module Version: 1.101330
Games::RailRoad::Vector - an opaque vector class.

version 1.101330

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.

Create and return a new vector. Accept a hash reference with the attribute values.

Create a new vector, from a given direction. The recognized directions are `e`

, `n`

, `ne`

, `nw`

, `s`

, `se`

, `sw`

, `w`

.

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

.

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"`

).

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

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`

.

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;

Jerome Quelin

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.

syntax highlighting: