.\" Automatically generated by Pod::Man v1.37, Pod::Parser v1.32 .\" .\" Standard preamble: .\" ======================================================================== .de Sh \" Subsection heading .br .if t .Sp .ne 5 .PP \fB\\$1\fR .PP .. .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' 'br\} .\" .\" If the F register is turned on, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.Sh), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . nr % 0 . rr F .\} .\" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .hy 0 .if n .na .\" .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2). .\" Fear. Run. Save yourself. No user-serviceable parts. . \" fudge factors for nroff and troff .if n \{\ . ds #H 0 . ds #V .8m . ds #F .3m . ds #[ \f1 . ds #] \fP .\} .if t \{\ . ds #H ((1u-(\\\\n(.fu%2u))*.13m) . ds #V .6m . ds #F 0 . ds #[ \& . ds #] \& .\} . \" simple accents for nroff and troff .if n \{\ . ds ' \& . ds ` \& . ds ^ \& . ds , \& . ds ~ ~ . ds / .\} .if t \{\ . ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u" . ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u' . ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u' . ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u' . ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u' . ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u' .\} . \" troff and (daisy-wheel) nroff accents .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V' .ds 8 \h'\*(#H'\(*b\h'-\*(#H' .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#] .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H' .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u' .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#] .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#] .ds ae a\h'-(\w'a'u*4/10)'e .ds Ae A\h'-(\w'A'u*4/10)'E . \" corrections for vroff .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u' .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u' . \" for low resolution devices (crt and lpr) .if \n(.H>23 .if \n(.V>19 \ \{\ . ds : e . ds 8 ss . ds o a . ds d- d\h'-1'\(ga . ds D- D\h'-1'\(hy . ds th \o'bp' . ds Th \o'LP' . ds ae ae . ds Ae AE .\} .rm #[ #] #H #V #F C .\" ======================================================================== .\" .IX Title "Math::Vec 3pm" .TH Math::Vec 3pm "2007-06-15" "perl v5.8.8" "User Contributed Perl Documentation" .SH "NAME" Math::Vec \- Object\-Oriented Vector Math Methods in Perl .SH "SYNOPSIS" .IX Header "SYNOPSIS" .Vb 2 \& use Math::Vec; \& $v = Math::Vec\->new(0,1,2); .Ve .PP .Vb 1 \& or .Ve .PP .Vb 5 \& use Math::Vec qw(NewVec); \& $v = NewVec(0,1,2); \& @res = $v\->Cross([1,2.5,0]); \& $p = NewVec(@res); \& $q = $p\->Dot([0,1,0]); .Ve .PP .Vb 1 \& or .Ve .PP .Vb 3 \& use Math::Vec qw(:terse); \& $v = V(0,1,2); \& $q = ($v x [1,2.5,0]) * [0,1,0]; .Ve .SH "NOTICE" .IX Header "NOTICE" This module is still somewhat incomplete. If a function does nothing, there is likely a really good reason. Please have a look at the code if you are trying to use this in a production environment. .SH "AUTHOR" .IX Header "AUTHOR" Eric L. Wilhelm .PP http://scratchcomputing.com .SH "DESCRIPTION" .IX Header "DESCRIPTION" This module was adapted from Math::Vector, written by Wayne M. Syvinski. .PP It uses most of the same algorithms, and currently preserves the same names as the original functions, though some aliases have been added to make the interface more natural (at least to the way I think.) .PP The \*(L"object\*(R" for the object oriented calling style is a blessed array reference which contains a vector of the form [x,y,z]. Methods will typically return a list. .SH "COPYRIGHT NOTICE" .IX Header "COPYRIGHT NOTICE" Copyright (C) 2003\-2006 Eric Wilhelm .PP portions Copyright 2003 Wayne M. Syvinski .SH "NO WARRANTY" .IX Header "NO WARRANTY" Absolutely, positively \s-1NO\s0 \s-1WARRANTY\s0, neither express or implied, is offered with this software. You use this software at your own risk. In case of loss, neither Wayne M. Syvinski, Eric Wilhelm, nor anyone else, owes you anything whatseover. You have been warned. .PP Note that this includes \s-1NO\s0 \s-1GUARANTEE\s0 of \s-1MATHEMATICAL\s0 \s-1CORRECTNESS\s0. If you are going to use this code in a production environment, it is \s-1YOUR\s0 \&\s-1RESPONSIBILITY\s0 to verify that the methods return the correct values. .SH "LICENSE" .IX Header "LICENSE" You may use this software under one of the following licenses: .PP .Vb 4 \& (1) GNU General Public License \& (found at http://www.gnu.org/copyleft/gpl.html) \& (2) Artistic License \& (found at http://www.perl.com/pub/language/misc/Artistic.html) .Ve .SH "SEE ALSO" .IX Header "SEE ALSO" .Vb 1 \& Math::Vector .Ve .SH "Constructor" .IX Header "Constructor" .Sh "new" .IX Subsection "new" Returns a blessed array reference to cartesian point ($x, \f(CW$y\fR, \f(CW$z\fR), where \f(CW$z\fR is optional. Note the feed\-me\-list, get-back-reference syntax here. This is the opposite of the rest of the methods for a good reason (it allows nesting of function calls.) .PP The z value is optional, (and so are x and y.) Undefined values are silently translated into zeros upon construction. .PP .Vb 1 \& $vec = Math::Vec\->new($x, $y, $z); .Ve .Sh "NewVec" .IX Subsection "NewVec" This is simply a shortcut to Math::Vec\->new($x, \f(CW$y\fR, \f(CW$z\fR) for those of you who don't want to type so much so often. This also makes it easier to nest / chain your function calls. Note that methods will typically output lists (e.g. the answer to your question.) While you can simply [bracket] the answer to make an array reference, you need that to be blessed in order to use the \f(CW$object\fR\->method(@args) syntax. This function does that blessing. .PP This function is exported as an option. To use it, simply use Math::Vec qw(NewVec); at the start of your code. .PP .Vb 3 \& use Math::Vec qw(NewVec); \& $vec = NewVec($x, $y, $z); \& $diff = NewVec($vec\->Minus([$ovec\->ScalarMult(0.5)])); .Ve .SH "Terse Functions" .IX Header "Terse Functions" These are all one-letter shortcuts which are imported to your namespace with the :terse flag. .PP .Vb 1 \& use Math::Vec qw(:terse); .Ve .Sh "V" .IX Subsection "V" This is the same as Math::Vec\->new($x,$y,$z). .PP .Vb 1 \& $vec = V($x, $y, $z); .Ve .Sh "U" .IX Subsection "U" Shortcut to V($x,$y,$z)\->\fIUnitVector()\fR .PP .Vb 1 \& $unit = U($x, $y, $z); .Ve .PP This will also work if called with a vector object: .PP .Vb 1 \& $unit = U($vector); .Ve .Sh "X" .IX Subsection "X" Returns an x\-axis unit vector. .PP .Vb 1 \& $xvec = X(); .Ve .Sh "Y" .IX Subsection "Y" Returns a y\-axis unit vector. .PP .Vb 1 \& $yvec = Y(); .Ve .Sh "Z" .IX Subsection "Z" Returns a z\-axis unit vector. .PP .Vb 1 \& $zvec = Z(); .Ve .SH "Overloading" .IX Header "Overloading" Best used with the :terse functions, the Overloading scheme introduces an interface which is unique from the Methods interface. Where the methods take references and return lists, the overloaded operators will return references. This allows vector arithmetic to be chained together more easily. Of course, you can easily dereference these with @{$vec}. .PP The following sections contain equivelant expressions from the longhand and terse interfaces, respectively. .Sh "Negation:" .IX Subsection "Negation:" .Vb 2 \& @a = NewVec\->(0,1,1)\->ScalarMult(\-1); \& @a = @{\-V(0,1,1)}; .Ve .Sh "Stringification:" .IX Subsection "Stringification:" This also performs concatenation and other string operations. .PP .Vb 1 \& print join(", ", 0,1,1), "\en"; .Ve .PP .Vb 1 \& print V(0,1,1), "\en"; .Ve .PP .Vb 4 \& $v = V(0,1,1); \& print "$v\en"; \& print "$v" . "\en"; \& print $v, "\en"; .Ve .Sh "Addition:" .IX Subsection "Addition:" .Vb 1 \& @a = NewVec(0,1,1)\->Plus([2,2]); .Ve .PP .Vb 1 \& @a = @{V(0,1,1) + V(2,2)}; .Ve .PP .Vb 2 \& # only one argument needs to be blessed: \& @a = @{V(0,1,1) + [2,2]}; .Ve .PP .Vb 2 \& # and which one is blessed doesn't matter: \& @a = @{[0,1,1] + V(2,2)}; .Ve .Sh "Subtraction:" .IX Subsection "Subtraction:" .Vb 1 \& @a = NewVec(0,1,1)\->Minus([2,2]); .Ve .PP .Vb 1 \& @a = @{[0,1,1] \- V(2,2)}; .Ve .Sh "Scalar Multiplication:" .IX Subsection "Scalar Multiplication:" .Vb 1 \& @a = NewVec(0,1,1)\->ScalarMult(2); .Ve .PP .Vb 1 \& @a = @{V(0,1,1) * 2}; .Ve .PP .Vb 1 \& @a = @{2 * V(0,1,1)}; .Ve .Sh "Scalar Division:" .IX Subsection "Scalar Division:" .Vb 1 \& @a = NewVec(0,1,1)\->ScalarMult(1/2); .Ve .PP .Vb 2 \& # order matters! \& @a = @{V(0,1,1) / 2}; .Ve .Sh "Cross Product:" .IX Subsection "Cross Product:" .Vb 1 \& @a = NewVec(0,1,1)\->Cross([0,1]); .Ve .PP .Vb 1 \& @a = @{V(0,1,1) x [0,1]}; .Ve .PP .Vb 1 \& @a = @{[0,1,1] x V(0,1)}; .Ve .Sh "Dot Product:" .IX Subsection "Dot Product:" Also known as the \*(L"Scalar Product\*(R". .PP .Vb 1 \& $a = NewVec(0,1,1)\->Dot([0,1]); .Ve .PP .Vb 1 \& $a = V(0,1,1) * [0,1]; .Ve .PP Note: Not using the '.' operator here makes everything more efficient. I know, the * is not a dot, but at least it's a mathematical operator (perl does some implied string concatenation somewhere which drove me to avoid the dot.) .Sh "Comparison:" .IX Subsection "Comparison:" The == and != operators will compare vectors for equal direction and magnitude. No attempt is made to apply tolerance to this equality. .Sh "Length:" .IX Subsection "Length:" .Vb 1 \& $a = NewVec(0,1,1)\->Length(); .Ve .PP .Vb 1 \& $a = abs(V(0,1,1)); .Ve .Sh "Vector Projection:" .IX Subsection "Vector Projection:" This one is a little different. Where the method is written \&\f(CW$a\fR\->Proj($b) to give the projection of \f(CW$b\fR onto \f(CW$a\fR, this reads like you would say it (b projected onto a): \f(CW$b\fR>>$a. .PP .Vb 1 \& @a = NewVec(0,1,1)\->Proj([0,0,1]); .Ve .PP .Vb 1 \& @a = @{V(0,0,1)>>[0,1,1]}; .Ve .SH "Chaining Operations" .IX Header "Chaining Operations" The above examples simply show how to go from the method interface to the overloaded interface, but where the overloading really shines is in chaining multiple operations together. Because the return values from the overloaded operators are all references, you dereference them only when you are done. .Sh "Unit Vector left of a line" .IX Subsection "Unit Vector left of a line" This comes from the \fICAD::Calc::line_to_rectangle()\fR function. .PP .Vb 5 \& use Math::Vec qw(:terse); \& @line = ([0,1],[1,0]); \& my ($a, $b) = map({V(@$_)} @line); \& $unit = U($b \- $a); \& $left = $unit x \-Z(); .Ve .Sh "Length of a cross product" .IX Subsection "Length of a cross product" .Vb 1 \& $length = abs($va x $vb); .Ve .Sh "Vectors as coordinate axes" .IX Subsection "Vectors as coordinate axes" This is useful in drawing eliptical arcs using dxf data. .PP .Vb 11 \& $val = 3.14159; # the 'start parameter' \& @c = (14.15973317961194, 6.29684276451746); # codes 10, 20, 30 \& @e = (6.146127847120538, 0); # codes 11, 21, 31 \& @ep = @{V(@c) + \e@e}; # that's the axis endpoint \& $ux = U(@e); # unit on our x' axis \& $uy = U($ux x \-Z()); # y' is left of x' \& $center = V(@c); \& # autodesk gives you this: \& @pt = ($a * cos($val), $b * sin($val)); \& # but they don't tell you about the major/minor axis issue: \& @pt = @{$center + $ux * $pt[0] + $uy * $pt[1]};; .Ve .SH "Precedence" .IX Header "Precedence" The operator precedence is going to be whatever perl wants it to be. I have not yet investigated this to see if it matches standard vector arithmetic notation. If in doubt, use parentheses. .PP One item of note here is that the 'x' and '*' operators have the same precedence, so the leftmost wins. In the following example, you can get away without parentheses if you have the cross-product first. .PP .Vb 3 \& # dot product of a cross product: \& $v1 x $v2 * $v3 \& ($v1 x $v2) * $v3 .Ve .PP .Vb 2 \& # scalar crossed with a vector (illegal!) \& $v3 * $v1 x $v2 .Ve .SH "Methods" .IX Header "Methods" The typical theme is that methods require array references and return lists. This means that you can choose whether to create an anonymous array ref for use in feeding back into another function call, or you can simply use the list as\-is. Methods which return a scalar or list of scalars (in the mathematical sense, not the Perl \s-1SV\s0 sense) are exempt from this theme, but methods which return what could become one vector will return it as a list. .PP If you want to chain calls together, either use the NewVec constructor, or enclose the call in square brackets to make an anonymous array out of the result. .PP .Vb 5 \& my $vec = NewVec(@pt); \& my $doubled = NewVec($vec\->ScalarMult(0.5)); \& my $other = NewVec($vec\->Plus([0,2,1], [4,2,3])); \& my @result = $other\->Minus($doubled); \& $unit = NewVec(NewVec(@result)\->UnitVector()); .Ve .PP The vector objects are simply blessed array references. This makes for a fairly limited amount of manipulation, but vector math is not complicated stuff. Hopefully, you can save at least two lines of code per calculation using this module. .Sh "Dot" .IX Subsection "Dot" Returns the dot product of \f(CW$vec\fR 'dot' \f(CW$othervec\fR. .PP .Vb 1 \& $vec\->Dot($othervec); .Ve .Sh "DotProduct" .IX Subsection "DotProduct" Alias to \fIDot()\fR .PP .Vb 1 \& $number = $vec\->DotProduct($othervec); .Ve .Sh "Cross" .IX Subsection "Cross" Returns \f(CW$vec\fR x \f(CW$other_vec\fR .PP .Vb 3 \& @list = $vec\->Cross($other_vec); \& # or, to use the result as a vec: \& $cvec = NewVec($vec\->Cross($other_vec)); .Ve .Sh "CrossProduct" .IX Subsection "CrossProduct" Alias to \fICross()\fR (should really strip out all of this clunkiness and go to operator overloading, but that gets into other hairiness.) .PP .Vb 1 \& $vec\->CrossProduct(); .Ve .Sh "Length" .IX Subsection "Length" Returns the length of \f(CW$vec\fR .PP .Vb 1 \& $length = $vec\->Length(); .Ve .Sh "Magnitude" .IX Subsection "Magnitude" .Vb 1 \& $vec\->Magnitude(); .Ve .Sh "UnitVector" .IX Subsection "UnitVector" .Vb 1 \& $vec\->UnitVector(); .Ve .Sh "ScalarMult" .IX Subsection "ScalarMult" Factors each element of \f(CW$vec\fR by \f(CW$factor\fR. .PP .Vb 1 \& @new = $vec\->ScalarMult($factor); .Ve .Sh "Minus" .IX Subsection "Minus" Subtracts an arbitrary number of vectors. .PP .Vb 1 \& @result = $vec\->Minus($other_vec, $another_vec?); .Ve .PP This would be equivelant to: .PP .Vb 1 \& @result = $vec\->Minus([$other_vec\->Plus(@list_of_vectors)]); .Ve .Sh "VecSub" .IX Subsection "VecSub" Alias to \fIMinus()\fR .PP .Vb 1 \& $vec\->VecSub(); .Ve .Sh "InnerAngle" .IX Subsection "InnerAngle" Returns the acute angle (in radians) in the plane defined by the two vectors. .PP .Vb 1 \& $vec\->InnerAngle($other_vec); .Ve .Sh "DirAngles" .IX Subsection "DirAngles" .Vb 1 \& $vec\->DirAngles(); .Ve .Sh "Plus" .IX Subsection "Plus" Adds an arbitrary number of vectors. .PP .Vb 1 \& @result = $vec\->Plus($other_vec, $another_vec); .Ve .Sh "PlanarAngles" .IX Subsection "PlanarAngles" If called in list context, returns the angle of the vector in each of the primary planes. If called in scalar context, returns only the angle in the xy plane. Angles are returned in radians counter-clockwise from the primary axis (the one listed first in the pairs below.) .PP .Vb 1 \& ($xy_ang, $xz_ang, $yz_ang) = $vec\->PlanarAngles(); .Ve .Sh "Ang" .IX Subsection "Ang" A simpler alias to \fIPlanarAngles()\fR which eliminates the concerns about context and simply returns the angle in the xy plane. .PP .Vb 1 \& $xy_ang = $vec\->Ang(); .Ve .Sh "VecAdd" .IX Subsection "VecAdd" .Vb 1 \& $vec\->VecAdd(); .Ve .Sh "UnitVectorPoints" .IX Subsection "UnitVectorPoints" Returns a unit vector which points from \f(CW$A\fR to \f(CW$B\fR. .PP .Vb 1 \& $A\->UnitVectorPoints($B); .Ve .Sh "InnerAnglePoints" .IX Subsection "InnerAnglePoints" Returns the \fIInnerAngle()\fR between the three points. \f(CW$Vert\fR is the vertex of the points. .PP .Vb 1 \& $Vert\->InnerAnglePoints($endA, $endB); .Ve .Sh "PlaneUnitNormal" .IX Subsection "PlaneUnitNormal" Returns a unit vector normal to the plane described by the three points. The sense of this vector is according to the right-hand rule and the order of the given points. The \f(CW$Vert\fR vector is taken as the vertex of the three points. e.g. if \f(CW$Vert\fR is the origin of a coordinate system where the x\-axis is \f(CW$A\fR and the y\-axis is \f(CW$B\fR, then the return value would be a unit vector along the positive z\-axis. .PP .Vb 1 \& $Vert\->PlaneUnitNormal($A, $B); .Ve .Sh "TriAreaPoints" .IX Subsection "TriAreaPoints" Returns the angle of the triangle formed by the three points. .PP .Vb 1 \& $A\->TriAreaPoints($B, $C); .Ve .Sh "Comp" .IX Subsection "Comp" Returns the scalar projection of \f(CW$B\fR onto \f(CW$A\fR (also called the component of \f(CW$B\fR along \f(CW$A\fR.) .PP .Vb 1 \& $A\->Comp($B); .Ve .Sh "Proj" .IX Subsection "Proj" Returns the vector projection of \f(CW$B\fR onto \f(CW$A\fR. .PP .Vb 1 \& $A\->Proj($B); .Ve .Sh "PerpFoot" .IX Subsection "PerpFoot" Returns a point on line \f(CW$A\fR,$B which is as close to \f(CW$pt\fR as possible (and therefore perpendicular to the line.) .PP .Vb 1 \& $pt\->PerpFoot($A, $B); .Ve .SH "Incomplete Methods" .IX Header "Incomplete Methods" The following have yet to be translated into this interface. They are shown here simply because I intended to fully preserve the function names from the original Math::Vector module written by Wayne M. Syvinski. .Sh "TripleProduct" .IX Subsection "TripleProduct" .Vb 1 \& $vec\->TripleProduct(); .Ve .Sh "\s-1IJK\s0" .IX Subsection "IJK" .Vb 1 \& $vec\->IJK(); .Ve .Sh "OrdTrip" .IX Subsection "OrdTrip" .Vb 1 \& $vec\->OrdTrip(); .Ve .Sh "\s-1STV\s0" .IX Subsection "STV" .Vb 1 \& $vec\->STV(); .Ve .Sh "Equil" .IX Subsection "Equil" .Vb 1 \& $vec\->Equil(); .Ve