
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:]
help! thanks in advance

since you are in 2d, 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.

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

of course.....sine, cosine are both predefined and allows you to calculate the angle.
as for idea, I gave you two options already.

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

Great mate... thanks everyone for your input.. ivan guess i get your idea..
If there are easier option do post... thanks alot
cheers

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 evacq8r; 07022006 at 10:51 AM.

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

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

Cool... guess i know how to go about tackling this problem thanks alot everyone
cheers :)

You might look at atan2 to get the corrected angle if you find that you need this.

erm hi jonnin care to elaborate? I don't quite understand. thanks

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.


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)?
please advise... thanks
Similar Threads

Replies: 4
Last Post: 02202010, 04:17 AM

Replies: 0
Last Post: 07182005, 10:46 AM

Replies: 146
Last Post: 08122002, 11:40 PM

By Rob Teixeira in forum .NET
Replies: 129
Last Post: 06062002, 06:23 AM

By Sean Woods in forum VB Classic
Replies: 0
Last Post: 02042002, 08:31 PM
Posting Permissions
 You may not post new threads
 You may not post replies
 You may not post attachments
 You may not edit your posts

Forum Rules

Development Centers
 Android Development Center
 Cloud Development Project Center
 HTML5 Development Center
 Windows Mobile Development Center
