/// <summary> /// This method calculates the distance to a Line L /// The parameter Lam1 can be taken to calculate the point, which has the lowest distance to /// the other Line L . Nearest1 = Value(Lam1). /// The parameter Lam2 can be taken to calculate the point of L , which has the lowest distance to /// the Linetype this. Nearest2 = L.Value(Lam2). /// </summary> /// <param name="L">The other LineType</param> /// <param name="Lam1">Paramter which belongs to this line : Nearest1 = Value(Lam1)</param> /// <param name="Lam2">Paramter which belongs to the Line L: Nearest2 = L.Value(Lam2)</param> /// <param name="Nearest1">The point on the Line, which has the smallest distance to the Line L</param> /// <param name="Nearest2">The point on the Line L, which has the smallest distance </param> /// <returns>Returns the distance to the line L</returns> public double Distance(LineType L, out double Lam1, out double Lam2, out xyz Nearest1, out xyz Nearest2) { xyz PQ = L.P.sub(P); double PQV = PQ.Scalarproduct(Direction); double PQW = PQ.Scalarproduct(L.Direction); double vv = Direction.Scalarproduct(Direction); double ww = L.Direction.Scalarproduct(L.Direction); double vw = L.Direction.Scalarproduct(Direction); double Det = vv * ww - vw * vw; if (Det == 0)// parallele { xyz n = PQ.cross(Direction).cross(Direction).normalized(); double result = PQ.Scalarproduct(n); Nearest2 = P.add(n.mul(result)); Nearest1 = P; Lam1 = 0; Lam2 = L.Direction.normalized().Scalarproduct(Nearest2.sub(L.P)); return(System.Math.Abs(result)); } else { Lam1 = (PQV * ww - PQW * vw) / Det; Lam2 = -(PQW * vv - PQV * vw) / Det; Nearest1 = P.add(Direction.mul(Lam1)); Nearest2 = L.P.add(L.Direction.mul(Lam2)); return(Nearest1.dist(Nearest2)); } }