GIDForums

Go Back   GIDForums > Computer Programming Forums > CPP / C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read


 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 20-Mar-2008, 08:25
mosquito mosquito is offline
Junior Member
 
Join Date: Feb 2008
Posts: 40
mosquito is on a distinguished road

Angle between vectors


Hi,

I have a fixed unit vector in the direction of z, vec_0 = (0,0,1). And I rotate it randomly around the x, y or z axes. After a rotation like this I will get a vector vec_1. Then I rotate this vec_1 randomly according to the above method, this way I will get vector vec_2...and so on. For example I want to generate 100 vectors.

I should calculate the angles between the vec_0 and the other vectors, i.e. :

angle vec_0 and vec_1,
angle vec_0 and vec_2,
angle vec_0 and vec_3,...

Could you help me to solve this?

CPP / C++ / C Code:
...
struct vector{float x, y, z;};

vector vec;

		// fixed unit vector (0,0,1)
		vec.x = 0;
		vec.y = 0;
		vec.z = 1;

	ofstream ofile;
        ofile.open("rotvec.dat");

	for (i = 0; i < 100; i++) 
	{

            switch (rand()%3) // a number that is either 0, 1, or 2
	    { 

        	case 0:
		//To rotate vec around the X axis
    		vec = Rotate(mX, vec);
            	break;

        	case 1:
		//To rotate vec around the Y axis
    		vec = Rotate(mY, vec);
            	break;

        	case 2:
		//To rotate vec around the Z axis
    		vec = Rotate(mZ, vec);
            	break;
            }

              // Here I would like to get the angle between vec_0 and vec_1, vec_2,...
	      // And print out the result like this
              // cout << angle vec_0-vec_i << endl;	
	}
       ofile.close();	
...
  #2  
Old 20-Mar-2008, 09:05
Blake's Avatar
Blake Blake is online now
Member
 
Join Date: Nov 2005
Posts: 167
Blake will become famous soon enough

Re: Angle between vectors


To find the angle between two vectors, you need to calculate the sine and the cosine. Since they're unit vectors, the dot product gives the cosine, and the magnitude of the cross product gives the sine.

That is, given vectors a and b, the cosine of the angle is a.x*b.x + a.y*b.y + a.z + b.z

The cross product is a vector. Call it c. Then

c.x = a.y*b.z - a.z*b.y
c.y = a.z*b.x - a.x*b.z
c.z = a.x*b.y - a.y*b.x

The magnitude of c is the sine of the angle. In other words, the sine is given by

c.x*c.x + c.y*c.y + c.z*c.z

Now you know the sine and the cosine of the angle. The tangent of the angle is the sine divided by the cosine. Once you know the tangent, you can use the inverse tangent to compute the angle.

Make sure you have a special case if the cosine is 0, to avoid dividing by 0.

EDIT: You need to be using an orthonormal basis for this to work. If you don't know what that means, it's probably safe to assume that you are using one.
__________________
www.blake-foster.com
  #3  
Old 20-Mar-2008, 09:23
mosquito mosquito is offline
Junior Member
 
Join Date: Feb 2008
Posts: 40
mosquito is on a distinguished road

Re: Angle between vectors


All right, indeed I should have only the cosine.

Could you give me some code taking the above code into account?

I think, I should have
CPP / C++ / C Code:
vector vec_0 = {0,0,1};
. I must get the cosine form the switch-case part, somehow....
  #4  
Old 20-Mar-2008, 09:30
Blake's Avatar
Blake Blake is online now
Member
 
Join Date: Nov 2005
Posts: 167
Blake will become famous soon enough

Re: Angle between vectors


I'm not sure exactly what you're asking. Could you clarify.

You don't need a switch statement to get the cosine. Just use the dot product.

This would give you the cosine:

CPP / C++ / C Code:
double cosine = vec_0.x*vec_1.x + vec_0.y*vec_1.y + vec_0.z*vec_1.z

This is the cosine of the angle between vec_0 and vec_1. Just make sure that they're unit vectors.

Also, the cosine alone is not sufficient to calculate the angle. You need the sine as well.
__________________
www.blake-foster.com
  #5  
Old 21-Mar-2008, 03:26
Peter_APIIT Peter_APIIT is offline
Regular Member
 
Join Date: May 2007
Location: Malaysia
Posts: 353
Peter_APIIT is on a distinguished road

Re: Angle between vectors


No idea what u all talking here.
__________________
Linux is the best OS in the world.
  #6  
Old 21-Mar-2008, 07:31
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,527
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Angle between vectors


Quote:
Originally Posted by Blake

