Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculate direction angle from two vectors?

Say I have two 2D vectors, one for an objects current position and one for that objects previous position. How can I work out the angular direction of travel?

This image might help understand what I'm after:

(image) http://files.me.com/james.ingham/crcvmy

like image 575
ingh.am Avatar asked Apr 03 '10 11:04

ingh.am


People also ask

How do you find the directional angle between two vectors?

To calculate the angle between two vectors in a 2D space: Find the dot product of the vectors. Divide the dot product by the magnitude of the first vector. Divide the resultant by the magnitude of the second vector.

What is the formula for angle between two vectors?

Formula for angle between two Vectors The cosine of the angle between two vectors is equal to the sum of the product of the individual constituents of the two vectors, divided by the product of the magnitude of the two vectors. =| A | | B | cosθ.

What is the direction angle formula?

The direction angle, θ , of the vector, v=ai+bj v = a i + b j , can be found using the arctangent of the ratio of the vertical component of the vector and the horizontal component of the vector. θ=tan−1ba θ = tan − 1 ⁡ However, this is only the reference angle.


1 Answers

The direction vector of travel will be the difference of the two position vectors,

d = (x1, y1) - (x, y) = (x1 - x, y1 - y)

Now when you ask for the direction angle, that depends what direction you want to measure the angle against. Is it against the x axis? Go with Radu's answer. Against an arbitrary vector? See justjeff's answer.

Edit: To get the angle against the y-axis:

tan (theta) = (x1 -x)/(y1 - y)          

the tangent of the angle is the ratio of the x-coordinate of the difference vector to the y-coordinate of the difference vector.

So

theta = arctan[(x1 - x)/(y1 - y)]

Where arctan means inverse tangent. Not to be confused with the reciprocal of the tangent, which many people do, since they're both frequently denoted tan^-1. And make sure you know whether you're working in degrees or radians.

like image 137
Rob Lachlan Avatar answered Oct 20 '22 20:10

Rob Lachlan