How to resolve the algorithm Find the intersection of two lines step by step in the C++ programming language
Published on 7 June 2024 03:52 AM
How to resolve the algorithm Find the intersection of two lines step by step in the C++ programming language
Table of Contents
Problem Statement
Find the point of intersection of two lines in 2D.
The 1st line passes though (4,0) and (6,10) . The 2nd line passes though (0,3) and (10,7) .
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Find the intersection of two lines step by step in the C++ programming language
This C++ program calculates the intersection point of two lines using the line-line intersection algorithm. Here's how it works:
-
Header Includes:
- The program includes necessary header files for input/output, mathematical functions, and assertions.
-
Determinant Calculation:
- The
Det
function is defined to calculate the determinant of a 2x2 matrix[a b; c d]
, which is used later in the line-line intersection calculations.
- The
-
Line-Line Intersection Function:
- The
LineLineIntersect
function takes the start and end points of two lines as input and calculates their intersection point, if it exists. - It calculates the determinant of each line and uses Cramer's rule to solve for the intersection point.
- If the determinant is zero, the lines are parallel and don't intersect, so it returns
false
. - If the intersection point is valid and finite, it returns
true
and sets the output parametersixOut
andiyOut
to the coordinates of the intersection point.
- The
-
Main Function:
- In the
main
function:- It defines the start and end points of two lines (
Line 1
andLine 2
). - It calls the
LineLineIntersect
function to calculate the intersection point of the two lines and stores the result inresult
,ix
, andiy
. - It prints the result.
- It uses assertions to check the correctness of the intersection point.
- It defines the start and end points of two lines (
- In the
-
Program Execution:
- For the given input lines (
Line 1
andLine 2
), the program calculates the intersection point and prints the result. - The assertions verify that the intersection point is correct, with a tolerance of
eps
.
- For the given input lines (
In summary, this program demonstrates how to calculate the intersection point of two lines using the line-line intersection algorithm. It handles the case of parallel lines (no intersection) and performs checks to ensure the validity of the intersection point.
Source code in the cpp programming language
#include <iostream>
#include <cmath>
#include <cassert>
using namespace std;
/** Calculate determinant of matrix:
[a b]
[c d]
*/
inline double Det(double a, double b, double c, double d)
{
return a*d - b*c;
}
/// Calculate intersection of two lines.
///\return true if found, false if not found or error
bool LineLineIntersect(double x1, double y1, // Line 1 start
double x2, double y2, // Line 1 end
double x3, double y3, // Line 2 start
double x4, double y4, // Line 2 end
double &ixOut, double &iyOut) // Output
{
double detL1 = Det(x1, y1, x2, y2);
double detL2 = Det(x3, y3, x4, y4);
double x1mx2 = x1 - x2;
double x3mx4 = x3 - x4;
double y1my2 = y1 - y2;
double y3my4 = y3 - y4;
double denom = Det(x1mx2, y1my2, x3mx4, y3my4);
if(denom == 0.0) // Lines don't seem to cross
{
ixOut = NAN;
iyOut = NAN;
return false;
}
double xnom = Det(detL1, x1mx2, detL2, x3mx4);
double ynom = Det(detL1, y1my2, detL2, y3my4);
ixOut = xnom / denom;
iyOut = ynom / denom;
if(!isfinite(ixOut) || !isfinite(iyOut)) // Probably a numerical issue
return false;
return true; //All OK
}
int main()
{
// **Simple crossing diagonal lines**
// Line 1
double x1=4.0, y1=0.0;
double x2=6.0, y2=10.0;
// Line 2
double x3=0.0, y3=3.0;
double x4=10.0, y4=7.0;
double ix = -1.0, iy = -1.0;
bool result = LineLineIntersect(x1, y1, x2, y2, x3, y3, x4, y4, ix, iy);
cout << "result " << result << "," << ix << "," << iy << endl;
double eps = 1e-6;
assert(result == true);
assert(fabs(ix - 5.0) < eps);
assert(fabs(iy - 5.0) < eps);
return 0;
}
You may also check:How to resolve the algorithm Kronecker product step by step in the Rust programming language
You may also check:How to resolve the algorithm Unix/ls step by step in the C++ programming language
You may also check:How to resolve the algorithm 21 game step by step in the C++ programming language
You may also check:How to resolve the algorithm Greatest element of a list step by step in the Dart programming language
You may also check:How to resolve the algorithm Greedy algorithm for Egyptian fractions step by step in the C++ programming language