NAME

Math::3Space::Vector - Object wrapping a buffer of three doubles

SYNOPSIS

use Math::3Space::Vector 'vec3';

$vec= vec3(1,2,3);

say $vec->x;
$vec->x(12);

($x, $y, $z)= $vec->xyz;
$vec->set(4,3,2);

$dot_product= vec3(0,1,0)->dot(1,0,0);
$cross_product= vec3(1,0,0)->cross(0,0,1);

DESCRIPTION

This object is a blessed scalar-ref of a buffer of floating point numbers (Perl's float type, either double or long double). The vector is always 3 elements long. For more general vector classes, see many other modules on CPAN. This is simply an efficient way for the 3Space object to pass vectors around without fully allocating Perl structures for them.

CONSTRUCTOR

vec3

$vec= vec3($x, $y, $z);
$vec= vec3([ $x, $y, $z ]);
$vec2= vec3($vec);

new

$vec= Math::3Space::Vector->new(); # 0,0,0
$vec= Math::3Space::Vector->new([ $x, $y, $z ]);
$vec= Math::3Space::Vector->new(x => $x, y => $y, z => $z);
$vec= Math::3Space::Vector->new({ x => $x, y => $y, z => $z });

ATTRIBUTES

x

Read/write 'x' field.

y

Read/write 'y' field.

z

Read/write 'z' field.

xyz

Read list of (x,y,z).

magnitude

$mag= $vector->magnitude;
$vector->magnitude($new_length);

Read/write length of vector. Attempting to write to a vector with length 0 emits a warning and does nothing.

METHODS

set

$vector->set($vec2);
$vector->set($x,$y,$z);
$vector->set([$x,$y,$z]);

add

$vector->add($vec2);
$vector->add($x,$y);
$vector->add($x,$y,$z);
$vector->add([$x,$y,$z]);

sub

$vector->sub($vec2);
$vector->sub($x,$y);
$vector->sub($x,$y,$z);
$vector->sub([$x,$y,$z]);

scale

$vector->scale($scale); # x= y= z= $scale
$vector->scale($x, $y); # z= 1
$vector->scale($x, $y, $z);
$vector->scale([$x, $y, $z]);
$vector->scale($vec2);

Multiply each component of the vector by a scalar.

dot

$prod= $vector->dot($vector2);
$prod= $vector->dot($x,$y,$z);
$prod= $vector->dot([$x,$y,$z]);

Dot product with another vector.

cross

$c= $a->cross($b);
$c= $a->cross($bx, $by, $bz);
$c= $a->cross([$bx, $by, $bz]);
$c->cross($a, $b);

Return a new vector which is the cross product A x B, or if called with 2 parameters assign the cross product to the object itself.

AUTHOR

Michael Conrad <mike@nrdvana.net>

VERSION

version 0.003

COPYRIGHT AND LICENSE

This software is copyright (c) 2023 by Michael Conrad.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.