Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determining if one 2D vector is to the right or left of another

Given two 2D vectors, how can you tell whether the second is to the right (clockwise) of the first, or to the left (counter-clockwise)?

For instance, in these diagram B is to the right (counter-clockwise) of A

A   B   .       .----> A
^  ¬    |\      |   
| /     | \     |  
|/      V  \    V 
.       B   A   B
like image 567
Eric Avatar asked Nov 04 '12 19:11

Eric


People also ask

How do you find the direction of a 2d vector?

The direction of a vector is the measure of the angle it makes with a horizontal line . tanθ=y2 − y1x2 − x1 , where (x1,y1) is the initial point and (x2,y2) is the terminal point. Example 2: Find the direction of the vector →PQ whose initial point P is at (2,3) and end point is at Q is at (5,8) .

How do you know if two vectors are facing each other?

With the dot product of both forward vectors you can get which direction they are facing. If the dot product is greater than 0 both vectors are pointing in the same direction, if the dot product of both forward vectors is less than 0 the two vectors are facing in opposite directions.


2 Answers

You can achieve this using a dot product. dot(a, b) == a.x*b.x + a.y*b.y can be used to find whether vectors are perpendicular:

var dot = a.x*b.x + a.y*b.y
if(dot > 0)
    console.log("<90 degrees")
else if(dot < 0)
    console.log(">90 degrees")
else
    console.log("90 degrees")

Put another way. dot > 0 tells you if a is "in front of" b.


Assume b is on the right of a. Rotating b 90 degrees counterclockwise puts it in front of a.
Now assume b is on the left of a. Rotating b 90 degrees counterclockwise puts it behind a.

Therefore, the sign of dot(a, rot90CCW(b)) tells you whether b is on the right or left of a, where rot90CCW(b) == {x: -b.y, y: b.x}.

Simplyifying:

var dot = a.x*-b.y + a.y*b.x;
if(dot > 0)
    console.log("b on the right of a")
else if(dot < 0)
    console.log("b on the left of a")
else
    console.log("b parallel/antiparallel to a")
like image 152
Eric Avatar answered Oct 02 '22 21:10

Eric


In the clarification in a comment from @Eric, "if A points forward, which side of it is B on?"

In this formulation the answer is dead-simple. "A" points forward, as in the example, when its x-coordinate is zero. With this assumption, "B" is on the right when its x-coordinate is positive, is on the left when negative, and is neither when zero.

Extending this clarification to "A" in general position means introducing a new coordinate system, as follows: "In a coordinate system where A points forward, ...". The simplest new coordinate system is the one where the basis vectors are A and (1,0). (If A is a multiple of (1,0), then it's just a 90 degree rotation of the basic situation.) The coordinate transform is L : P = (P_x, P_y) --> P' = (P'_x, P'_y) = (A_y * P_x - A_x * P_y, P_y). This kind of linear transformation is called a skew transformation. The test is the sign of the coordinate P'_x. Check that L takes A to the vector (0,1) in the new coordinate system. This method uses the same arithmetic as the other answer.

I wrote this up so that the deeper geometric content may be illuminating.

like image 26
eh9 Avatar answered Oct 02 '22 23:10

eh9