У меня есть 2 строки. Обе линии содержат 2 точки X и Y. Это означает, что они обе имеют длину.
Я вижу 2 формулы, одна с использованием определителей и одна с использованием нормальной алгебры. Что было бы наиболее эффективным для расчета и как выглядит формула?
Мне трудно использовать матрицы в коде.
Это то, что я имею до сих пор, это может быть более эффективным?
public static Vector3 Intersect(Vector3 line1V1, Vector3 line1V2, Vector3 line2V1, Vector3 line2V2)
{
//Line1
float A1 = line1V2.Y - line1V1.Y;
float B1 = line1V2.X - line1V1.X;
float C1 = A1*line1V1.X + B1*line1V1.Y;
//Line2
float A2 = line2V2.Y - line2V1.Y;
float B2 = line2V2.X - line2V1.X;
float C2 = A2 * line2V1.X + B2 * line2V1.Y;
float det = A1*B2 - A2*B1;
if (det == 0)
{
return null;//parallel lines
}
else
{
float x = (B2*C1 - B1*C2)/det;
float y = (A1 * C2 - A2 * C1) / det;
return new Vector3(x,y,0);
}
}