![]() |
|
#1
|
|||
|
|||
Angle between vectorsHi,
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:
|
|||
|
#2
|
||||
|
||||
Re: Angle between vectorsTo 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
|
|||
|
|||
Re: Angle between vectorsAll 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:
|
|
#4
|
||||
|
||||
Re: Angle between vectorsI'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:
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
|
|||
|
|||
Re: Angle between vectorsNo idea what u all talking here.
__________________
Linux is the best OS in the world. |
|
#6
|
|||
|
|||
Re: Angle between vectorsQuote:
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
|
||||
|
||||
Re: Angle between vectorsThe 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
|
|||
|
|||
Re: Angle between vectorsQuote:
Quote:
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:
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:
Regards, Dave |
|
#9
|
||||
|
||||
Re: Angle between vectorsOk. 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
|
|||
|
|||
Re: Angle between vectorsQuote:
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 GIDBlog
New Corolla Altis, 10th Generation - Part I by Nihal
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
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