Can someone tell me what's wrong, bcos my head is burning at the moment and I can't look at it anymore:
/// <summary>
/// Tests if the passed lines intersect.
/// Input: two finite segments
/// Output: intersectPoint1 = intersect point (when it exists)
/// intersectPoint2 = endpoint of intersect segment [intersectPoint1,intersect
Point2] (when it exists)
/// Return: 0=disjoint (no intersect) or the lines only touch each other at the start or end point
/// 1=intersect in unique point intersectPoint1
/// 2=overlap in segment from intersectPoint1 to intersectPoint2
/// </summary>
/// <param name="start1">start point of the first line</param>
/// <param name="end1">end point of the first line</param>
/// <param name="start2">start point of the second line</param>
/// <param name="end2">end point of the second line</param>
/// <param name="intersectPoint1">out
put: intersect point (when it exists)</param>
/// <param name="intersectPoint2">out
put: endpoint of intersect segment [intersectPoint1,intersect
Point2] (when it exists)</param>
/// <returns>0=disjoint (no intersect) or the lines only touch each other at the start or end point
/// 1=intersect in unique point intersectPoint1
/// 2=overlap in segment from intersectPoint1 to intersectPoint2</returns>
private byte linesIntersect(DPoint start1, DPoint end1, DPoint start2, DPoint end2,
out DPoint intersectPoint1, out DPoint intersectPoint2)
{
//line1 to form Ax+By=C
double A1, B1, C1;
A1 = end1.Y - start1.Y;
B1 = start1.X - end1.X;
C1 = A1 * start1.X + B1 * start1.Y;
//line2 to form Ax+By=C
double A2, B2, C2;
A2 = end2.Y - start2.Y;
B2 = start2.X - end2.X;
C2 = A2 * start2.X + B2 * start2.Y;
//find the intersection (if it exists)
intersectPoint1 = new DPoint();
intersectPoint2 = new DPoint();
double det =A1 * B2 - A2 * B1;
if (det == 0)// => lines are parallel
{
if (isPointOnLine(start1, end1, start2))
{
if (isPointOnLine(start1, end1, end2))
{
intersectPoint1 = start2;
intersectPoint2 = end2;
return 2;
}
else if (isPointOnLine(start2, end2, end1))
{
intersectPoint1 = start2;
intersectPoint2 = end1;
return 2;
}
}
else if (isPointOnLine(start2, end2, start1))
{
if (isPointOnLine(start1, end1, end2))
{
intersectPoint1 = start1;
intersectPoint2 = end2;
return 2;
}
else if (isPointOnLine(start2, end2, end1))
{
intersectPoint1 = start1;
intersectPoint2 = end1;
return 2;
}
}
return 0;
}
else
{
intersectPoint1.X = (B2 * C1 - B1 * C2) / det;
intersectPoint1.Y = (A1 * C2 - A2 * C1) / det;
if (isPointOnLine(start1, end1, intersectPoint1) ||
isPointOnLine(start2, end2, intersectPoint1))
{
if ((start1 == intersectPoint1) ||
(start2 == intersectPoint1) ||
(end1 == intersectPoint1) ||
(end2 == intersectPoint1))
{
intersectPoint1 = new DPoint();
return 0;
}
return 1;
}
else
{
intersectPoint1 = new DPoint();
return 0;
}
}
}
THANKS IN ADVANCE!
Start Free Trial