https://github.com/dnmfarrell/math-shape-vector
2d vector library in cartesian space
https://github.com/dnmfarrell/math-shape-vector
Last synced: 1 day ago
JSON representation
2d vector library in cartesian space
- Host: GitHub
- URL: https://github.com/dnmfarrell/math-shape-vector
- Owner: dnmfarrell
- License: other
- Created: 2014-09-01T16:59:02.000Z (almost 12 years ago)
- Default Branch: master
- Last Pushed: 2015-10-01T18:14:21.000Z (over 10 years ago)
- Last Synced: 2025-10-14T16:07:21.523Z (8 months ago)
- Language: Perl
- Homepage: https://metacpan.org/pod/Math::Shape::Vector
- Size: 281 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.pod
- Changelog: Changes
- License: LICENSE
Awesome Lists containing this project
README
=pod
=encoding UTF-8
=head1 NAME
Math::Shape::Vector - A 2d vector library in cartesian space
=head1 VERSION
version 0.15
=head1 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);
=head1 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:
=over
=item *
L - a 2d vector (this package)
=item *
L - an infinite 2d line
=item *
L - a finite 2d line (with a start and end)
=item *
L - a number range (e.g 1 through 20)
=item *
L - a 2d Circle
=item *
L - a 2d axis-oriented rectangle
=item *
L - a 2d oriented rectangle
=back
=head1 METHODS
=head2 new
Create a new vector. Requires two numerical arguments for the origin and magnitude.
my $vector = Math::Shape::Vector->new(3, 5);
=head2 add_vector
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);
=head2 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);
=head2 negate
Returns a new vector with negated values values e.g. (1,3) becomes (-1, -3).
my $new_vector = $vector->negate();
=head2 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);
=head2 multiply
Returns a new vector object with the x and y values multiplied by a number.
my $new_vector = $vector->multiply(3);
=head2 divide
Returns a new vector object with the x and y values divided by a number.
my $new_vector = $vector->divide(2);
=head2 rotate
Returns a new vector with the x and y values rotated in radians.
use Math::Trig ':pi';
my $new_vector = $vector->rotate(pi);
=head2 rotate_90
Returns a new vector object with the x and y values rotated 90 degrees anti-clockwise.
my $new_vector = $vector->rotate_90;
=head2 dot_product
Returns the dot product. Requires another Math::Shape::Vector object as an argument.
=head2 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;
=head2 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;
=head2 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);
=head2 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
}
=head2 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);
=head2 radians
Returns the angle of the vector expressed in radians (0 - 2 pi).
$vector->radians;
=head2 header_vector
Returns a unit (normalized) vector representing the direction towards another vector. This header_vector can be converted into radians using the C method. Requires an L object as an argument.
my $header_vector = $vector_a->header_vector($vector_b);
my $radians_a_to_b = $header_vector->radians;
=head2 collides
Boolean method that returns 1 if the vector collides with another L 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
=head2 distance
Returns the distance from the vector to the nearest point of another shape. Requires an L 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);
=head1 REPOSITORY
L
=head1 THANKS
The source code for this class was inspired by the code in Thomas Schwarzl's 2d collision detection book L.
=head1 AUTHOR
David Farrell
=head1 COPYRIGHT AND LICENSE
This software is copyright (c) 2015 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.
=cut
