我有两行。两条线都包含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); } }
假设您有两行表格Ax + By = C,可以很容易地找到它:
Ax + By = C
float delta = A1 * B2 - A2 * B1; if (delta == 0) throw new ArgumentException("Lines are parallel"); float x = (B2 * C1 - B1 * C2) / delta; float y = (A1 * C2 - A2 * C1) / delta;
从这里拉