Last active
December 14, 2015 00:09
-
-
Save hfossli-agens/4997266 to your computer and use it in GitHub Desktop.
C implementation of http://stackoverflow.com/a/565282/202451
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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