lineLineMinimumPoints method

List<Vector3> lineLineMinimumPoints(
  1. Line line1,
  2. Line line2
)

Implementation

List<Vector3> lineLineMinimumPoints(Line line1,Line line2 ){
  Vector3 r = _v1..setFrom( line1.end )..sub( line1.start );
  Vector3 s = _v2..setFrom( line2.end )..sub( line2.start );
  Vector3 w = _v3..setFrom( line2.start )..sub( line1.start );

  num a = r.dot( s ),
    b = r.dot( r ),
    c = s.dot( s ),
    d = s.dot( w ),
    e = r.dot( w );

  double t1;
  double t2;
  num divisor = b * c - a * a;

  if (divisor.abs() < eps ) {
    double d1 = - d / c;
    double d2 = ( a - d ) / c;

    if ( ( d1 - 0.5 ).abs() < ( d2 - 0.5 ).abs() ) {
      t1 = 0;
      t2 = d1;
    }
    else {
      t1 = 1;
      t2 = d2;
    }
  }
  else {
    t1 = ( d * a + e * c ) / divisor;
    t2 = ( t1 * a - d ) / c;
  }

  t2 = max(0, min( 1, t2 ) );
  t1 = max(0, min( 1, t1 ) );

  Vector3 point1 = r..scale( t1 )..add( line1.start );
  Vector3 point2 = s..scale( t2 )..add( line2.start );

  return [point1, point2];
}