POV-Ray : Newsgroups : povray.general : Rotation angle of a vector around z axis Server Time
30 Jul 2024 02:22:52 EDT (-0400)
  Rotation angle of a vector around z axis (Message 1 to 4 of 4)  
From: MadKairon
Subject: Rotation angle of a vector around z axis
Date: 27 Mar 2010 19:30:00
Message: <web.4bae93e3cf3ff0f95e975f010@news.povray.org>
I need to find the rotation angle (always counter clockwise) of a vector around
z axis. I have a script but too slow, no precise... and I'm sure some of you who
did pay attention at school will find an easier way to do this, here is the
script in case you want to check, it tries to make a box that points to vector
"vect":

#include "colors.inc"
#include "math.inc"

#declare vect = <1,-1,0>;
#declare ang = 0;
#declare hipo = VDist ( <0,0,0>, vect );
#declare vect2 = vect;

camera{
  location <0, 0, -10>
  look_at 0
  angle 30
}

light_source{ <2,20,-20> White }

sphere { 0 .025 pigment { rgb <0,0,1> }} // ORIGIN
sphere { vect .025 pigment { rgb <1,0,0> }}


#while (VDist(<0,hipo,0>, vect2) > .1)
#declare vect2 = vrotate (vect2, <0,0,1>);
#declare ang = ang + 1;
sphere { vect2 .025 pigment { rgb .5 } }
#end

box { <-.25,0,.25>, .25 pigment { rgb .5}
    scale <.25,1,.25>
    rotate <0,0,ang * -1>
    }


Post a reply to this message

From: Slime
Subject: Re: Rotation angle of a vector around z axis
Date: 27 Mar 2010 19:38:41
Message: <4bae9701$1@news.povray.org>
You can simply use atan2( vect.y, vect.x ). Note that this is equivalent to 
atan( vect.y / vect.x ) but avoids the problem of division by zero.

This will give you the angle in radians. If you want it in degrees, use 
degrees( atan2( vect.y, vect.x ) ).

 - Slime
 [ http://www.slimeland.com/ ]


Post a reply to this message

From: MadKairon
Subject: Re: Rotation angle of a vector around z axis
Date: 27 Mar 2010 20:15:01
Message: <web.4bae9e9d6eaea3b55e975f010@news.povray.org>
"Slime" <fak### [at] emailaddress> wrote:
> You can simply use atan2( vect.y, vect.x ). Note that this is equivalent to
> atan( vect.y / vect.x ) but avoids the problem of division by zero.
>
> This will give you the angle in radians. If you want it in degrees, use
> degrees( atan2( vect.y, vect.x ) ).
>
>  - Slime
>  [ http://www.slimeland.com/ ]


yep... much better. I had no idea how to make that, THANKS!! but... here's the
problem I've been trying to avoid:

#include "colors.inc"
#include "math.inc"

#declare vect = <-1,1,0>;

camera{
  location <0, 0, -10>
  look_at 0
  angle 30
}

light_source{ <2,20,-20> White }

sphere { 0 .025 pigment { rgb <0,0,1> }} // ORIGIN
sphere { vect .025 pigment { rgb <1,0,0> }}


box { <-.25,0,-.25>, .25 pigment { rgb .5}
    scale <.25,1,.25>
    rotate <0,0,degrees( atan2( vect.y, vect.x ) )>
    }

depending on the quadrant where the vector is the box will point the wrong way.
Couldn't figure out an easy solution for this. Any ideas? Thanks again!


Post a reply to this message

From: Slime
Subject: Re: Rotation angle of a vector around z axis
Date: 27 Mar 2010 21:13:43
Message: <4baead47$1@news.povray.org>
> depending on the quadrant where the vector is the box will point the wrong 
> way.
> Couldn't figure out an easy solution for this. Any ideas? Thanks again!

The expression I gave you gives the angle of the vector from the positive X 
direction, whereas I think you're looking for the angle from the Y 
direction. You could fix this by first rotating the box 90 degrees to the 
right to line it up with the X direction.


 - Slime
 [ http://www.slimeland.com/ ]


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.