@Test public void testNearestLinePoints() throws Exception { SimpleVector[] result = Util3D.nearestLinePoints( new SimpleVector[]{ new SimpleVector(-1, 0, 1), new SimpleVector(1, 0, 1)}, new SimpleVector[]{ new SimpleVector(1, 1, 0), new SimpleVector(1, -1, 0)} ); System.out.println(result[0].x + ", " + result[0].y + ", " + result[0].z); System.out.println(result[1].x + ", " + result[1].y + ", " + result[1].z); }
public static SimpleVector[] nearestLinePoints(SimpleVector[] line1, SimpleVector[] line2) { SimpleVector d21 = new SimpleVector(line1[0]); d21.sub(line1[1]); SimpleVector d34 = new SimpleVector(line2[1]); d34.sub(line2[0]); SimpleVector d13 = new SimpleVector(line2[0]); d13.sub(line1[0]); float a = d21.calcDot(d21); float b = d21.calcDot(d34); float c = d34.calcDot(d34); float d = -d13.calcDot(d21); float e = -d13.calcDot(d34); float[] u = new float[2]; u[0] = (d*c-e*b)/(c*a-b*b); u[1] = (e - b * u[0]) / c; return new SimpleVector[] { new SimpleVector( line1[0].x + (line1[1].x - line1[0].x) * u[0], line1[0].y + (line1[1].y - line1[0].y) * u[0], line1[0].z + (line1[1].z - line1[0].z) * u[0] ), new SimpleVector( line2[0].x + (line2[1].x - line2[0].x) * u[1], line2[0].y + (line2[1].y - line2[0].y) * u[1], line2[0].z + (line2[1].z - line2[0].z) * u[1] ) }; }
Util3D { public static SimpleVector[] nearestLinePoints(SimpleVector[] line1, SimpleVector[] line2) { SimpleVector d21 = new SimpleVector(line1[0]); d21.sub(line1[1]); SimpleVector d34 = new SimpleVector(line2[1]); d34.sub(line2[0]); SimpleVector d13 = new SimpleVector(line2[0]); d13.sub(line1[0]); float a = d21.calcDot(d21); float b = d21.calcDot(d34); float c = d34.calcDot(d34); float d = -d13.calcDot(d21); float e = -d13.calcDot(d34); float[] u = new float[2]; u[0] = (d*c-e*b)/(c*a-b*b); u[1] = (e - b * u[0]) / c; return new SimpleVector[] { new SimpleVector( line1[0].x + (line1[1].x - line1[0].x) * u[0], line1[0].y + (line1[1].y - line1[0].y) * u[0], line1[0].z + (line1[1].z - line1[0].z) * u[0] ), new SimpleVector( line2[0].x + (line2[1].x - line2[0].x) * u[1], line2[0].y + (line2[1].y - line2[0].y) * u[1], line2[0].z + (line2[1].z - line2[0].z) * u[1] ) }; } }
Util3D { public static SimpleVector[] nearestLinePoints(SimpleVector[] line1, SimpleVector[] line2) { SimpleVector d21 = new SimpleVector(line1[0]); d21.sub(line1[1]); SimpleVector d34 = new SimpleVector(line2[1]); d34.sub(line2[0]); SimpleVector d13 = new SimpleVector(line2[0]); d13.sub(line1[0]); float a = d21.calcDot(d21); float b = d21.calcDot(d34); float c = d34.calcDot(d34); float d = -d13.calcDot(d21); float e = -d13.calcDot(d34); float[] u = new float[2]; u[0] = (d*c-e*b)/(c*a-b*b); u[1] = (e - b * u[0]) / c; return new SimpleVector[] { new SimpleVector( line1[0].x + (line1[1].x - line1[0].x) * u[0], line1[0].y + (line1[1].y - line1[0].y) * u[0], line1[0].z + (line1[1].z - line1[0].z) * u[0] ), new SimpleVector( line2[0].x + (line2[1].x - line2[0].x) * u[1], line2[0].y + (line2[1].y - line2[0].y) * u[1], line2[0].z + (line2[1].z - line2[0].z) * u[1] ) }; }  }
Util3D { public static SimpleVector[] nearestLinePoints(SimpleVector[] line1, SimpleVector[] line2) { SimpleVector d21 = new SimpleVector(line1[0]); d21.sub(line1[1]); SimpleVector d34 = new SimpleVector(line2[1]); d34.sub(line2[0]); SimpleVector d13 = new SimpleVector(line2[0]); d13.sub(line1[0]); float a = d21.calcDot(d21); float b = d21.calcDot(d34); float c = d34.calcDot(d34); float d = -d13.calcDot(d21); float e = -d13.calcDot(d34); float[] u = new float[2]; u[0] = (d*c-e*b)/(c*a-b*b); u[1] = (e - b * u[0]) / c; return new SimpleVector[] { new SimpleVector( line1[0].x + (line1[1].x - line1[0].x) * u[0], line1[0].y + (line1[1].y - line1[0].y) * u[0], line1[0].z + (line1[1].z - line1[0].z) * u[0] ), new SimpleVector( line2[0].x + (line2[1].x - line2[0].x) * u[1], line2[0].y + (line2[1].y - line2[0].y) * u[1], line2[0].z + (line2[1].z - line2[0].z) * u[1] ) }; }  static SimpleVector nearestPoint(SimpleVector[] line, SimpleVector point); static SimpleVector[] nearestLinePoints(SimpleVector[] line1, SimpleVector[] line2); static SimpleVector rayTriangleIntersects(
            SimpleVector v0, SimpleVector v1, SimpleVector v2,
            SimpleVector origin, SimpleVector dir,
            boolean isSingledSided
    ); }
Util3D { public static SimpleVector[] nearestLinePoints(SimpleVector[] line1, SimpleVector[] line2) { SimpleVector d21 = new SimpleVector(line1[0]); d21.sub(line1[1]); SimpleVector d34 = new SimpleVector(line2[1]); d34.sub(line2[0]); SimpleVector d13 = new SimpleVector(line2[0]); d13.sub(line1[0]); float a = d21.calcDot(d21); float b = d21.calcDot(d34); float c = d34.calcDot(d34); float d = -d13.calcDot(d21); float e = -d13.calcDot(d34); float[] u = new float[2]; u[0] = (d*c-e*b)/(c*a-b*b); u[1] = (e - b * u[0]) / c; return new SimpleVector[] { new SimpleVector( line1[0].x + (line1[1].x - line1[0].x) * u[0], line1[0].y + (line1[1].y - line1[0].y) * u[0], line1[0].z + (line1[1].z - line1[0].z) * u[0] ), new SimpleVector( line2[0].x + (line2[1].x - line2[0].x) * u[1], line2[0].y + (line2[1].y - line2[0].y) * u[1], line2[0].z + (line2[1].z - line2[0].z) * u[1] ) }; }  static SimpleVector nearestPoint(SimpleVector[] line, SimpleVector point); static SimpleVector[] nearestLinePoints(SimpleVector[] line1, SimpleVector[] line2); static SimpleVector rayTriangleIntersects(
            SimpleVector v0, SimpleVector v1, SimpleVector v2,
            SimpleVector origin, SimpleVector dir,
            boolean isSingledSided
    );  }