boolean void TwoSegmentIntersections(
Vector seg1p1, Vector seg1p2,
Vector seg2p1, Vector seg2p2,
out Vector oIntersection)
{
// point d'intersection au besoin
oIntersection = Vector.zero;
Vector vseg1 = seg1p2 - seg1p1;
Vector vseg2 = seg2p2 - seg2p1;
// colinear test
float crossProduct = vseg1.x * vseg2.y - vseg1.y * vseg2.x;
if (crossProduct == 0)
{
//vecteur colineraire donc pas d'intersection possible
return false;
}
// projection des vecteurs
Vector c = seg2p1 - seg1p1;
float t = (c.x * vseg2.y - c.y * vseg2.x) / crossProduct;
if ((t < 0) || (t > 1))
{
return false;
}
float u = (c.x * vseg1.y - c.y * vseg1.x) / crossProduct;
if ((u < 0) || (u > 1))
{
return false;
}
oIntersection = seg1p1 + t * vseg1;
return true;
}