Name
Math::Vectors2 - Vectors in two dimensions
Synopsis
use Math::Vectors2;
my ($zero, $x, $y) = Math::Vectors2::zeroAndUnits;
ok near deg2rad(-60), $x + $y * sqrt(3) < $x;
ok near deg2rad(+30), ($x + $y * sqrt(3))->angle($y);
Description
Vectors in two dimensions
Version 20200402.
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
Vector methods.
new($x, $y)
Create new vector from components.
Parameter Description
1 $x X component
2 $y Y component
Example:
my ($zero, $x, $y) = zeroAndUnits;
ok near $y->angle(𝗻𝗲𝘄(+1, -1)), deg2rad(-135);
ok near $y->angle(𝗻𝗲𝘄(+1, 0)), deg2rad(-90);
ok near $y->angle(𝗻𝗲𝘄(+1, +1)), deg2rad(-45);
ok near $y->angle(𝗻𝗲𝘄( 0, +1)), deg2rad(+0);
ok near $y->angle(𝗻𝗲𝘄(-1, +1)), deg2rad(+45);
ok near $y->angle(𝗻𝗲𝘄(-1, 0)), deg2rad(+90);
ok near $y->angle(𝗻𝗲𝘄(-1, -1)), deg2rad(+135);
ok near 𝗻𝗲𝘄(1,1) < 𝗻𝗲𝘄( 0, -1), deg2rad(-135);
ok near 𝗻𝗲𝘄(1,1) < 𝗻𝗲𝘄( 1, -1), deg2rad(-90);
ok near 𝗻𝗲𝘄(1,1) < 𝗻𝗲𝘄( 1, 0), deg2rad(-45);
ok near 𝗻𝗲𝘄(1,1) < 𝗻𝗲𝘄( 1, 1), deg2rad(0);
ok near 𝗻𝗲𝘄(1,1) < 𝗻𝗲𝘄( 0, 1), deg2rad(+45);
ok near 𝗻𝗲𝘄(1,1) < 𝗻𝗲𝘄(-1, 1), deg2rad(+90);
ok near 𝗻𝗲𝘄(1,1) < 𝗻𝗲𝘄(-1, 0), deg2rad(+135);
ok near $x + $y * sqrt(3) < $x, deg2rad(-60);
ok near $x + $y * sqrt(3) < $y, deg2rad(+30);
for my $i(-179..179)
{ok near $x < 𝗻𝗲𝘄(cos(deg2rad($i)), sin(deg2rad($i))), deg2rad($i);
}
This is a static method and so should either be imported or invoked as:
Math::Vectors2::new
zeroAndUnits()
Create the useful vectors: o=(0,0), x=(1,0), y=(0,1)
Example:
my ($z, $x, $y) = 𝘇𝗲𝗿𝗼𝗔𝗻𝗱𝗨𝗻𝗶𝘁𝘀;
ok $x + $y + $z == $x->plus($y);
ok $x - $y == $x->minus($y);
ok $x * 3 == $x->multiply(3);
ok $y / 2 == $y->divide(2);
ok $x + $y eq '(1,1)';
ok $x - $y eq '(1,-1)';
ok $x * 3 eq '(3,0)';
ok $y / 2 eq '(0,0.5)';
ok (($x * 2 + $y * 3)-> print eq '(2,3)');
This is a static method and so should either be imported or invoked as:
Math::Vectors2::zeroAndUnits
eq($o, $p)
Whether two vectors are equal to within the accuracy of floating point arithmetic
Parameter Description
1 $o First vector
2 $p Second vector
Example:
my ($z, $x, $y) = zeroAndUnits;
ok $x + $y + $z == $x->plus($y);
ok $x - $y == $x->minus($y);
ok $x * 3 == $x->multiply(3);
ok $y / 2 == $y->divide(2);
ok $x + $y 𝗲𝗾 '(1,1)';
ok $x - $y 𝗲𝗾 '(1,-1)';
ok $x * 3 𝗲𝗾 '(3,0)';
ok $y / 2 𝗲𝗾 '(0,0.5)';
ok (($x * 2 + $y * 3)-> print 𝗲𝗾 '(2,3)');
zero($o)
Whether a vector is equal to zero within the accuracy of floating point arithmetic
Parameter Description
1 $o Vector
Example:
my ($𝘇𝗲𝗿𝗼, $x, $y) = zeroAndUnits;
ok $𝘇𝗲𝗿𝗼->𝘇𝗲𝗿𝗼;
ok !$x->𝘇𝗲𝗿𝗼;
ok !$y->𝘇𝗲𝗿𝗼;
print($p, @p)
Print one or more vectors.
Parameter Description
1 $p Vector to print
2 @p More vectors to print
Example:
my ($z, $x, $y) = zeroAndUnits;
ok $x + $y + $z == $x->plus($y);
ok $x - $y == $x->minus($y);
ok $x * 3 == $x->multiply(3);
ok $y / 2 == $y->divide(2);
ok $x + $y eq '(1,1)';
ok $x - $y eq '(1,-1)';
ok $x * 3 eq '(3,0)';
ok $y / 2 eq '(0,0.5)';
ok (($x * 2 + $y * 3)-> 𝗽𝗿𝗶𝗻𝘁 eq '(2,3)');
clone($o)
Clone a vector.
Parameter Description
1 $o Vector to clone
Example:
my ($z, $x, $y) = zeroAndUnits;
ok $x->swap == $y;
ok $x->𝗰𝗹𝗼𝗻𝗲 == $x;
Plus($o, @p)
Add zero or more other vectors to the first vector and return the result.
Parameter Description
1 $o First vector
2 @p Other vectors
Example:
my ($zero, $x, $y) = zeroAndUnits;
$x->𝗣𝗹𝘂𝘀(new(1,1));
ok $x eq '(2,1)';
$y += new(1,1);
ok $y eq '(1,2)';
plus($o, @p)
Add zero or more other vectors to a copy of the first vector and return the result.
Parameter Description
1 $o First vector
2 @p Other vectors
Example:
my ($z, $x, $y) = zeroAndUnits;
ok $x + $y + $z == $x->𝗽𝗹𝘂𝘀($y);
ok $x - $y == $x->minus($y);
ok $x * 3 == $x->multiply(3);
ok $y / 2 == $y->divide(2);
ok $x + $y eq '(1,1)';
ok $x - $y eq '(1,-1)';
ok $x * 3 eq '(3,0)';
ok $y / 2 eq '(0,0.5)';
ok (($x * 2 + $y * 3)-> print eq '(2,3)');
Minus($o, @p)
Subtract zero or more vectors from the first vector and return the result.
Parameter Description
1 $o First vector
2 @p Other vectors
Example:
my ($zero, $x, $y) = zeroAndUnits;
$x->𝗠𝗶𝗻𝘂𝘀(new(0, 1));
ok $x eq '(1,-1)';
$y -= new(1,1);
ok $y eq '(-1,0)';
minus($o, @p)
Subtract zero or more vectors from a copy of the first vector and return the result.
Parameter Description
1 $o First vector
2 @p Other vectors
Example:
my ($z, $x, $y) = zeroAndUnits;
ok $x + $y + $z == $x->plus($y);
ok $x - $y == $x->𝗺𝗶𝗻𝘂𝘀($y);
ok $x * 3 == $x->multiply(3);
ok $y / 2 == $y->divide(2);
ok $x + $y eq '(1,1)';
ok $x - $y eq '(1,-1)';
ok $x * 3 eq '(3,0)';
ok $y / 2 eq '(0,0.5)';
ok (($x * 2 + $y * 3)-> print eq '(2,3)');
Multiply($o, $m)
Multiply a vector by a scalar and return the result.
Parameter Description
1 $o Vector
2 $m Scalar to multiply by
Example:
my ($zero, $x, $y) = zeroAndUnits;
$x->𝗠𝘂𝗹𝘁𝗶𝗽𝗹𝘆(2);
ok $x eq '(2,0)';
$y *= 2;
ok $y eq '(0,2)';
multiply($o, $m)
Multiply a copy of a vector by a scalar and return the result.
Parameter Description
1 $o Vector
2 $m Scalar to multiply by
Example:
my ($z, $x, $y) = zeroAndUnits;
ok $x + $y + $z == $x->plus($y);
ok $x - $y == $x->minus($y);
ok $x * 3 == $x->𝗺𝘂𝗹𝘁𝗶𝗽𝗹𝘆(3);
ok $y / 2 == $y->divide(2);
ok $x + $y eq '(1,1)';
ok $x - $y eq '(1,-1)';
ok $x * 3 eq '(3,0)';
ok $y / 2 eq '(0,0.5)';
ok (($x * 2 + $y * 3)-> print eq '(2,3)');
Divide($o, $d)
Divide a vector by a scalar and return the result.
Parameter Description
1 $o Vector
2 $d Scalar to multiply by
Example:
my ($zero, $x, $y) = zeroAndUnits;
$x->𝗗𝗶𝘃𝗶𝗱𝗲(1/2);
ok $x eq '(2,0)';
$y /= 1/2;
ok $y eq '(0,2)';
divide($o, $d)
Divide a copy of a vector by a scalar and return the result.
Parameter Description
1 $o Vector
2 $d Scalar to divide by
Example:
my ($z, $x, $y) = zeroAndUnits;
ok $x + $y + $z == $x->plus($y);
ok $x - $y == $x->minus($y);
ok $x * 3 == $x->multiply(3);
ok $y / 2 == $y->𝗱𝗶𝘃𝗶𝗱𝗲(2);
ok $x + $y eq '(1,1)';
ok $x - $y eq '(1,-1)';
ok $x * 3 eq '(3,0)';
ok $y / 2 eq '(0,0.5)';
ok (($x * 2 + $y * 3)-> print eq '(2,3)');
l($o)
Length of a vector.
Parameter Description
1 $o Vector
Example:
my ($z, $x, $y) = zeroAndUnits;
ok 5 == ($x * 3 + $y * 4)->𝗹;
ok 25 == ($x * 3 + $y * 4)->l2;
ok 2 * ($x + $y)->𝗹 == ($x + $y)->d (-$x - $y);
ok 4 * ($x + $y)->l2 == ($x + $y)->d2(-$x - $y);
l2($o)
Length squared of a vector.
Parameter Description
1 $o Vector
Example:
my ($z, $x, $y) = zeroAndUnits;
ok 5 == ($x * 3 + $y * 4)->l;
ok 25 == ($x * 3 + $y * 4)->𝗹𝟮;
ok 2 * ($x + $y)->l == ($x + $y)->d (-$x - $y);
ok 4 * ($x + $y)->𝗹𝟮 == ($x + $y)->d2(-$x - $y);
d($o, $p)
Distance between the points identified by two vectors when placed on the same point.
Parameter Description
1 $o Vector 1
2 $p Vector 2
Example:
my ($z, $x, $y) = zeroAndUnits;
ok 5 == ($x * 3 + $y * 4)->l;
ok 25 == ($x * 3 + $y * 4)->l2;
ok 2 * ($x + $y)->l == ($x + $y)->𝗱 (-$x - $y);
ok 4 * ($x + $y)->l2 == ($x + $y)->d2(-$x - $y);
d2($o, $p)
Distance squared between the points identified by two vectors when placed on the same point.
Parameter Description
1 $o Vector 1
2 $p Vector 2
Example:
my ($z, $x, $y) = zeroAndUnits;
ok 5 == ($x * 3 + $y * 4)->l;
ok 25 == ($x * 3 + $y * 4)->l2;
ok 2 * ($x + $y)->l == ($x + $y)->d (-$x - $y);
ok 4 * ($x + $y)->l2 == ($x + $y)->𝗱𝟮(-$x - $y);
n($o)
Return a normalized a copy of a vector.
Parameter Description
1 $o Vector
Example:
my ($z, $x, $y) = zeroAndUnits;
ok (($x * 3 + $y * 4)->𝗻 == $x * 3/5 + $y * 4/5);
ok 0 == $x . $y;
ok 1 == $x . $x;
ok 1 == $y . $y;
ok 8 == ($x * 1 + $y * 2) .($x * 2 + $y * 3);
dot($o, $p)
Dot product of two vectors.
Parameter Description
1 $o Vector 1
2 $p Vector 2
Example:
my ($z, $x, $y) = zeroAndUnits;
ok (($x * 3 + $y * 4)->n == $x * 3/5 + $y * 4/5);
ok 0 == $x . $y;
ok 1 == $x . $x;
ok 1 == $y . $y;
ok 8 == ($x * 1 + $y * 2) .($x * 2 + $y * 3);
area($o, $p)
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.
Parameter Description
1 $o Vector 1
2 $p Vector 2
Example:
my ($z, $x, $y) = zeroAndUnits;
ok +1 == $x->cosine($x);
ok +1 == $y->cosine($y);
ok 0 == $x->cosine($y);
ok 0 == $y->cosine($x);
ok 0 == $x->sine($x);
ok 0 == $y->sine($y);
ok +1 == $x->sine($y);
ok -1 == $y->sine($x);
ok near -sqrt(1/2), ($x + $y)->sine($x);
ok near +sqrt(1/2), ($x + $y)->sine($y);
ok near -2, ($x + $y)->𝗮𝗿𝗲𝗮($x * 2);
ok near +2, ($x + $y)->𝗮𝗿𝗲𝗮($y * 2);
cosine($o, $p)
cos(angle between two vectors)
Parameter Description
1 $o Vector 1
2 $p Vector 2
Example:
my ($z, $x, $y) = zeroAndUnits;
ok +1 == $x->𝗰𝗼𝘀𝗶𝗻𝗲($x);
ok +1 == $y->𝗰𝗼𝘀𝗶𝗻𝗲($y);
ok 0 == $x->𝗰𝗼𝘀𝗶𝗻𝗲($y);
ok 0 == $y->𝗰𝗼𝘀𝗶𝗻𝗲($x);
ok 0 == $x->sine($x);
ok 0 == $y->sine($y);
ok +1 == $x->sine($y);
ok -1 == $y->sine($x);
ok near -sqrt(1/2), ($x + $y)->sine($x);
ok near +sqrt(1/2), ($x + $y)->sine($y);
ok near -2, ($x + $y)->area($x * 2);
ok near +2, ($x + $y)->area($y * 2);
sine($o, $p)
sin(angle between two vectors)
Parameter Description
1 $o Vector 1
2 $p Vector 2
Example:
my ($z, $x, $y) = zeroAndUnits;
ok +1 == $x->cosine($x);
ok +1 == $y->cosine($y);
ok 0 == $x->cosine($y);
ok 0 == $y->cosine($x);
ok 0 == $x->𝘀𝗶𝗻𝗲($x);
ok 0 == $y->𝘀𝗶𝗻𝗲($y);
ok +1 == $x->𝘀𝗶𝗻𝗲($y);
ok -1 == $y->𝘀𝗶𝗻𝗲($x);
ok near -sqrt(1/2), ($x + $y)->𝘀𝗶𝗻𝗲($x);
ok near +sqrt(1/2), ($x + $y)->𝘀𝗶𝗻𝗲($y);
ok near -2, ($x + $y)->area($x * 2);
ok near +2, ($x + $y)->area($y * 2);
angle($o, $p)
Angle in radians anticlockwise that the first vector must be rotated to point along the second vector normalized to the range: -pi to +pi.
Parameter Description
1 $o Vector 1
2 $p Vector 2
Example:
my ($zero, $x, $y) = zeroAndUnits;
ok near $y->𝗮𝗻𝗴𝗹𝗲(new(+1, -1)), deg2rad(-135);
ok near $y->𝗮𝗻𝗴𝗹𝗲(new(+1, 0)), deg2rad(-90);
ok near $y->𝗮𝗻𝗴𝗹𝗲(new(+1, +1)), deg2rad(-45);
ok near $y->𝗮𝗻𝗴𝗹𝗲(new( 0, +1)), deg2rad(+0);
ok near $y->𝗮𝗻𝗴𝗹𝗲(new(-1, +1)), deg2rad(+45);
ok near $y->𝗮𝗻𝗴𝗹𝗲(new(-1, 0)), deg2rad(+90);
ok near $y->𝗮𝗻𝗴𝗹𝗲(new(-1, -1)), deg2rad(+135);
ok near new(1,1) < new( 0, -1), deg2rad(-135);
ok near new(1,1) < new( 1, -1), deg2rad(-90);
ok near new(1,1) < new( 1, 0), deg2rad(-45);
ok near new(1,1) < new( 1, 1), deg2rad(0);
ok near new(1,1) < new( 0, 1), deg2rad(+45);
ok near new(1,1) < new(-1, 1), deg2rad(+90);
ok near new(1,1) < new(-1, 0), deg2rad(+135);
ok near $x + $y * sqrt(3) < $x, deg2rad(-60);
ok near $x + $y * sqrt(3) < $y, deg2rad(+30);
for my $i(-179..179)
{ok near $x < new(cos(deg2rad($i)), sin(deg2rad($i))), deg2rad($i);
}
r90($o)
Rotate a vector by 90 degrees anticlockwise.
Parameter Description
1 $o Vector to rotate
Example:
my ($z, $x, $y) = zeroAndUnits;
ok $x->𝗿𝟵𝟬 == $y;
ok $y->𝗿𝟵𝟬 == -$x;
ok $x->𝗿𝟵𝟬->𝗿𝟵𝟬 == -$x;
ok $y->𝗿𝟵𝟬->𝗿𝟵𝟬 == -$y;
ok $x->𝗿𝟵𝟬->𝗿𝟵𝟬->𝗿𝟵𝟬 == -$y;
ok $y->𝗿𝟵𝟬->𝗿𝟵𝟬->𝗿𝟵𝟬 == $x;
r180($o)
Rotate a vector by 180 degrees.
Parameter Description
1 $o Vector to rotate
Example:
my ($z, $x, $y) = zeroAndUnits;
ok $x->r90 == $y;
ok $y->r90 == -$x;
ok $x->r90->r90 == -$x;
ok $y->r90->r90 == -$y;
ok $x->r90->r90->r90 == -$y;
ok $y->r90->r90->r90 == $x;
r270($o)
Rotate a vector by 270 degrees anticlockwise.
Parameter Description
1 $o Vector to rotate
Example:
my ($z, $x, $y) = zeroAndUnits;
ok $x->r90 == $y;
ok $y->r90 == -$x;
ok $x->r90->r90 == -$x;
ok $y->r90->r90 == -$y;
ok $x->r90->r90->r90 == -$y;
ok $y->r90->r90->r90 == $x;
swap($o)
Swap the components of a vector
Parameter Description
1 $o Vector
Example:
my ($z, $x, $y) = zeroAndUnits;
ok $x->𝘀𝘄𝗮𝗽 == $y;
ok $x->clone == $x;
Math::Vectors2 Definition
Attributes of a vector
Output fields
x - X coordinate
y - Y coordinate
Index
1 angle - Angle in radians anticlockwise that the first vector must be rotated to point along the second vector normalized to the range: -pi to +pi.
2 area - Signed area of the parallelogram defined by the two vectors.
3 clone - Clone a vector.
4 cosine - cos(angle between two vectors)
5 d - Distance between the points identified by two vectors when placed on the same point.
6 d2 - Distance squared between the points identified by two vectors when placed on the same point.
7 divide - Divide a copy of a vector by a scalar and return the result.
8 Divide - Divide a vector by a scalar and return the result.
9 dot - Dot product of two vectors.
10 eq - Whether two vectors are equal to within the accuracy of floating point arithmetic
11 l - Length of a vector.
12 l2 - Length squared of a vector.
13 Minus - Subtract zero or more vectors from the first vector and return the result.
14 minus - Subtract zero or more vectors from a copy of the first vector and return the result.
15 Multiply - Multiply a vector by a scalar and return the result.
16 multiply - Multiply a copy of a vector by a scalar and return the result.
17 n - Return a normalized a copy of a vector.
18 new - Create new vector from components.
19 Plus - Add zero or more other vectors to the first vector and return the result.
20 plus - Add zero or more other vectors to a copy of the first vector and return the result.
21 print - Print one or more vectors.
22 r180 - Rotate a vector by 180 degrees.
23 r270 - Rotate a vector by 270 degrees anticlockwise.
24 r90 - Rotate a vector by 90 degrees anticlockwise.
25 sine - sin(angle between two vectors)
26 swap - Swap the components of a vector
27 zero - Whether a vector is equal to zero within the accuracy of floating point arithmetic
28 zeroAndUnits - Create the useful vectors: o=(0,0), x=(1,0), y=(0,1)
Installation
This module is written in 100% Pure Perl and, thus, it is easy to read, comprehend, use, modify and install via cpan:
sudo cpan install Math::Vectors2
Author
Copyright
Copyright (c) 2016-2019 Philip R Brenan.
This module is free software. It may be used, redistributed and/or modified under the same terms as Perl itself.