Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to calculate angle between two direction vectors that form a closed/open shape?

I am trying to figure out the correct trig. eq./function to determine the following: The Angle-change (in DEGREES) between two DIRECTION VECTORS(already determined), that represent two line-segment. This is used in the context of SHAPE RECOGTNITION (hand drawn by user on screen).

SO basically,

a) if the user draws a (rough) shape, such as a circle, or oval, or rectangle etc - the lines that makes up that shape are broken down in to say.. 20 points(x-y pairs).

b) I have the DirectionVector for each of these LINE SEGMENTS.

c) So the BEGINNING of a LINE SEGMENT(x0,y0), will the END points of the previous line(so as to form a closed shape like a rectangle, let's say).

SO, my question is , given the context(i.e. determinign the type of a polygon), how does one find the angle-change between two DIRECTION VECTORS(available as two floating point values for x and y) ???

I have seen so many different trig. equations and I'm seeking clarity on this.

Thanks so much in advance folks!

like image 644
ImmortalBuddha Avatar asked Nov 28 '10 00:11

ImmortalBuddha


3 Answers

If (x1,y1) is the first direction vector and (x2,y2) is the second one, it holds:

cos( alpha ) = (x1 * x2 + y1 * y2) / ( sqrt(x1*x1 + y1*y1) * sqrt(x2*x2 + y2*y2) )

sqrt means the square root.

Look up http://en.wikipedia.org/wiki/Dot_product

Especially the section "Geometric Representation".

like image 72
Lagerbaer Avatar answered Nov 02 '22 23:11

Lagerbaer


You could try atan2:

float angle = atan2(previousY-currentY, previousX-currentY);

but also, as the previous answers mentioned, the

angle between two verctors = acos(first.dotProduct(second))

like image 35
George Profenza Avatar answered Nov 02 '22 23:11

George Profenza


I guess you have the vector as three points (x_1, y_1), (x_2, y_2) and (x_3, y_3).

Then you can move the points so that (x_1, y_1) == (0, 0) by

(x_1, y_1) = (x_2, y_2) - (x_1, y_1) (x_2, y_2) = (x_3, y_3) - (x_1, y_1)

Now you have this situation:

enter image description here

Think of this triangle as two right-angled triangles. The first one has the angle alpha and a part of beta, the second right-angled triangle has the other part of beta.

Then you can apply:

enter image description here

You can calculate alpha like this:

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

like image 35
Martin Thoma Avatar answered Nov 02 '22 23:11

Martin Thoma