 # How to find angle between two lines?

Given the two coordinate points located on each line, how can i find the angle between both the lines?

1 Like

Suppose the two point of line 1 are : (x11,y11) and (x12,y12)

similarly the two point of line 2 are : (x21,y21) and (x22,y22)

now as we know from vector algebra we can express the first line vectorially as : (x12-x11)i + (y12-y11) j

and the second line as : (x22-x21)i + (y22-y21) j

Now We have two vectors and we can easily calculate the angle between them by dot product:

let A = (x12-x11)i + (y12-y11) j

``````B = (x22-x21)i + (y22-y21) j
``````

we know dot product of two vectors A.B = Ax * Bx + Ay* By + Az*Bz = |A| * |B| * cos(angle between A and B)

also we know that magnitude of A = |A| = sqrt(Ax^2 + Ay^2)

magnitude of B = |B| = sqrt(Bx^2 + By^2)

You have all the data here so you can easily compute the angle.

Some Reference :

Vector Between Two Points

Dot Product

Also if you want you can follow another process which computes angle using slopes. As given here

3 Likes

#define PI 3.14159265

``````m1 = (y12 - y11) / (x12 - x11);   // tan(A)
A = atan(m1) * 180 / PI;
``````

A = angle between x-axis and line 1

``````m2 = (y22 - y21) / (x22 - x21);  // tan(B)
B = atan(m2) * 180 / PI;
``````

B = angle between x-axis and line 2

Angle between line 1 and line 2 = A - B

3 Likes

Include math.h and then use the following formula:

atan((y2-y1)/(x2-x1))

This will give you desired angle in radians.
Similarly find the same for the other line and subtract for the angle between two lines.

Use this formula to convert into degrees: PI radian = 180 degrees

1 Like

#include<stdio.h>
#include<math.h>
int main()
{
int x,y;
float m,k,a;
printf(“enter the coordinates of 1st point on line 1 “);
scanf(”%d%d”,&x,&y);
printf(“enter the coordinates of 2nd point on line 1 “);
scanf(”%d%d”,&x,&y);
printf(“enter the coordinate of 1st point on line 2 “);
scanf(”%d%d”,&x,&y);
printf(“enter the coordinate of 2nd point on line 2 “);
scanf(”%d%d”,&x,&y);

``````	m=(y-y)/(x-x); //slope of line1
m=(y-y)/(x-x); //slope of line2
k=(m-m)/(1+m*m);
if(k<0)
{
k=-k;
}
printf("angle between these two lines in degree is %f",a);
}``````