function testLineLine(line1, line2) {
  const [p1, p2] = lineToPoints(line1);
  const [p3, p4] = lineToPoints(line2);
  const dx1 = distanceX(p2, p1);
  const dy1 = distanceY(p2, p1);
  const dx2 = distanceX(p4, p3);
  const dy2 = distanceY(p4, p3);
  const dx3 = distanceX(p1, p3);
  const dy3 = distanceY(p1, p3);
  let ub = dy2 * dx1 - dx2 * dy1;
  const uat = dx2 * dy3 - dy2 * dx3;
  const ubt = dx1 * dy3 - dy1 * dx3;
  let t1;
  let t2;

  if (dx1 === 0 && dy1 === 0) {
    // Line segment has no length
    return false;
  }

  if (dx2 === 0 && dy2 === 0) {
    // Line segment has no length
    return false;
  }

  if (ub === 0) {
    if (uat === 0 && ubt === 0) {
      // COINCIDENT;
      if (dx1 === 0) {
        if (dy1 === 0) {
          // p1 = p2
          return p1.x === p2.x && p1.y === p2.y;
        }
        t1 = distanceY(p3, p1) / dy1;
        t2 = distanceY(p4, p1) / dy1;
      } else {
        t1 = (p3.x - p1.x) / dx1;
        t2 = (p4.x - p1.x) / dx1;
      }
      if ((t1 < 0 && t2 < 0) || (t1 > 1 && t2 > 1)) {
        return false;
      }
      return true;
    }
    return false; // PARALLEL;
  }
  const ua = uat / ub;
  ub = ubt / ub;
  if (ua >= 0.0 && ua <= 1.0 && ub >= 0.0 && ub <= 1.0) {
    return true;
  }
  return false;
}
/**
 * Test if a Line collider with another line. If so, returns true and false otherwise.
 * Both lines must have length greater then 0.
 * @private
 * @param {object} line
 * @param {number} line.x1 - x-coordinate
 * @param {number} line.y1 - y-coordinate
 * @param {number} line.x1 - x-coordinate
 * @param {number} line.y1 - y-coordinate
 * @param {object} line
 * @param {number} line.x1 - x-coordinate
 * @param {number} line.y1 - y-coordinate
 * @param {number} line.x1 - x-coordinate
 * @param {number} line.y1 - y-coordinate
 * @return {boolean} true if line collide with line
 */
Test if a Line collider with another line. If so, returns true and false otherwise.
Both lines must have length greater then 0.
Test if a Line collider with another line . If so returns true and false otherwise . Both lines must have length greater then 0 .
function testLineLine(line1, line2) {
  const [p1, p2] = lineToPoints(line1);
  const [p3, p4] = lineToPoints(line2);
  const dx1 = distanceX(p2, p1);
  const dy1 = distanceY(p2, p1);
  const dx2 = distanceX(p4, p3);
  const dy2 = distanceY(p4, p3);
  const dx3 = distanceX(p1, p3);
  const dy3 = distanceY(p1, p3);
  let ub = dy2 * dx1 - dx2 * dy1;
  const uat = dx2 * dy3 - dy2 * dx3;
  const ubt = dx1 * dy3 - dy1 * dx3;
  let t1;
  let t2;
  if (dx1 === 0 && dy1 === 0) {
    return false;
  }
  if (dx2 === 0 && dy2 === 0) {
    return false;
  }
  if (ub === 0) {
    if (uat === 0 && ubt === 0) {
      if (dx1 === 0) {
        if (dy1 === 0) {
          return p1.x === p2.x && p1.y === p2.y;
        }
        t1 = distanceY(p3, p1) / dy1;
        t2 = distanceY(p4, p1) / dy1;
      } else {
        t1 = (p3.x - p1.x) / dx1;
        t2 = (p4.x - p1.x) / dx1;
      }
      if ((t1 < 0 && t2 < 0) || (t1 > 1 && t2 > 1)) {
        return false;
      }
      return true;
    }
    return false; 
  }
  const ua = uat / ub;
  ub = ubt / ub;
  if (ua >= 0.0 && ua <= 1.0 && ub >= 0.0 && ub <= 1.0) {
    return true;
  }
  return false;
}
function testLineLine ( line1 , line2 ) { const [ p1 , p2 ] = lineToPoints ( line1 ) ; const [ p3 , p4 ] = lineToPoints ( line2 ) ; const dx1 = distanceX ( p2 , p1 ) ; const dy1 = distanceY ( p2 , p1 ) ; const dx2 = distanceX ( p4 , p3 ) ; const dy2 = distanceY ( p4 , p3 ) ; const dx3 = distanceX ( p1 , p3 ) ; const dy3 = distanceY ( p1 , p3 ) ; let ub = dy2 * dx1 - dx2 * dy1 ; const uat = dx2 * dy3 - dy2 * dx3 ; const ubt = dx1 * dy3 - dy1 * dx3 ; let t1 ; let t2 ; if ( dx1 === 0 && dy1 === 0 ) { return false ; } if ( dx2 === 0 && dy2 === 0 ) { return false ; } if ( ub === 0 ) { if ( uat === 0 && ubt === 0 ) { if ( dx1 === 0 ) { if ( dy1 === 0 ) { return p1 . x === p2 . x && p1 . y === p2 . y ; } t1 = distanceY ( p3 , p1 ) / dy1 ; t2 = distanceY ( p4 , p1 ) / dy1 ; } else { t1 = ( p3 . x - p1 . x ) / dx1 ; t2 = ( p4 . x - p1 . x ) / dx1 ; } if ( ( t1 < 0 && t2 < 0 ) || ( t1 > 1 && t2 > 1 ) ) { return false ; } return true ; } return false ; } const ua = uat / ub ; ub = ubt / ub ; if ( ua >= 0.0 && ua <= 1.0 && ub >= 0.0 && ub <= 1.0 ) { return true ; } return false ; }
Test if a Line collider with another line.
Test if a Line collider with another line .
// Line segment has no length // Line segment has no length // COINCIDENT; // p1 = p2 // PARALLEL;