Skip to content

Instantly share code, notes, and snippets.

@hfossli-agens
Last active December 14, 2015 00:09
Show Gist options
  • Save hfossli-agens/4997266 to your computer and use it in GitHub Desktop.
Save hfossli-agens/4997266 to your computer and use it in GitHub Desktop.
BOOL AGLineIntersection(AGLine l1, AGLine l2, AGPoint *out_pointOfIntersection)
{
// http://stackoverflow.com/a/565282/202451
AGPoint p = l1.start;
AGPoint q = l2.start;
AGPoint r = AGPointSubtract(l1.end, l1.start);
AGPoint s = AGPointSubtract(l2.end, l2.start);
double s_r_crossProduct = AGPointCrossProduct(r, s);
double t = AGPointCrossProduct(AGPointSubtract(q, p), s) / s_r_crossProduct;
double u = AGPointCrossProduct(AGPointSubtract(q, p), r) / s_r_crossProduct;
if(t < 0 || t > 1.0 || u < 0 || u > 1.0)
{
if(out_pointOfIntersection != NULL)
{
*out_pointOfIntersection = AGPointZero;
}
return NO;
}
else
{
if(out_pointOfIntersection != NULL)
{
AGPoint i = AGPointAdd(p, AGPointMultiply(r, t));
*out_pointOfIntersection = i;
}
return YES;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment