Visual C++  Finding the Intersection of two Lines Given End Points of Two Lines
Here is the Visual 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 <iostream>
#include <math.h>
void main()
{
float m1, c1, m2, c2;
float x1, y1, x2, y2;
float dx, dy;
float intersection_X, intersection_Y;
std::cout << " Program to find the intersecting point of two lines:\n";
std::cout << "Enter Line1  X1: ";
std::cin >> x1;
std::cout << "Enter Line1  Y1: ";
std::cin >> y1;
std::cout << "Enter Line1  X2: ";
std::cin >> x2;
std::cout << "Enter Line1  Y2: ";
std::cin >> 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
std::cout << "Enter Line2  X1: ";
std::cin >> x1;
std::cout << "Enter Line2  Y1: ";
std::cin >> y1;
std::cout << "Enter Line2  X2: ";
std::cin >> x2;
std::cout << "Enter Line2  Y2: ";
std::cin >> y2;
dx = x2  x1;
dy = y2  y1;
m2 = dy / dx;
c2 = y2  m2 * x2; // which is same as y2  slope * x2
std::cout << "Equation of line1: ";
std::cout << m1 << "X " << ((c1 < 0) ? ' ' : '+') << c1 << "\n";
std::cout << "Equation of line2: ";
std::cout << m2 << "X " << ((c2 < 0) ? ' ' : '+') << c2 << "\n";
if( (m1  m2) == 0)
std::cout << "No Intersection between the lines\n";
else
{
intersection_X = (c2  c1) / (m1  m2);
intersection_Y = m1 * intersection_X + c1;
std::cout << "Intersecting Point: = ";
std::cout << intersection_X;
std::cout << ",";
std::cout << intersection_Y;
std::cout << "\n";
}
}
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 . . .
