No such thing as a small change PerlMonks

### Cartesian 3-Vectors

by Zaxo (Archbishop)
 on Jun 18, 2001 at 10:56 UTC 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]);
}
```

Create A New User
Node Status?
node history
Node Type: snippet [id://89236]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2018-05-25 05:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
World peace can best be achieved by:

Results (181 votes). Check out past polls.

Notices?