 go ahead... be a heretic PerlMonks

### Re: Rotate a 3D vector

by true (Pilgrim)
 on Oct 18, 2004 at 00:02 UTC ( #400007=note: print w/replies, xml ) Need Help??

in reply to Rotate a 3D vector

Clarification then. i only need to rotate the vector on the z axis. So the 3dvector point 3 1 3 is rotated 23 degrees on the z axis only. Any help?
jtrue

Replies are listed 'Best First'.
Re^2: Rotate a 3D vector
by tachyon (Chancellor) on Oct 18, 2004 at 01:01 UTC

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

The beauty of concrete! Thanks so much for a perfect example tachyon. Also, a good page about the topic: 2D Math
jtrue
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!)

Re^2: Rotate a 3D vector
by FoxtrotUniform (Prior) on Oct 18, 2004 at 00:28 UTC

Well, the quick-and-dirty way to do it is to scale your x component by cos(t) and your y component by sin(t). You might be happier with a matrix-based solution in the long run, though.

Edit: The math above is, of course, totally bogus. I plead momentary imbecility. johnnywang has the correct solution. D'oh!

What kind of program are you writing, btw?

--
Yours in pedantry,
F o x t r o t U n i f o r m

"Lines of code don't matter as long as I'm not writing them." -- merlyn

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

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

No recent polls found

Notices?