NAME

Language::Befunge::Vector - an opaque, N-dimensional vector class.

SYNOPSIS

my $v1 = Language::Befunge::Vector->new($d, $x, $y, ...);
my $v2 = Language::Befunge::Vector->new_zeroes($d);

DESCRIPTION

This class abstracts normal vector manipulation. It lets you pass around one argument to your functions, rather than N arguments, one per dimension. This means much of your code doesn't have to care how many dimensions you're working with.

You can do vector arithmetic, test for equality, or even stringify the vector to a string like "(1,2,3)".

CONSTRUCTORS

new( dimensions, x, [y, ...] )

Creates a new vector. The first argument is an integer specifying how many dimensions this vector operates in. The remaining arguments constitute the actual vector data; one integer per dimension.

new_zeroes( dimensions )

Creates a new vector, set to the origin (all zeroes). ->new_zeroes(2) is exactly equivalent to ->new(2, 0, 0).

PUBLIC METHODS

get_dims( )

Returns the number of dimensions, an integer.

vector_subtract( v2 )

$v0 = $v1->vector_subtract($v2);
$v0 = $v1 - $v2;

Returns a new Vector object, which is the result of v1 minus v2.

vector_invert( )

$v1->vector_invert();
$v2 = -$v1;

Subtracts v1 from the origin. Effectively, gives the inverse of the original vector. The new vector is the same distance from the origin, in the opposite direction.

vector_add( v2 )

$v0 = $v1->vector_add($v2);
$v0 = $v1 + $v2;

Returns a new Vector object, which is the result of v1 plus v2.

vector_add_inplace( v2 )

$v1->vector_add_inplace($v2);
$v1 += $v2;

Adds v2 to v1, and stores the result back into v1.

vector_copy( )

$v0 = $v1->vector_copy();
$v0 = $v1;

Returns a new Vector object, which has the same dimensions and coordinates as v1.

set_component( dimension, data )

$v->set_component(0, 1); # set X to 1

Sets the value for dimension dimension to the value data.

get_component( dimension )

my $x = $v->get_component(0);

Gets the value for dimension dimension.

get_all_components( )

my $v = Language::Befunge::Vector->new(3, 1, 2, 3);
# $v now holds a 3-dimensional vector, <1,2,3>
my @list = $v->get_all_components(); # returns (1, 2, 3)

Gets the value for all dimensions, in order from 0..N.

zero( )

Sets the vector back to the origin, all 0's. See also the constructor, new_from_origin, above.

bounds_check( begin, end )

die "out of bounds"
    unless $vector->bounds_check($begin, $end);

Checks whether the given vector is within the box defined by begin and end. Returns 1 if vector is contained within the box, and 0 otherwise.

vector_as_string( )

Returns the stringified form of the vector. For instance, a Befunge vector might look like "(1,2)".

vector_equality( v2 )

print("Equal!\n") if $v1->vector_equality($v2);
print("Equal!\n") if $v1 == $v2;

Checks whether the vectors both point at the same spot. Returns 1 if they do, 0 if they don't.

vector_inequality( v2 )

print("Equal!\n") unless $v1->vector_inequality($v2);
print("Equal!\n") unless $v1 != $v2;

Checks whether the vectors point to different spots. Returns 1 if they don't, 0 if they do. Compare vector_equality, above.

SEE ALSO

Language::Befunge

AUTHOR

Jerome Quelin, <jquelin@cpan.org>

Development is discussed on <language-befunge@mongueurs.net>

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.