DevX Home Today's Headlines   Articles Archive   Tip Bank   Forums

# Thread: Calculating angle between two lines (using C++)

1. Registered User
Join Date
Dec 2003
Posts
38

## Oops... Sorry

Affirmative. I checked the expression several times for accuracy before I responded and I'm extremely disappointed that I failed to catch that mistake. :(

-EVAC  Reply With Quote

2. Registered User
Join Date
Jun 2006
Posts
17
Hi Evac, nah don't be disappointed.. everyone make mistake right? And im extremely grateful that you response to my thread which lead to me solving my problem. Once more thanks for your help and everyone else too!

cheers  Reply With Quote

3. Registered User
Join Date
Dec 2003
Posts
38
Is it possible you could post the code behind this calculation? I'd be interested in seeing how it looks, that is, if you have already completed it.

EVAC  Reply With Quote

4. Registered User
Join Date
Jun 2006
Posts
17
pt1 = x2 - x1;
pt2 = y2 - y1;
pt3 = x4 - x3;
pt4 = y4 - y3;

angle = ( (pt1 * pt3) + (pt2 * pt4) )/((sqrt(pt1*pt1 + pt2*pt2)) * (sqrt(pt3*pt3 + pt4*pt4)) );

result = acos(angle) *180 /PI;

EVAC, here the code.

One more question, since there are a total of 4 points which is 2 lines. So the angle will be calculated from which line to which line? And what is the calculation order of the angle (clockwise, anti-clockwise)? Also if i were to swap the points around, will the result still be consistent?

Help help! thanks
Last edited by larree; 07-03-2006 at 09:38 AM.  Reply With Quote

5. Registered User
Join Date
Dec 2003
Posts
38
Which way the angle is calculated shouldn't really matter. You can only get the same angle but either positive or negative (same absolute value). Just a matter of which line is used for which point... basically just a matter of convention. Here is website explaining a dot product of vectors and the angle in between Website . Keep in mind we are using a vector representation for the lines which is OK since all we really care about is the orientation of the lines.  Reply With Quote

6. Registered User
Join Date
Jul 2006
Posts
1
hey laree,
what about finding the slopes with respect to the x-axis using
slope=(y1-y2)/(x1-x2)=m1=tan@1
. find the slope of each line.
now slope m1=tan @1. similarly find tan@2=m2.
now find @1 and @2 and add them
or simply use
angle between lines @=tan-1( (m1+m2)/1-m1m2)
hope ur problems r over!!!!!
Last edited by ardentmanufan; 07-13-2006 at 09:13 AM. Reason: error  Reply With Quote

7. Registered User
Join Date
Jun 2006
Posts
17
cool thanks for your feedback i get what you mean. By using this formula will i be able narrow down to calculate the angle by clockwise direction only?  Reply With Quote

8. Registered User
Join Date
Jun 2006
Posts
17
Hi everyone, having problem again.. Anyone knows how to calculate the angle in only either clockwise or anti clockwise direction only? Cause now when i calculate the angle two different line facing different direction pop out with the same angle! Help Help help!  Reply With Quote

9. Registered User
Join Date
Jun 2006
Posts
17
evac how should i do in order to get angle in a fixed direction?
Example will be 270 and 90 instead of both 90.. cheers  Reply With Quote

10. Registered User
Join Date
Apr 2012
Posts
1

## Easy angle check of a Vector Originally Posted by larree Hi everyone just to clarify again the formula

Cosine( Theta ) = ( (a x c) + (b x d) )/(Sqrt(a^2 + b^2) x Sqrt(b^2 + d^2) ) = Z.

is correct?

Or is the second part of the sqrt should be (c^2 + d^2) instead of Sqrt(b^2 + d^2)?

I don't believe your system will work about 360 degrees, however if you are suggesting something like this, where a & b are two vectors & you are looking for the angle between them;

float theta = (float)( (a.x* b.x) + (a.y* b.y) );
float aScale = Mathf.Pow( (Mathf.Sqrt(a.x) + Mathf.Sqrt(a.y)) , 0.5f );
float bScale = Mathf.Pow( (Mathf.Sqrt(b.x) + Mathf.Sqrt(b.y)) , 0.5f );
float angle = Mathf.Cos( theta / (aScale * bScale) );
return angle;

then this won't work because getting the power of 0.5 on a negative number will be an imaginary number (as will getting the square root of a negative number)

I've found the best thing to do is work out the angle of each vector seperatly & then check them against each other as bellow;

//do this for each vector
degrees+=180;

You could also rotate the second vector by the first and then do this, the result is the same but most of the time knowing the angle acording to the x-axis is useful

Hope this helps =]  Reply With Quote

11. Registered User
Join Date
Apr 2012
Posts
1

## keep it simple

looking at this a little bit simpler, conceptually:
1. determine which point on each line is closer to the axes origin by finding minimum distance (vector length) [ d = sqrt (x^2 + y^2) ] -- refer to the Pythagorean theorem; furthest point will be the (x2,y2) in steps 2-4, closest point becomes (x1,y1) in next step.
2. determine slope of the line as mentioned above [ m = (y2-y1)/(x2-x1) ] for each line.
3. use the slope (m) and (x2,y2) for each line to calculate new (x1,y1), AKA y-intercept, where x=0 [ y1 = y2 - ( m*x2 ) ].
4. Each line now has points of [(0,y1),(x2,y2)] that reflect original angles.
5. Translate each line "down" the Y axis so that the first point sits atop the axis origin (0,0), yielding (x3 = x2; y3 = y2-y1 where y1 is >= 0 and y3 = y2 + y1 where y1 < 0), giving points [(0,0), (x3,y3)].
6. Angle for each line in degrees is [ result = atan2 (y3,x3) * 180 / PI; ] as mentioned previously.
7. Use sign of each (x3,y3) to determine quadrant for each line and adjust the sign of each [ result ] to calculate the difference correctly.

Yes, this can be optimized, but I find it easier to optimize once I guarantee I am getting the correct answer for all conditions. HTH!  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
• 