DevX Home Today's Headlines   Articles Archive   Tip Bank   Forums

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

1. Registered User
Join Date
Jun 2006
Posts
17

Calculating angle between two lines (using C++)

I have a total of four x and y coordinates.
Two of which will be joined as one line and the other two as another line. The problem now is how to find the angle between these two lines?
Is there any pre defined function which allows me to calculate the angle?

[:confused:]

2. Registered User
Join Date
Oct 2005
Posts
173
since you are in 2-d, shift a line so that it shares a common end point with the other.
or find the point where the two lines intersect.
Once you end up with 2 lines and 3 points, it's going to be pretty easy to figure out.

3. Registered User
Join Date
Jun 2006
Posts
17
Erm yeah sound good but is there any pre defined function in c++ language which allow me to calculate the angle between the two line or any idea which you think i could use to calculate it?

cheers mate

4. Registered User
Join Date
Oct 2005
Posts
173
of course.....sine, cosine are both predefined and allows you to calculate the angle.
as for idea, I gave you two options already.

5. Registered User
Join Date
Apr 2006
Location
Zagreb, Croatia
Posts
152
I think the easisest way is to calculate the angle of each line with respect to the X axis. Then simply subtract the calculated angles. To calculate the angles with respect to the x axis use alpha = tan(dy/dx) so if your line's start point is (20,20) and the last point is (80,30) the tan equals 10/60 which is 9,4 degrees. Use the same for another line and then subtract the angles.
Note that you will have to add 90 degrees for obtuse angles

6. Registered User
Join Date
Jun 2006
Posts
17
Great mate... thanks everyone for your input.. ivan guess i get your idea..
If there are easier option do post... thanks alot

cheers

7. Registered User
Join Date
Dec 2003
Posts
38

Try this.

You may like this solution using dot products between two vectors. Suppose you have two lines where the first line passes through the points (x1,y1), (x2,y2), and the second line passes through the points (x3,y3) and (x4,y4). Take the differences between the x and y coordinates for both lines. a = x2 - x1, b = y2 - y1, c = x4 - x3, and d = y4 - y3. The so called dot product yields the equation

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

The angle you'd be interested in is Theta or the angle between the two lines and that can be found using the inverse cosine or Theta = ArcCos( Z ) where of course Z is the somewhat complicated expression involving a,b,c,d. Probably be difficult for you to find a class that contains an interface that readily accomplish this for you. You could just code it up in alot less time. Hope you find this useful.

EVAC
Last edited by evac-q8r; 07-02-2006 at 09:51 AM.

8. Registered User
Join Date
Jun 2006
Posts
17
Hi Evac,

With your idea can i apply the formula regardless of where the two lines is? Even without any contact point at all? Sorry my calculus skill abit poor.

And also i can directly get the angle by inverse cosine Z?
where z = ( (a x c) + (b x d) )/(Sqrt(a^2 + b^2) x Sqrt(b^2 + d^2) ) ?

Thanks alot.

cheers

9. Registered User
Join Date
Dec 2003
Posts
38

Yes

That is correct (for all three questions). Even if the point of intersection between the lines are unknown given they are in the same plane in 2d (guarantees intersection unless parallel) and not 3d (may skew instead)
space. As long as your points for each line are determined by the same coordinate system or axes then the formula is valid. This equation unlike the prior one suggested you don't need to worry about adding or subtracting any angles relative to some axes. If interested look up Scalar Products of Vectors and you should see some pages describing this concept.

EVAC

10. Registered User
Join Date
Jun 2006
Posts
17
Cool... guess i know how to go about tackling this problem thanks alot everyone

cheers :)

11. Senior Member
Join Date
Dec 2003
Posts
3,366
You might look at atan2 to get the corrected angle if you find that you need this.

12. Registered User
Join Date
Jun 2006
Posts
17
erm hi jonnin care to elaborate? I don't quite understand. thanks

13. Senior Member
Join Date
Dec 2003
Posts
3,366
Just read the help file on atan2 for a complete description. In a nutshell you can lose what quadrant the angle was in by using inverse trig functions (for some types of problems). You can correct for this manually of course, but the built in atan2 function corrects it for you. Depending on what you are doing, it may or may not be important to use this function, but at least be aware that it exists.

14. Registered User
Join Date
Jun 2006
Posts
17
roger that thanks

15. Registered User
Join Date
Jun 2006
Posts
17
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)?