Name

Math::Vectors2 - Vectors in two dimensions

Synopsis

use Math::Vectors2;

my ($o, $x, $y) = Math::Vectors2::zeroAndUnits;
ok $o->print($x, $y) eq '(0,0), (1,0), (0,1)';

my $p1 = $x->times(3);
my $p2 = $y->times(4);
my $p  = $o->plus($p1, $p2);

ok $p->print($p1, $p2) eq '(3,4), (3,0), (0,4)';
ok $o->d($p) == 5;

Or more briefly:

use Math::Vectors2;

*v = *Math::Vectors2::new;

ok v(3,4)->l == 5;

Description

The following sections describe the methods in each functional area of this module. For an alphabetic listing of all methods by name see Index.

Attributes

Attributes that can be set by =

x :lvalue

X component of vector.

y :lvalue

Y component of vector.

Methods

Vector msethods.

new($$)

Create new vector from components.

1  $x  X component
2  $y  Y component

Example:

ok $o->print($x, $y) eq '(0,0), (1,0), (0,1)';

ok near2(Math::Vectors2::new(0, 0), $o);

zeroAndUnits()

Create the useful vectors: o=(0,0), x=(1,0), y=(0,1)

Example:

my ($o, $x, $y) = Math::Vectors2::zeroAndUnits;

ok $o->print($x, $y) eq '(0,0), (1,0), (0,1)';

print($@)

Print one or more vectors.

1  $p  Vector to print
2  @p  More vectors to print

Example:

ok $o->print($x, $y) eq '(0,0), (1,0), (0,1)';

values($)

Return components of a vector as a list.

1  $p  Vector

Example:

my ($x, $y) = Math::Vectors2::new(3, 4)->values;

ok $x == 3 && $y == 4;

clone($)

Clone a vector.

1  $o  Vector to clone

Example:

ok $o->print($P, $p1, $p2) eq '(0,0), (3,4), (3,0), (0,4)';

my $p = $P->clone;

ok $p->print($P) eq '(3,4), (3,4)';

plus($@)

Add zero or more other vectors to a copy of the first vector and return the result.

1  $o  First vector
2  @p  Other vectors

Example:

my $P = $o->plus($p1, $p2);

ok $o->print($P, $p1, $p2) eq '(0,0), (3,4), (3,0), (0,4)';

minus($@)

Subtract zero or more vectors from a copy of the first vector and return the result.

1  $o  First vector
2  @p  Other vectors

Example:

ok $o->print($p, $p1, $p2) eq '(0,0), (3,4), (3,0), (0,4)';

ok near2($o, $p->minus($p1, $p2));

times($$)

Multiply a copy of a vector by a scalar and return the result.

1  $o  Vector
2  $m  Scalar to multiply by

Example:

ok $o->print($x, $y) eq '(0,0), (1,0), (0,1)';

my $p1 = $x->times(3);

my $p2 = $y->times(4);

ok $o->print($P, $p1, $p2) eq '(0,0), (3,4), (3,0), (0,4)';

l($)

Length of a vector.

1  $o  Vector

Example:

ok $p->print($P) eq '(3,4), (3,4)';

ok $p->l == 5;

l2($)

Length squared of a vector.

1  $o  Vector

Example:

ok near1($x->plus($y)->l2, 2);

d($$)

Distance between the points identified by two vectors when placed on the same point.

1  $o  Vector 1
2  $p  Vector 2

Example:

ok $o->print($p) eq '(0,0), (3,4)';

ok $o->d($p) == 5;

d2($$)

Distance squared between the points identified by two vectors when placed on the same point.

1  $o  Vector 1
2  $p  Vector 2

Example:

ok $o->print($x, $y) eq '(0,0), (1,0), (0,1)';

ok near1($x->d2($y), 2);

ok near1($x->plus($x)->d2($y), 5);

n($)

Return a normalized a copy of a vector.

1  $o  Vector

Example:

ok $p->l == 5;

ok near1($p->n->l, 1);

dot($$)

Dot product of two vectors.

1  $o  Vector 1
2  $p  Vector 2

Example:

ok $o->print($x, $y) eq '(0,0), (1,0), (0,1)';

ok near1($x->dot($y), 0);

area($$)

Signed area of the parallelogram defined by the two vectors. The area is negative if the second vector appears to the right of the first if they are both placed at the origin and the observer stands against the z-axis in a left handed coordinate system.

1  $o  Vector 1
2  $p  Vector 2

Example:

ok near1($x->plus($y)->area($x->times(2)), -2);

ok near1($x->plus($y)->area($y->times(2)), +2);

cos($$)

cos(angle between two vectors) in radians.

1  $o  Vector 1
2  $p  Vector 2

Example:

ok near1($x->cos($x), +1);

ok zero1($x->cos($y));

sin($$)

sin(angle between two vectors) in radians.

1  $o  Vector 1
2  $p  Vector 2

Example:

ok zero1($x->sin($x));

ok near1($x->sin($y), +1);

r90($)

Rotate a vector by 90 degrees anticlockwise.

1  $o  Vector to rotate

Example:

ok near2($x->r90, $y);

ok near2($y->r90, $o->minus($x));

r180($)

Rotate a vector by 180 degrees.

1  $o  Vector to rotate

Example:

ok near2($x->r90->r90, $x->r180);

r270($)

Rotate a vector by 270 degrees anticlockwise.

1  $o  Vector to rotate

Example:

ok near2($x->r90->r90->r90, $x->r270);

Index

1 area

2 clone

3 cos

4 d

5 d2

6 dot

7 l

8 l2

9 minus

10 n

11 new

12 plus

13 print

14 r180

15 r270

16 r90

17 sin

18 times

19 values

20 x

21 y

22 zeroAndUnits

Installation

This module is written in 100% Pure Perl and, thus, it is easy to read, use, modify and install.

Standard Module::Build process for building and installing modules:

perl Build.PL
./Build
./Build test
./Build install

Author

philiprbrenan@gmail.com

http://www.appaapps.com

Copyright

Copyright (c) 2016-2017 Philip R Brenan.

This module is free software. It may be used, redistributed and/or modified under the same terms as Perl itself.