++ed by:
David Farrell

NAME

Math::Shape::Vector - A 2d vector library in cartesian space

version 0.15

SYNOPSIS

``````    use Math::Shape::Vector;

my \$v1 = Math::Shape::Vector->new(3, 5);
my \$v2 = Math::Shape::Vector->new(1, 17);

\$v1->negate;
\$v1->multiply(5);
\$v1->is_equal(\$v2);``````

DESCRIPTION

This module contains 2d vector-based objects intended as base classes for 2d games programming. Most of the objects have collision detection (among other methods). All objects are immutable in so far as their methods return new objects everytime. The objects available are:

METHODS

new

Create a new vector. Requires two numerical arguments for the origin and magnitude.

``    my \$vector = Math::Shape::Vector->new(3, 5);``

Adds a vector to the vector object, returning a new vector object with the resulting x & y values.

``    my \$new_vector = \$vector->add_vector(\$vector_2);``

subtract_vector

Subtracts a vector from the vector object, returning a new vector object with the resulting x & y values.

``    my \$new_vector = \$vector->subtract_vector(\$vector_2);``

negate

Returns a new vector with negated values values e.g. (1,3) becomes (-1, -3).

``    my \$new_vector = \$vector->negate();``

is_equal

Compares a vector to the vector object, returning 1 if they are the same or 0 if they are different.

``    \$vector->is_equal(\$vector_2);``

multiply

Returns a new vector object with the x and y values multiplied by a number.

``    my \$new_vector = \$vector->multiply(3);``

divide

Returns a new vector object with the x and y values divided by a number.

``    my \$new_vector = \$vector->divide(2);``

rotate

Returns a new vector with the x and y values rotated in radians.

``````    use Math::Trig ':pi';

my \$new_vector = \$vector->rotate(pi);``````

rotate_90

Returns a new vector object with the x and y values rotated 90 degrees anti-clockwise.

``    my \$new_vector = \$vector->rotate_90;``

dot_product

Returns the dot product. Requires another Math::Shape::Vector object as an argument.

length

Returns the vector length.

``    my \$length = \$vector->length;``

Useful if you want to calculate the distance between two vectors:

``````    my \$vector_c = \$vector_a->subtract_vector(\$vector_b);
my \$distance_a_to_b = \$vector_c->length;``````

convert_to_unit_vector

Returns a new vector object with a length of 1 (aka a normalized vector).

``    my \$unit_vector = \$vector->convert_to_unit_vector;``

project

Maps the vector to another vector, returning a new vector object. Requires a Math::Shape::Vector object as an argument.

``    my \$new_vector = \$vector->project(\$vector_2);``

is_parallel

Boolean method that returns 1 if the vector is parallel with another vector else returns zero. Requires a Math::Shape::Vector object as an argument.

``````    my \$v2 = Math::Shape::Vector(1, 2);

if (\$v->is_parallel(\$v2))
{
# do something
}``````

enclosed_angle

Returns the enclosed angle of another vector. Requires a Math::Shape::Vector object as an argument.

``````    my \$v2 = Math::Shape::Vector(4, 2);
my \$enclosed_angle = \$v->enclosed_angle(\$v2);``````

Returns the angle of the vector expressed in radians (0 - 2 pi).

``    \$vector->radians;``

Returns a unit (normalized) vector representing the direction towards another vector. This header_vector can be converted into radians using the `radians` method. Requires an Math::Shape::Vector object as an argument.

``````    my \$header_vector = \$vector_a->header_vector(\$vector_b);

collides

Boolean method that returns 1 if the vector collides with another Math::Shape::Vector library object or not or 0 if not. Requires a Math::Shape::Vectorlibrary object as an argument

``````    my \$v1 = Math::Shape::Vector(4, 2);
my \$v2 = Math::Shape::Vector(4, 2);

\$v1->collides(\$v2); # 1

my \$circle = Math::Shape::Circle->new(0, 0, 3); # x, y and radius
\$v1->collides(\$circle); # 0``````

distance

Returns the distance from the vector to the nearest point of another shape. Requires an Math::Shape::Vector library object as an argument. Currently only implemented for vector and circle objects. For OrientedRectangle objects, distance uses the distance to the circle hull of the OrientedRectangle (not completely accurate).

``    my \$distance = \$vector->distance(\$other_vector);``

REPOSITORY

https://github.com/sillymoose/Math-Shape-Vector.git

THANKS

The source code for this class was inspired by the code in Thomas Schwarzl's 2d collision detection book http://www.collisiondetection2d.net.

AUTHOR

David Farrell <dfarrell@cpan.org>