private void LineLineSegmentIntersection(
double x1, double y1,
double x2, double y2,
double x3, double y3,
double x4, double y4, ref double[] p)
{
// x1,y1 P1 coordinates (point of line)
// x2,y2 P2 coordinates (point of line)
// x3,y3 P3 coordinates (point of line segment)
// x4,y4 P4 coordinates (point of line segment)
// p[1],p[2] intersection coordinates
//
// This function returns a pointer array which first index indicates
// weather they intersect on one point or not, followed by coordinate pairs.
double u_a, u_b, denom;
double compConst = 0.0000000000001;
// calculate denominator first
denom = (y4 - y3) * (x2 - x1) - (x4 - x3) * (y2 - y1);
u_a = (x4 - x3) * (y1 - y3) - (y4 - y3) * (x1 - x3);
u_b = (x2 - x1) * (y1 - y3) - (y2 - y1) * (x1 - x3);
//if(fabs(denom-0.0) < compConst && (fabs(u_b-0.0) < compConst && fabs(u_a-0.0) < compConst)){
//printf("denom %.20f u_b %.20f u_a %.20f\n",denom, u_b, u_a);
if (Math.Abs(denom - 0.0) < compConst)
{
if (Math.Abs(u_b - 0.0) < compConst && Math.Abs(u_a - 0.0) < compConst)
{
p[0] = 2.0; // if denominator and numerator equal to zero, lines are coincident
}
else
{
p[0] = 0.0;// if denominator equals to zero, lines are parallel
}
}
else
{
u_b = u_b / denom;
u_a = u_a / denom;
// printf("u_b %.20f\n", u_b);
if (u_b < -compConst || u_b > 1.0 + compConst)
{ // check if it is on the line segment
// printf("line (%.20f, %.20f) (%.20f, %.20f) line seg (%.20f, %.20f) (%.20f, %.20f) \n",x1, y1 ,x2, y2 ,x3, y3 , x4, y4);
p[0] = 0.0;
}
else
{
p[0] = 1.0;
p[1] = x1 + u_a * (x2 - x1); // intersection point
p[2] = y1 + u_a * (y2 - y1);
}
}
}