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

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->add($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.

Methods

Methods

x :lvalue

X component of vector

y :lvalue

Y component of vector

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

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

add($@)

Add zero or more other vectors to the first vector

  1  $o  First vector
  2  @p  Other vectors

Example:

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

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

minus($@)

Subtract zero or more vectors from the first vector

  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 vector by a scalar

  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;

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;

Index

1 add

2 clone

3 d

4 l

5 minus

6 new

7 print

8 times

9 x

10 y

11 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.