http://qs1969.pair.com?node_id=400016


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 ) = @_; $angle = deg2rad($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*$_[0]/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!)