sandbox-intersection/line_intersection.cpp
2025-04-15 15:11:37 -05:00

28 lines
548 B
C++

vec2 line_intersection(vec2 a1, vec2 a2,
vec2 b1, vec2 b2)
{
float x1 = a1.x;
float y1 = a1.y;
float x2 = a2.x;
float y2 = a2.y;
float x3 = b1.x;
float y3 = b1.y;
float x4 = b2.x;
float y4 = b2.y;
float x1x2 = x1 - x2;
float x1x3 = x1 - x3;
float x3x4 = x3 - x4;
float y1y2 = y1 - y2;
float y1y3 = y1 - y3;
float y3y4 = y3 - y4;
float div = 1.0f / (x1x2 * y3y4 - y1y2 * x3x4);
float t = (x1x3 * y3y4 - y1y3 * x3x4) * div;
float u = (x1x2 * y1y3 - y1y2 * x1x3) * div;
return {t, u};
}