public static OrderedTriple findThirdVector( OrderedTriple v1, OrderedTriple v2, double a1, double a2, OrderedTriple testv) throws Exception { // v1 and v2 must be equal lengths N. returns the vector a1 rad from v1, a2 // rad from v2 and N length. if there are two such vectors // it returns the one with positive component of testv // System.out.println( "angle between v1 & v2: " + v1.degBetween( v2 ) ); double epsilon = 0.0001; if (isApprox(a1, 0, epsilon)) return new OrderedTriple(v1); if (isApprox(a1, Math.PI, epsilon)) return v1.negative(); if (isApprox(a2, 0, epsilon)) return new OrderedTriple(v2); if (isApprox(a2, Math.PI, epsilon)) return v2.negative(); OrderedTriple v3 = null; double a = v1.x; double b = v1.y; double c = v1.z; double d = v2.x; double e = v2.y; double f = v2.z; double g, h, i; double roots[] = null; double N = v1.length(); double A = N * N * Math.cos(a1); double B = N * N * Math.cos(a2); double x12 = a * e - b * d; double x23 = b * f - c * e; double x31 = c * d - a * f; double P, Q, R, S; P = Q = R = S = 0; if (!isApprox(x12, 0, epsilon)) { // System.out.println( "route 1" ); P = x23 / x12; Q = (e * A - b * B) / x12; R = x31 / x12; S = (a * B - d * A) / x12; roots = solveQuadratic(1 + P * P + R * R, 2 * P * Q + 2 * R * S, Q * Q + S * S - N * N); i = roots[0]; v3 = new OrderedTriple(P * i + Q, R * i + S, i); if (testv != null && v3.dot(testv) < 0) { i = roots[1]; v3.become(P * i + Q, R * i + S, i); } } else if (!isApprox(x23, 0, epsilon)) { // System.out.println( "route 2" ); P = x31 / x23; Q = (f * A - c * B) / x23; R = x12 / x23; S = (b * B - e * A) / x23; roots = solveQuadratic(1 + P * P + R * R, 2 * P * Q + 2 * R * S, Q * Q + S * S - N * N); g = roots[0]; v3 = new OrderedTriple(g, P * g + Q, R * g + S); if (testv != null && v3.dot(testv) < 0) { g = roots[1]; v3.become(g, P * g + Q, R * g + S); } } else if (!isApprox(x31, 0, epsilon)) { // System.out.println( "route 3" ); P = x23 / x31; Q = (c * B - f * A) / x31; R = x12 / x31; S = (d * A - a * B) / x31; roots = solveQuadratic(1 + P * P + R * R, 2 * P * Q + 2 * R * S, Q * Q + S * S - N * N); h = roots[0]; v3 = new OrderedTriple(P * h + Q, h, R * h + S); if (testv != null && v3.dot(testv) < 0) { h = roots[1]; v3.become(P * h + Q, h, R * h + S); } } else { throw new Exception("EXCEPTION: no route found in findThirdVector"); } // if( ! isApprox( v3.radBetween( v1 ), a1, 0.1 ) ) System.out.println( // "error v1" ); // if( ! isApprox( v3.radBetween( v2 ), a2, 0.1 ) ) System.out.println( // "error v2" ); return v3; }