 Perl-Sensitive Sunglasses PerlMonks

### Re^2: Rotate a 3D vector

by tachyon (Chancellor)
 on Oct 18, 2004 at 01:01 UTC ( #400016=note: print w/replies, xml ) Need Help??

in reply to Re: Rotate a 3D vector
in thread Rotate a 3D vector

If you only need to rotate around the Z axis then it is a simple 2D trig problem. The z co-ordinate is invariant as this is our rotational plane.

```sub rotate {
my ( \$x,\$y,\$z,\$angle ) = @_;
my \$x_new = cos(\$angle)*\$x - sin(\$angle)*\$y;
my \$y_new = sin(\$angle)*\$x + cos(\$angle)*\$y;
return \$x_new, \$y_new, \$z;
}
sub deg2rad { return 3.14159265358979*\$_/180 };

Here is a nice article with a pretty applet Update Corrected error noted by johnnywang

cheers

tachyon

Replies are listed 'Best First'.
Re^3: Rotate a 3D vector
by true (Pilgrim) on Oct 18, 2004 at 01:33 UTC
The beauty of concrete! Thanks so much for a perfect example tachyon. Also, a good page about the topic: 2D Math
jtrue
Re^3: Rotate a 3D vector
by johnnywang (Priest) on Oct 18, 2004 at 02:02 UTC
just want to point out it's not quite right. You need to first find the polar angle for the original point (x,y), then add the \$angle to that for the new polar angle.

Updated. I'm only refering to the geometric meaning. There is a formula: multiplying by the rotation matrix given by ((cos, -sin),(sin, cos)) (not perl notation!)

Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://400016]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2023-01-29 08:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?

No recent polls found

Notices?