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 20231001.
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(new(+1, -1)), deg2rad(-135); # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
ok near $y->angle(new(+1, 0)), deg2rad(-90); # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
ok near $y->angle(new(+1, +1)), deg2rad(-45); # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
ok near $y->angle(new( 0, +1)), deg2rad(+0); # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
ok near $y->angle(new(-1, +1)), deg2rad(+45); # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
ok near $y->angle(new(-1, 0)), deg2rad(+90); # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
ok near $y->angle(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 deg2rad(-60), $x + $y * sqrt(3) < $x;
ok near deg2rad(+30), ($x + $y * sqrt(3))->angle($y);
ok near deg2rad( 0), $y->smallestAngleToNormalPlane( $x); # First vector is y, second vector is 0 degrees anti-clockwise from x axis
ok near deg2rad(+45), $y->smallestAngleToNormalPlane( $x + $y);
ok near deg2rad(+90), $y->smallestAngleToNormalPlane( $y);
ok near deg2rad(+45), $y->smallestAngleToNormalPlane(-$x + -$y);
ok near deg2rad( 0), $y->smallestAngleToNormalPlane(-$x);
ok near deg2rad(+45), $y->smallestAngleToNormalPlane(-$x + -$y);
ok near deg2rad(+90), $y->smallestAngleToNormalPlane( -$y);
ok near deg2rad(+45), $y->smallestAngleToNormalPlane(-$x + -$y);
ok near deg2rad( 0), $y->smallestAngleToNormalPlane( $x);
for my $i(-179..179)
{ok near $x < new(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: zero=(0,0), x=(1,0), y=(0,1).
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)-> 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 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)'); # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
zero($o)
Whether a vector is equal to zero within the accuracy of floating point arithmetic.
Parameter Description
1 $o Vector
Example:
my ($zero, $x, $y) = zeroAndUnits; # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
ok $zero->zero; # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
ok !$x->zero; # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
ok !$y->zero; # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
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)-> print 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->clone == $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->Plus(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->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)');
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->Minus(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->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)');
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->Multiply(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->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)');
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->Divide(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->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)');
l($o)
Length 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)->l2;
ok 2 * ($x + $y)->l == ($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)->l2; # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
ok 2 * ($x + $y)->l == ($x + $y)->d (-$x - $y);
ok 4 * ($x + $y)->l2 == ($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)->d (-$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)->d2(-$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)->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);
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)->area($x * 2); # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
ok near +2, ($x + $y)->area($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->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)->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->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);
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->angle(new(+1, -1)), deg2rad(-135); # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
ok near $y->angle(new(+1, 0)), deg2rad(-90); # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
ok near $y->angle(new(+1, +1)), deg2rad(-45); # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
ok near $y->angle(new( 0, +1)), deg2rad(+0); # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
ok near $y->angle(new(-1, +1)), deg2rad(+45); # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
ok near $y->angle(new(-1, 0)), deg2rad(+90); # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
ok near $y->angle(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 deg2rad(-60), $x + $y * sqrt(3) < $x;
ok near deg2rad(+30), ($x + $y * sqrt(3))->angle($y); # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
ok near deg2rad( 0), $y->smallestAngleToNormalPlane( $x); # First vector is y, second vector is 0 degrees anti-clockwise from x axis
ok near deg2rad(+45), $y->smallestAngleToNormalPlane( $x + $y);
ok near deg2rad(+90), $y->smallestAngleToNormalPlane( $y);
ok near deg2rad(+45), $y->smallestAngleToNormalPlane(-$x + -$y);
ok near deg2rad( 0), $y->smallestAngleToNormalPlane(-$x);
ok near deg2rad(+45), $y->smallestAngleToNormalPlane(-$x + -$y);
ok near deg2rad(+90), $y->smallestAngleToNormalPlane( -$y);
ok near deg2rad(+45), $y->smallestAngleToNormalPlane(-$x + -$y);
ok near deg2rad( 0), $y->smallestAngleToNormalPlane( $x);
for my $i(-179..179)
{ok near $x < new(cos(deg2rad($i)), sin(deg2rad($i))), deg2rad($i);
}
smallestAngleToNormalPlane($a, $b)
The smallest angle between the second vector and a plane normal to the first vector.
Parameter Description
1 $a Vector 1
2 $b Vector 2
r90($o)
Rotate a vector by 90 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; # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
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;
rotate($p, $o, $sin, $cos)
Rotate a vector about another vector through an angle specified by its values as sin, and cos.
Parameter Description
1 $p Vector to rotate
2 $o Center of rotation
3 $sin Sin of the angle of rotation
4 $cos Cosine of the angle of rotation
Example:
ok near2 new(1, 0)->rotate(new(0,0), 1, 0), new( 0, 1); # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
ok near2 new(1, 1)->rotate(new(0,0), 1, 0), new(-1, 1); # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
ok near2 new(0, 1)->rotate(new(0,0), 1, 0), new(-1, 0); # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
ok near2 new(2, 2)->rotate(new(1,1), -1/sqrt(2), 1/sqrt(2)), new(1+sqrt(2), 1); # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
ok near2 new(3, 1)->rotate(new(1,1), sqrt(3)/2, 1/2), new(2, 1+sqrt(3)); # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
ok near2 new(3, 1)->rotate(new(1,1), # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
new(1, 0)->sine (new(1,1)),
new(1, 0)->cosine(new(1,1))),
new(1+sqrt(2), 1+sqrt(2));
intersection($a, $b, $c, $d)
Find the intersection of two line segments delimited by vectors if such a point exists.
Parameter Description
1 $a Start of first line segment
2 $b End of first line segment
3 $c Start of second line segment
4 $d End of second line segment
Example:
ok near2 intersection(new(0,0), new(2,2), new(0,2),new(2,0)), new(1,1); # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
ok near2 intersection(new(1,1), new(3,3), new(1,3),new(3,1)), new(2,2); # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
triangulate($clockwise, @boundary)
Find a set of triangles that cover a shape whose boundary points are represented by an array of vectors. The points along the boundary must be given in such away that the interior of the shape is always on the same side for each pair of successive points as indicated by the clockwise parameter.
Parameter Description
1 $clockwise If true then the interior of the shape is on the left as the boundary of the shape is traversed otherwise on the right
2 @boundary Vectors representing the boundary of the shape
Example:
my @t = triangulate(1, new(0,0), new(2,0), new(2,2), new(0,2)); # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
ok near2 $t[0][0], new(1, 1);
ok near2 $t[0][1], new(0, 0);
ok near2 $t[0][2], new(2, 0);
ok near2 $t[1][0], new(1, 1);
ok near2 $t[1][1], new(2, 0);
ok near2 $t[1][2], new(2, 2);
ok near2 $t[2][0], new(1, 1);
ok near2 $t[2][1], new(2, 2);
ok near2 $t[2][2], new(0, 2);
ok near2 $t[3][0], new(0, 0);
ok near2 $t[3][1], new(1, 1);
ok near2 $t[3][2], new(0, 2);
my @t = triangulate(0, new(2,2), new(2, 4), new(4,4), new(4, 2)); # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
ok near2 $t[0][0], new(3, 3);
ok near2 $t[0][1], new(2, 2);
ok near2 $t[0][2], new(2, 4);
ok near2 $t[1][0], new(3, 3);
ok near2 $t[1][1], new(2, 4);
ok near2 $t[1][2], new(4, 4);
ok near2 $t[2][0], new(3, 3);
ok near2 $t[2][1], new(4, 4);
ok near2 $t[2][2], new(4, 2);
ok near2 $t[3][0], new(2, 2);
ok near2 $t[3][1], new(3, 3);
ok near2 $t[3][2], new(4, 2);
swap($o)
Swap the components of a vector.
Parameter Description
1 $o Vector
Example:
my ($z, $x, $y) = zeroAndUnits;
ok $x->swap == $y; # 𝗘𝘅𝗮𝗺𝗽𝗹𝗲
ok $x->clone == $x;
Hash Definitions
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 vector by a scalar and return the result.
8 divide - Divide a copy of 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 intersection - Find the intersection of two line segments delimited by vectors if such a point exists.
12 l - Length of a vector.
13 l2 - Length squared of a vector.
14 Minus - Subtract zero or more vectors from the first vector and return the result.
15 minus - Subtract zero or more vectors from a copy of the first vector and return the result.
16 multiply - Multiply a copy of a vector by a scalar and return the result.
17 Multiply - Multiply a vector by a scalar and return the result.
18 n - Return a normalized a copy of a vector.
19 new - Create new vector from components.
20 plus - Add zero or more other vectors to a copy of the first vector and return the result.
21 Plus - Add zero or more other vectors to the first vector and return the result.
22 print - Print one or more vectors.
23 r180 - Rotate a vector by 180 degrees.
24 r270 - Rotate a vector by 270 degrees anticlockwise.
25 r90 - Rotate a vector by 90 degrees anticlockwise.
26 rotate - Rotate a vector about another vector through an angle specified by its values as sin, and cos.
27 sine - Sin(angle between two vectors).
28 smallestAngleToNormalPlane - The smallest angle between the second vector and a plane normal to the first vector.
29 swap - Swap the components of a vector.
30 triangulate - Find a set of triangles that cover a shape whose boundary points are represented by an array of vectors.
31 zero - Whether a vector is equal to zero within the accuracy of floating point arithmetic.
32 zeroAndUnits - Create the useful vectors: zero=(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-2023 Philip R Brenan.
This module is free software. It may be used, redistributed and/or modified under the same terms as Perl itself.