You should return false if the denominators are zero.
btw You can optimize this function by a lot. Denominators for uA and uB are exactly the same so you should compute the denominator once and save it to a variable. Then you can reuse the variable for both uA and uB.
Also certain subtractions is you algorithm are done twice.
This is my C++ version:
fn Intersect
(line Line1, line Line2)
{
v2 A = Line1.Start;
v2 B = Line1.End;
v2 C = Line2.Start;
v2 D = Line2.End;
v2 BA = B - A;
v2 DC = D - C;
v2 AC = A - C;
f32 Denominator = DC.Y*BA.X - DC.X*BA.Y;
if(Denominator == 0)
return false;
f32 U1 = (DC.X*AC.Y - DC.Y*AC.X) / Denominator;
f32 U2 = (BA.X*AC.Y - BA.Y*AC.X) / Denominator;
return U1 >= 0 && U1 <= 1 && U2 >= 0 && U2 <= 1;
}
You should return false if the denominators are zero.
btw You can optimize this function by a lot. Denominators for uA and uB are exactly the same so you should compute the denominator once and save it to a variable. Then you can reuse the variable for both uA and uB.
Also certain subtractions is you algorithm are done twice.
This is my C++ version:
fn Intersect (line Line1, line Line2) { v2 A = Line1.Start; v2 B = Line1.End; v2 C = Line2.Start; v2 D = Line2.End; v2 BA = B - A; v2 DC = D - C; v2 AC = A - C; f32 Denominator = DC.Y*BA.X - DC.X*BA.Y; if(Denominator == 0) return false; f32 U1 = (DC.X*AC.Y - DC.Y*AC.X) / Denominator; f32 U2 = (BA.X*AC.Y - BA.Y*AC.X) / Denominator; return U1 >= 0 && U1 <= 1 && U2 >= 0 && U2 <= 1; }