public bool IntersectionWith(Line line2, out Vector Intersection) { Intersection = new Vector(Dimensions); // Lösen der Gleichung n*Direction + P1 = m*Direction2 + P2 // => P2 - P1 = n*Direction - m*Direction2 // => P2 - P1 = [Direction | Direction2] * (n, m)t // => (n,m)t = [Direction | Direction2]^-1 * P2 - P1 Vector Pdiff = line2.P1 - P1; Transformations.Matrix M = new Transformations.Matrix(Dimensions, 2); M.SetColumn(0, Direction); M.SetColumn(1, line2.Direction); Transformations.Matrix Minv; if (!M.Invert(out Minv)) { return(false); } Vector nm = Minv.RightMul(Pdiff); Intersection.Set((nm[0] * Direction + P1).coordinates); return(true); }
/// <summary> /// Invertierung nach Austauschverfahren (Pivot) /// y = M x -> x = MInv y /// </summary> /// <param name="Minv"></param> /// <returns></returns> public bool Invert(out Matrix Minv) { Minv = new Matrix(this); int[] ixX = IndexGenerator(Dimensions).ToArray(); bool[] Xchanged = BoolFieldGenerator(false, Dimensions).ToArray(); int[] ixY = IndexGenerator(CountLines).ToArray(); bool[] Ychanged = BoolFieldGenerator(false, Dimensions).ToArray(); int pivotC = 0, pivotL = 0; try { // Tauschen aller x gegen alle y for (pivotC = 0; pivotC < Math.Min(this.Dimensions, this.CountLines); pivotC++) { // Suchen in aktueller Spalte nach einem Koeff für eine Zeile, deren y noch nicht getauscht wurde pivotL = ColAsTriple(Minv, pivotC, Ychanged).Where(line => line.Item2 != 0 && !line.Item3).First().Item1; // x und y tauschen. Tausch protokollieren int tausch = ixX[pivotC]; ixX[pivotC] = ixY[pivotL]; ixY[pivotL] = tausch; Xchanged[pivotC] = Ychanged[pivotL] = true; // Inversion in Hilfsmatrix durchführen Matrix M2 = new Matrix(Minv); double p = Minv[pivotC, pivotL]; M2.SetLine(pivotL, Minv.Line(pivotL) * (-1 / p)); M2.SetColumn(pivotC, Minv.Column(pivotC) * (1 / p)); M2[pivotL, pivotC] = 1 / p; for (int line = 0; line < Dimensions; line++) { if (line != pivotL) { for (int col = 0; col < Dimensions; col++) { if (col != pivotC) { M2[line, col] = Minv[line, col] + -Minv[line, pivotC] * Minv[pivotL, col] / p; } } } } // Hilfsmatrix zur neuen gültigen erklären Minv = M2; } // Umsortieren der Zeilen Matrix M3 = new Matrix(Minv.CountLines, Minv.Dimensions); for (int line = 0; line < CountLines; line++) { M3.SetLine(ixY[line], Minv.Line(line)); } Matrix M4 = new Matrix(Minv.CountLines, Minv.Dimensions); for (int col = 0; col < Dimensions; col++) { M4.SetColumn(ixX[col], M3.Column(col)); } Minv = M4; } catch (Exception) { return(false); } // Reihenfolge wiederherstellen return(true); }