Advertisement

Angle between vectors, positive or negative

Started by February 22, 2003 01:54 PM
4 comments, last by botman 22 years ago
Let''s say I have a vector between 2 points in space (2D or 3D). Let''s say I have another vector between the endpoint of the first vector and another point in space (i.e. they share a common point). We know that the angle between them can be determined by the inverse cosine of the dot product between these vectors, but this will always be a positive number. Now, let''s assume, for convenience sake, that these original 2 vectors line in a plane that we''ll call "the ground". I also want to say that if the non-shared point of my second vector is to the "right" of the first vector (as viewed from above the ground), that the angle between these vectors is positive and if the non-shared point of my second vector is to the "left" of the first vector then the angle between them is negative. The simplest way that I can come up with to determine the "sign" of the angle between these vectors is to do a cross product of the first vector with the second vector and examine the Z component of these vectors to see if the cross product vector goes up or down. If vector A cross vector B is positive then the angle between them is "positive". If vector A cross vector B is negative then the angle between them is negative (or it could also be 360 degrees minus the angle between them, if you want). Is there a simpler way to determine whether 2 vectors have an angle that is "positive" or "negative" (i.e. relative to an existing vector) without getting the cross product of the original vectors? botman
Hi,

Answer: I don''t think so. Coz when you think about it. What you do when you cross them, you specify a perpvector to the space they make up. That is, you somehow define a plane to "view" them from. Another possibility is to specify another arbitrary 2dplane and relate to that (project or whatever). Get what I mean? Note though, this is not a really good answer but it''s intuitive.

/Mankind gave birth to God.
/Mankind gave birth to God.
Advertisement
Yes I think there is another way to do this but I don''t know if it is faster:

Given vectors A and B originating from the same point P, and you want to find out whether or not B is on the left or right side of A (Vertex b can basically be considered the forward vector of point P).

When you take the dot product of those two vectors, you get the cosine of the angle between them. Now if B is to the left of A and is at a right angle, the angle is -90 and the cosine of that is 0, however if its to the right of A, the angle is (+)90 but the cosine is still 0! So this is useless for finding out left or right.

However, if B is directly in front of point P, meaning the vectors are parallel and running in the same direction, the angle is 0 and the cosine is = 1. If they are parallel but B is behind P, the angle is 180 and cosine = -1! So in other words using the dot product you can find out if an object is in front of point P (>0) or behind it (<0).

Now if you want to find if the object is to the left or right instead, you take the normalized vector perpendicular to vertex A, and take the dot product between the normal and B. So now you are checking if the point is behind or in front of the normal, which is the same as saying is B to the left or to the right of A.

Unitize B.
Get the normal of A. (normals are unitized)
Get the dot product of the two.
check if positive or negative.

* B
/
/
/
P *-----* A
|
|
* Na (normal of A)

See by taking the dot product Na*B you get the cosine of the angle between them and the sign tells you if B is on the left or right of P->A.
Yes I think there is another way to do this but I don''t know if it is faster:

Given vectors A and B originating from the same point P, and you want to find out whether or not B is on the left or right side of A (Vertex b can basically be considered the forward vector of point P).

When you take the dot product of those two vectors, you get the cosine of the angle between them. Now if B is to the left of A and is at a right angle, the angle is -90 and the cosine of that is 0, however if its to the right of A, the angle is (+)90 but the cosine is still 0! So this is useless for finding out left or right.

However, if B is directly in front of point P, meaning the vectors are parallel and running in the same direction, the angle is 0 and the cosine is = 1. If they are parallel but B is behind P, the angle is 180 and cosine = -1! So in other words using the dot product you can find out if an object is in front of point P (>0) or behind it (<0).

Now if you want to find if the object is to the left or right instead, you take the normalized vector perpendicular to vertex A, and take the dot product between the normal and B. So now you are checking if the point is behind or in front of the normal, which is the same as saying is B to the left or to the right of A.

Unitize B.
Get the normal of A. (normals are unitized)
Get the dot product of the two.
check if positive or negative.

* B
/
/
/
P *-----* A
|
|
* Na (normal of A)

See by taking the dot product Na*B you get the cosine of the angle between them and the sign tells you if B is on the left or right of P->A.
Hello botman! Nice to see you here

If the "ground plane" is always parallel to the XY plane (which I assume it is because you mention checking the Z coordinate previously), then you can use some simple linear algebra since the Z axis is unimportant.

From your description of how you use the cross-product, the point on the second vector is to the "left" of the first vector if it lies to the left of a line colinear with the first vector. Otherwise it is to the right. We can emulate such logic with a linear inequality, such as y < x.

What we would first do is find the equation of a line based on the first vector. Point-slope sounds best.

For example, let's say we have a first vector (0,0) - (9,10) and a second vector (0,0) - (-5,2). Using point slope equation:

First vector:
10 - 0 = m(9 - 0), m = 1.11

We're lucky there was no y-intercept here, but you'd have to find that too We can now plug in the non-common second vector point. If y < 1.11x, it's on the right, and if y > 1.11x, it's on the left. y > 1.11x is true in this example (2 > -5.55), so the point is on the left.

The sign of the slope will determine which inequality corresponds to either left of right.

Hopefully this solution doesn't turn out to be worse in code than the cross product method

[edited by - Zipster on February 22, 2003 10:21:13 PM]
If your points are in the XY plane, then all you need really is the z component of the cross-product, so that''s as fast as you can get:

if (x1*y2 > x2*y1) left, else right

Even if it''s not in the XY plane, it probably works; you can safely ignore the z component (unless your plane is vertical)

Cédric

This topic is closed to new replies.

Advertisement