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

NAME

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

VERSION

version 0.06

SYNOPSIS

    use Math::Shape::Vector;

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

    $v1->add_vector($v2);
    $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). The objects available are:

Coverage Status

METHODS

new

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

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

add_vector

Adds a vector to the vector object, updating its x & y values.

    $vector->add_vector($vector_2);

subtract_vector

Subtracts a vector from the vector object, updating its x & y values.

    $vector->subtract_vector($vector_2);

negate

Negates the vector's values e.g. (1,3) becomes (-1, -3).

    $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

Multiplies the vector's x and y values by a number.

    $vector->multiply(3);

divide

Divides the vector's x and y values by a number.

    $vector->divide(2);

rotate

Rotates the vector in radians.

    use Math::Trig ':pi';

    $vector->rotate(pi);

rotate_90

Rotates the vector 90 degrees anti-clockwise

dot_product

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

length

Returns the vector length.

    $vector->length;

convert_to_unit_vector

Converts the vector to have a length of 1.

    $vector->convert_to_unit_vector;

project

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

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

collides

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

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

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

REPOSITORY

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

THANKS

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

AUTHOR

David Farrell <sillymoos@cpan.org>

COPYRIGHT AND LICENSE

This software is copyright (c) 2014 by David Farrell.

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