Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Cartesian 3-Vectors

by Zaxo (Archbishop)
on Jun 18, 2001 at 10:56 UTC ( #89236=snippet: print w/ replies, xml ) Need Help??

Description:

These functions implement common operations on three dimensional vectors. Applications may use these by transforming to rectangular coordinates before calling the functions.

# These functions all take references to arrays over [0..2] without
# modifying the arguments. Arguments assume Cartesian(x,y,z) geometry
# This could be expanded to a module.

# Cartesian 3-vector dot product
sub dot {
    $_[0]->[0]*$_[1]->[0]+$_[0]->[1]*$_[1]->[1]+$_[0]->[2]*$_[1]->[2];
}

# Cartesian 3-vector cross product
# returns reference to array over [0..2]
sub cross {
    [$_[0]->[1]*$_[1]->[2]-$_[0]->[2]*$_[1]->[1],
     $_[0]->[2]*$_[1]->[0]-$_[0]->[0]*$_[1]->[2],
     $_[0]->[0]*$_[1]->[1]-$_[0]->[1]*$_[1]->[0]];
}

# length of a Cartesian 3-vector
# returns number
sub norm {
    sqrt(dot($_[0],$_[0]));
}

# angle between two Cartesian 3-vectors
# returns number: angle in radians
sub arc {
    atan2 norm(cross($_[0],$_[1])), dot($_[0],$_[1]);
}
Comment on Cartesian 3-Vectors
Download Code

Back to Snippets Section

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: snippet [id://89236]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (6)
As of 2014-11-27 19:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (187 votes), past polls