Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions algorithms/computational-geometry/2d-line-intersection.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include <vector>

// Equation of 2D line is in form ax +by +c =0
std::vector<float> line_intersection_2d(float a1, float b1, float c1,
float a2, float b2, float c2, float slopeTol = 0.01)
{
std::vector<float> intersectonPt;

float slopeDiff = ((a1/b1)-(a2/b2));
if(slopeDiff<0)
{
slopeDiff *=-1;
}
//Checking if lines are parallel
if(slopeDiff < slopeTol )
{
//if parallel, return empty vector
return intersectonPt;
}
float x = (b1*c2-b2*c1)/(a1*b2-a2*b1);
float y = (c1*a2-c2*a1)/(a1*b2-a2*b1);
intersectonPt.push_back(x);
intersectonPt.push_back(y);
return intersectonPt;
}