I'm looking over a few functions provided in a geometry class and I found this very poorly commented function that apparently tests whether or not there is an intersection between two lines. Can someone please explain to me how this works?
inline bool LineIntersection2D(Vector2D A,
Vector2D B,
Vector2D C,
Vector2D D)
{
double rTop = (A.y-C.y)*(D.x-C.x)-(A.x-C.x)*(D.y-C.y);
double sTop = (A.y-C.y)*(B.x-A.x)-(A.x-C.x)*(B.y-A.y);
double Bot = (B.x-A.x)*(D.y-C.y)-(B.y-A.y)*(D.x-C.x);
if (Bot == 0)//parallel
{
return false;
}
double invBot = 1.0/Bot;
double r = rTop * invBot;
double s = sTop * invBot;
if( (r > 0) && (r < 1) && (s > 0) && (s < 1) )
{
//lines intersect
return true;
}
//lines do not intersect
return false;
}
From what I've gathered, A and B are the two points of the first line and C and D are the two points of the second. After that I'm totally lost. Thanks in advance!