public static OrderedTriple round(OrderedTriple ot, int mantissaLength) { ot = new OrderedTriple(ot); ot.x = Misc.round(ot.x, mantissaLength); ot.y = Misc.round(ot.y, mantissaLength); ot.z = Misc.round(ot.z, mantissaLength); return ot; }
public OrderedTriple arbitraryPerpendicular() { OrderedTriple perp = new OrderedTriple(1, 1, 1); if (x != 0) perp.x = (y + z) / -x; else if (y != 0) perp.y = (x + z) / -y; else if (z != 0) perp.z = (x + y) / -z; else return null; return perp; }
public static OrderedTriple[] sectSphereLine( OrderedTriple c, double r, OrderedTriple a, OrderedTriple b) { // Find the intersection(s) of the sphere with radius length r and center at // point c and the line containing points a and b OrderedTriple v = b.minus(a); OrderedTriple d = a.minus(c); try { double[] k = solveQuadratic(v.lengthSquared(), 2 * d.dot(v), d.lengthSquared() - r * r); OrderedTriple[] answer = {v.times(k[0]).plus(a), v.times(k[1]).plus(a)}; return answer; } catch (Exception e) { return null; } }
public static OrderedTriple sectPlaneLine( OrderedTriple P1, OrderedTriple P2, OrderedTriple P3, OrderedTriple L1, OrderedTriple L2) { // Find the 3D coordinates of the intersection of the plane containing // points P1, P2, P3 and the line containing L1, L2 OrderedTriple n = P2.minus(P1).cross(P3.minus(P2)); OrderedTriple l = L2.minus(L1); double A = n.dot(l); if (A == 0) return null; double B = n.dot(P1); double C = n.dot(L1); double K = (B - C) / A; return l.times(K).plus(L1); }
public static OrderedTriple sectLines( OrderedTriple p1, OrderedTriple p2, OrderedTriple p3, OrderedTriple p4) { // line from p1 through p2 and line from p3 through p4 OrderedTriple A = p2.minus(p1); OrderedTriple B = p4.minus(p3); OrderedTriple C = p3.minus(p1); OrderedDouble KT1 = OrderedDouble.solveEquations(A.x, -B.x, C.x, A.y, -B.y, C.y); OrderedDouble KT2 = OrderedDouble.solveEquations(A.y, -B.y, C.y, A.z, -B.z, C.z); OrderedDouble KT3 = OrderedDouble.solveEquations(A.z, -B.z, C.z, A.x, -B.x, C.x); double K = 0; if (KT1 == null && KT2 == null && KT3 == null) { return LineLineIntersect(p1, p2, p3, p4); // return null; } else if (KT1 != null) { K = KT1.x; } else if (KT2 != null) { K = KT2.x; } else if (KT3 != null) { K = KT3.x; } return A.times(K).plus(p1); }
static double[] planeFrom3Points(OrderedTriple p1, OrderedTriple p2, OrderedTriple p3) { return planeFromNormalAndPoint(p1.minus(p2).cross(p2.minus(p3)), p1); }
static double[] planeFromNormalAndPoint(OrderedTriple n, OrderedTriple p) { double[] answer = {n.x, n.y, n.z, n.dot(p)}; return answer; }
/* * static OrderedTriple sectPlaneLine( OrderedTriple P1, OrderedTriple P2, * OrderedTriple P3, OrderedTriple L1, OrderedTriple L2 ){ OrderedTriple n = * P2.minus( P1 ).cross( P3.minus( P2 ) ); OrderedTriple l = L2.minus( L1 ); * double A = n.dot( l ); if( A == 0 ) return null; double B = n.dot( P1 ); * double C = n.dot( L1 ); double K = ( B - C )/A; return l.times( K ).plus( * L1 ); } */ public static double pointPlaneDistance( OrderedTriple p, OrderedTriple P1, OrderedTriple P2, OrderedTriple P3) { OrderedTriple n = P2.minus(P1).cross(P3.minus(P2)); return sectPlaneLine(P1, P2, P3, p, p.plus(n)).distance(p); }
public double radBetween(OrderedTriple t) { return Math.acos((dot(t)) / (length() * t.length())); }
void towardsEquals(OrderedTriple p, double distance) { OrderedTriple v = p.minus(this); plus(v.times(distance / v.length())); }
OrderedTriple towards(OrderedTriple p, double distance) { OrderedTriple v = p.minus(this); return plus(v.times(distance / v.length())); }
public OrderedTriple mid(OrderedTriple p, double percent) { percent /= 100; return plus(p.minus(this).times(percent)); }
private static OrderedTriple LineLineIntersect( OrderedTriple p1, OrderedTriple p2, OrderedTriple p3, OrderedTriple p4) { double EPS = 1e-12; OrderedTriple p13 = new OrderedTriple(), p43 = new OrderedTriple(), p21 = new OrderedTriple(), pa = new OrderedTriple(), pb = new OrderedTriple(); double d1343, d4321, d1321, d4343, d2121; double numer, denom, mua, mub; p13.x = p1.x - p3.x; p13.y = p1.y - p3.y; p13.z = p1.z - p3.z; p43.x = p4.x - p3.x; p43.y = p4.y - p3.y; p43.z = p4.z - p3.z; if (Math.abs(p43.x) < EPS && Math.abs(p43.y) < EPS && Math.abs(p43.z) < EPS) return (null); p21.x = p2.x - p1.x; p21.y = p2.y - p1.y; p21.z = p2.z - p1.z; if (Math.abs(p21.x) < EPS && Math.abs(p21.y) < EPS && Math.abs(p21.z) < EPS) return (null); d1343 = p13.x * p43.x + p13.y * p43.y + p13.z * p43.z; d4321 = p43.x * p21.x + p43.y * p21.y + p43.z * p21.z; d1321 = p13.x * p21.x + p13.y * p21.y + p13.z * p21.z; d4343 = p43.x * p43.x + p43.y * p43.y + p43.z * p43.z; d2121 = p21.x * p21.x + p21.y * p21.y + p21.z * p21.z; denom = d2121 * d4343 - d4321 * d4321; if (Math.abs(denom) < EPS) return (null); numer = d1343 * d4321 - d1321 * d4343; mua = numer / denom; mub = (d1343 + d4321 * (mua)) / d4343; pa.x = p1.x + mua * p21.x; pa.y = p1.y + mua * p21.y; pa.z = p1.z + mua * p21.z; pb.x = p3.x + mub * p43.x; pb.y = p3.y + mub * p43.y; pb.z = p3.z + mub * p43.z; return pa.mid(pb); }
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; }
public double cos(OrderedTriple v) { // returns the cosine of the angle between them return dot(v) / (length() * v.length()); }
static OrderedTriple[] sectPlanes( OrderedTriple p11, OrderedTriple p12, OrderedTriple p13, OrderedTriple p21, OrderedTriple p22, OrderedTriple p23) { OrderedTriple n1 = p11.minus(p12).cross(p12.minus(p13)); OrderedTriple n2 = p21.minus(p22).cross(p22.minus(p23)); OrderedTriple n3 = n1.cross(n2); double[] m1 = {n1.x, n1.y, n1.z}; double[] m2 = {n2.x, n2.y, n2.z}; double[] m3 = {n3.x, n3.y, n3.z}; double[][] m = {m1, m2, m3}; Matrix M = new Matrix(m); double[] b1 = {n1.dot(p11)}; double[] b2 = {n2.dot(p21)}; double[] b3 = {0}; double[][] b = {b1, b2, b3}; Matrix B = new Matrix(b); Matrix S = M.inverse().times(B); // OrderedTriple s1 = new OrderedTriple( S.mat[0][0], S.mat[1][0], // S.mat[2][0] ); OrderedTriple s1 = new OrderedTriple(S.mat[0][0], S.mat[1][0], S.mat[2][0]); OrderedTriple s2 = s1.plus(n3); OrderedTriple[] answer = {s1, s2}; return answer; }
OrderedTriple bisectAngle(OrderedTriple v) { // v = v.times( length()/v.length() ); v = v.unit().times(length()); return mid(v); }
public OrderedTriple(OrderedTriple t1, OrderedTriple t2) { this(t2.minus(t1)); }
public double distance(OrderedTriple p1, OrderedTriple p2) { // distance to line through p1 and p2 OrderedTriple v1 = p2.minus(p1); OrderedTriple v2 = minus(p1); return v2.length() * v2.sin(v1); }