Find line intersection and fix mistake in 2 C algorithm
Budget: $30 – $250 USD
Hi, I am searching for a freelancer who can explain algorithm 2 to find an intersection between two edges. The two edges in my code are specified through (a, b) and (c, d). 0 is the x-coordinate and 1 the y-coordinate. a, b, c, d arrays are float. Would be great if you could keep the structure.
Ideally you have some knowledge in C and geometry. The first algorithm is an implementation I wrote using determinants. But I just noticed that it fails in some scenarios. The second one is an algorithm I found online. But I cannot wrap my head around the triangle calculation. The job should be finished within a day. It should not be too hard. If you have some ideas to optimize the solution please let me know.
(1) Construct some examples and test both algorithms. pts1 and pts2 are just arrays from which the 4 points for the edges are extracted.
(2) Explain algorithm 2 for line intersection in text document (how do the triangle calculations work)
(3) Fix algorithm 2. Why are the results incorrect?
// Find intersection 1
// Extract edge points
a[0] = pts1[2 * i];
a[1] = pts1[2 * i + 1];
b[0] = pts1[2 * ((i + 1) % 4)];
b[1] = pts1[2 * ((i + 1) % 4) + 1];
c[0] = pts2[2 * j];
c[1] = pts2[2 * j + 1];
d[0] = pts2[2 * ((j + 1) % 4)];
d[1] = pts2[2 * ((j + 1) % 4) + 1];
// Calculate determinant
float det = ((a[0] - b[0]) * (c[1] - d[1])) - ((a[1] - b[1]) * (c[0] - d[0]));
// Check for no intersection and coinciding lines
if (det == 0.)
return false;
// Calculate intersection point
temp_pts[0] = (((a[0] * b[1] - a[1] * b[0]) * (c[0] - d[0])) - ((a[0] - b[0]) * (c[0] * d[1] - c[1] * d[0]))) / det;
temp_pts[1] = (((a[0] * b[1] - b[0] * a[1]) * (c[1] - d[1])) - ((a[1] - b[1]) * (c[0] * d[1] - c[1] * d[0]))) / det;
return true;
// Find intersection 2
float a[2];
float b[2];
float c[2];
float d[2];
float area_abc, area_abd, area_cda, area_cdb;
a[0] = pts1[2 * i];
a[1] = pts1[2 * i + 1];
b[0] = pts1[2 * ((i + 1) % 4)];
b[1] = pts1[2 * ((i + 1) % 4) + 1];
c[0] = pts2[2 * j];
c[1] = pts2[2 * j + 1];
d[0] = pts2[2 * ((j + 1) % 4)];
d[1] = pts2[2 * ((j + 1) % 4) + 1];
area_abc = trangle_area(a, b, c);
area_abd = trangle_area(a, b, d);
if(area_abc * area_abd >= -1e-5) {
return false;
}
area_cda = trangle_area(c, d, a);
area_cdb = area_cda + area_abc - area_abd;
if (area_cda * area_cdb >= -1e-5) {
return false;
}
float t = area_cda / (area_abd - area_abc);
float dx = t * (b[0] - a[0]);
float dy = t * (b[1] - a[1]);
temp_pts[0] = a[0] + dx;
temp_pts[1] = a[1] + dy;
return true;
float trangle_area(float* a, float* b, float* c) {
return ((a[0] - c[0]) * (b[1] - c[1]) - (a[1] - c[1]) * (b[0] - c[0])) / 2.0;
}
Ideally you have some knowledge in C and geometry. The first algorithm is an implementation I wrote using determinants. But I just noticed that it fails in some scenarios. The second one is an algorithm I found online. But I cannot wrap my head around the triangle calculation. The job should be finished within a day. It should not be too hard. If you have some ideas to optimize the solution please let me know.
(1) Construct some examples and test both algorithms. pts1 and pts2 are just arrays from which the 4 points for the edges are extracted.
(2) Explain algorithm 2 for line intersection in text document (how do the triangle calculations work)
(3) Fix algorithm 2. Why are the results incorrect?
// Find intersection 1
// Extract edge points
a[0] = pts1[2 * i];
a[1] = pts1[2 * i + 1];
b[0] = pts1[2 * ((i + 1) % 4)];
b[1] = pts1[2 * ((i + 1) % 4) + 1];
c[0] = pts2[2 * j];
c[1] = pts2[2 * j + 1];
d[0] = pts2[2 * ((j + 1) % 4)];
d[1] = pts2[2 * ((j + 1) % 4) + 1];
// Calculate determinant
float det = ((a[0] - b[0]) * (c[1] - d[1])) - ((a[1] - b[1]) * (c[0] - d[0]));
// Check for no intersection and coinciding lines
if (det == 0.)
return false;
// Calculate intersection point
temp_pts[0] = (((a[0] * b[1] - a[1] * b[0]) * (c[0] - d[0])) - ((a[0] - b[0]) * (c[0] * d[1] - c[1] * d[0]))) / det;
temp_pts[1] = (((a[0] * b[1] - b[0] * a[1]) * (c[1] - d[1])) - ((a[1] - b[1]) * (c[0] * d[1] - c[1] * d[0]))) / det;
return true;
// Find intersection 2
float a[2];
float b[2];
float c[2];
float d[2];
float area_abc, area_abd, area_cda, area_cdb;
a[0] = pts1[2 * i];
a[1] = pts1[2 * i + 1];
b[0] = pts1[2 * ((i + 1) % 4)];
b[1] = pts1[2 * ((i + 1) % 4) + 1];
c[0] = pts2[2 * j];
c[1] = pts2[2 * j + 1];
d[0] = pts2[2 * ((j + 1) % 4)];
d[1] = pts2[2 * ((j + 1) % 4) + 1];
area_abc = trangle_area(a, b, c);
area_abd = trangle_area(a, b, d);
if(area_abc * area_abd >= -1e-5) {
return false;
}
area_cda = trangle_area(c, d, a);
area_cdb = area_cda + area_abc - area_abd;
if (area_cda * area_cdb >= -1e-5) {
return false;
}
float t = area_cda / (area_abd - area_abc);
float dx = t * (b[0] - a[0]);
float dy = t * (b[1] - a[1]);
temp_pts[0] = a[0] + dx;
temp_pts[1] = a[1] + dy;
return true;
float trangle_area(float* a, float* b, float* c) {
return ((a[0] - c[0]) * (b[1] - c[1]) - (a[1] - c[1]) * (b[0] - c[0])) / 2.0;
}