Also, the cosine alone is not sufficient to calculate the angle. You need the sine as well.

Why would you need the sine? Just take arc-cosine (acos()) of the value of the expression that you showed. The result from acos() is an angle (in radians) from zero to pi, which is the usual definition of the angle between the two unit vectors. (Of course, if they are not unit vectors, just divide the result of the expression by the product of the magnitudes before calling acos().)

Regards,

Dave
  #7  
Old 21-Mar-2008, 07:43
Blake's Avatar
Blake Blake is online now
Member
 
Join Date: Nov 2005
Posts: 167
Blake will become famous soon enough

Re: Angle between vectors


The problem is that the sine and cosine are not 1-1. For any value of the sine or cosine (except +/-1), there are two possible solutions for the angle in the interval [0, 2pi). We need the sine and the cosine to find the exact angle.

In some cases, it doesn't matter (ie if you just need the absolute value of the angle), but it often does. I've been doing a lot with inverse kinematics lately, and it is very important to get the actual angle, not just the absolute value. That requires knowing both the sine and the cosine.
__________________
www.blake-foster.com
  #8  
Old 21-Mar-2008, 10:32
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,527
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Angle between vectors


Quote:
Originally Posted by Blake
The problem is that the sine and cosine are not 1-1.
Huh? What does that mean?

Quote:
Originally Posted by Blake
For any value of the sine or cosine (except +/-1), there are two possible solutions for the angle in the interval [0, 2pi).

Well, getting back to the problem we are dealing with on this thread: the angle between two unit vectors is usually taken to be the value of angle on [zero, pi) whose cosine is the value given by your dot product formula. This is what acos() provides.
Quote:
Originally Posted by Blake
We need the sine and the cosine to find the exact angle.

Can you give help me understand by giving an example of two vectors, a, and b, for which the following is not sufficient to find the angle between the vectors? (And how you use the magnitude of the cross product to get the correct answer.)
CPP / C++ / C Code:
angle = acos(dotproduct(a, b)/(magnitude(a)*magnitude(b));

Regards,

Dave
  #9  
Old 21-Mar-2008, 10:54
Blake's Avatar
Blake Blake is online now
Member
 
Join Date: Nov 2005
Posts: 167
Blake will become famous soon enough

Re: Angle between vectors


Ok. Suppose we have some angle theta between 0 and pi. By convention theta represents a counterclockwise rotation, while -theta represents a clockwise rotation. However, cos(theta) = cos(-theta). (Or we could use 2pi - theta in place of -theta if we want to constrain the angle to be between 0 and 2pi) Thus if we have only the cosine, we don't know whether the angle represents a clockwise or counterclockwise rotation. Knowing the sine of the angle will fix that problem, because no two angles in the interval [0, 2pi) will have both the same sine and the same cosine.

EDIT: Again, it comes down to whether you just want the absolute value of the angle constrained to the interval [0, pi), (which is given by the inverse cosine) or the angle between 0 and 2pi that you would use to identify the location of a point in polar coordinates. Finding the latter requires knowing both the sine and the cosine.
__________________
www.blake-foster.com
Last edited by Blake : 21-Mar-2008 at 11:25.
  #10  
Old 21-Mar-2008, 11:50
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,527
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Angle between vectors


Quote:
Originally Posted by Blake
Ok. Suppose we have some angle theta between 0 and pi. By convention theta represents a counterclockwise rotation,
But the problem stated by the Original Poster is that a vector repeatedly rotated a random amount for (large) number of times. Given the original vector and the final vector, how can any abiguity in the dot-product based formula be corrected by the cross-product term?

In the context of this problem, If i have rotated + pi/4 or -pi/4 (or one million times 2*pi + pi/4), the angle between the vectors is pi/4.

Now, how about my question about your formulas: Can you give me an example that uses your formulas for cosine and sine to get the correct answer for the case you state, whereas the cosine-based formula by itself gives the wrong answer. I am not challenging your assertion; I am trying to see the "light."

Thanks for your patience.


Regards,

Dave
 

Recent GIDBlogNew Corolla Altis, 10th Generation - Part I by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Generic searching through vectors zdenek CPP / C++ Forum 1 10-Oct-2006 08:44
partition a vector of vectors acosgaya CPP / C++ Forum 0 06-Oct-2006 09:27
Combining Vectors and References Frankg CPP / C++ Forum 7 14-Jan-2006 06:17
vectors of references mirizar CPP / C++ Forum 1 12-Apr-2005 02:02
finding angle between 2 vector jerry CPP / C++ Forum 9 24-Mar-2004 01:20

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 15:20.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.