Turbo C  Finding the Intersection of two Lines Given End Points of Two Lines
Here is the Turbo C program for Finding the Intersection of two Lines Given End Points of Two Lines.
Find out the equation for the two lines seperately Y = MX + C
Where M = Slope of a Line and C = Intercept
To Find the slope of a line
slope = (y2  y1) / (x2  x1)
To Find the intercept of the line, intercept = y1  (slope) * x1
which would be same as, intercept = y2  (slope) * x2
If the difference between the slope of two lines are ZERO, then there is no intersection.
Otherwise the intersecting point is calculated using the following formula:
intersection_X = (c2  c1) / (m1  m2);
intersection_Y = m1 * intersection_X + c1;
Where c1 and c2 are intercepts of two lines and m1 and m2 are slope for the two lines.
NOTE: This program does not guarantee that the intersecting point lies on the line or not. It just informs us the projected lines from the either direction will meet at some point. To get the intersecting point of two line segments in which the intersecting point lies on the two lines, click here.
Source Code
#include <stdio.h>
#include <math.h>
void main()
{
float m1, c1, m2, c2;
float x1, y1, x2, y2;
float dx, dy;
float intersection_X, intersection_Y;
printf("Program to find the intersecting point of two lines:\n");
printf("Enter Line1  X1: ");
scanf("%f", &x1);
printf("Enter Line1  Y1: ");
scanf("%f", &y1);
printf("Enter Line1  X2: ");
scanf("%f", &x2);
printf("Enter Line1  Y2: ");
scanf("%f", &y2);
dx = x2  x1;
dy = y2  y1;
m1 = dy / dx;
// y = mx + c
// intercept c = y  mx
c1 = y1  m1 * x1; // which is same as y2  slope * x2
printf("Enter Line2  X1: ");
scanf("%f", &x1);
printf("Enter Line2  Y1: ");
scanf("%f", &y1);
printf("Enter Line2  X2: ");
scanf("%f", &x2);
printf("Enter Line2  Y2: ");
scanf("%f", &y2);
dx = x2  x1;
dy = y2  y1;
m2 = dy / dx;
// y = mx + c
// intercept c = y  mx
c2 = y1  m2 * x1; // which is same as y2  slope * x2
printf("Equation of line1: Y = %.2fX %c %.2f\n", m1, (c1 < 0) ? ' ' : '+', c1);
printf("Equation of line2: Y = %.2fX %c %.2f\n", m2, (c2 < 0) ? ' ' : '+', c2);
if( (m1  m2) == 0)
printf("No Intersection between the lines\n");
else
{
intersection_X = (c2  c1) / (m1  m2);
intersection_Y = m1 * intersection_X + c1;
printf("Intersecting Point: = %.2f, %.2f\n", intersection_X, intersection_Y);
}
}
Output
Program to find the intersecting point of two lines:
Enter Line1  X1: 1
Enter Line1  Y1: 2
Enter Line1  X2: 5
Enter Line1  Y2: 7
Enter Line2  X1: 3
Enter Line2  Y1: 3
Enter Line2  X2: 4
Enter Line2  Y2: 5
Equation of line1: Y = 1.25X + 0.75
Equation of line2: Y = 2.00X 3.00
Intersecting Point: = 5.00, 7.00
Press any key to continue . . .
Program to find the intersecting point of two lines:
Enter Line1  X1: 1
Enter Line1  Y1: 3
Enter Line1  X2: 5
Enter Line1  Y2: 7
Enter Line2  X1: 4
Enter Line2  Y1: 6
Enter Line2  X2: 8
Enter Line2  Y2: 10
Equation of line1: Y = 1.00X + 2.00
Equation of line2: Y = 1.00X + 2.00
No Intersection between the lines
Press any key to continue . . .
