src/rect.js
function getIntersectionRect (rect1, rect2) {
  const intersectRect = {
    topLeft: {},
    bottomRight: {}
  };

  if (!doesIntersect(rect1, rect2)) {
    return;
  }

  const rect1Points = rectToPoints(rect1);
  const rect2Points = rectToPoints(rect2);

  if (rect1.width >= 0) {
    if (rect2.width >= 0) {
      intersectRect.topLeft.x = Math.max(rect1Points.topLeft.x, rect2Points.topLeft.x);
      intersectRect.bottomRight.x = Math.min(rect1Points.bottomRight.x, rect2Points.bottomRight.x);
    } else {
      intersectRect.topLeft.x = Math.max(rect1Points.topLeft.x, rect2Points.bottomRight.x);
      intersectRect.bottomRight.x = Math.min(rect1Points.bottomRight.x, rect2Points.topLeft.x);
    }
  } else if (rect2.width >= 0) {
    intersectRect.topLeft.x = Math.min(rect1Points.topLeft.x, rect2Points.bottomRight.x);
    intersectRect.bottomRight.x = Math.max(rect1Points.bottomRight.x, rect2Points.topLeft.x);
  } else {
    intersectRect.topLeft.x = Math.min(rect1Points.topLeft.x, rect2Points.topLeft.x);
    intersectRect.bottomRight.x = Math.max(rect1Points.bottomRight.x, rect2Points.bottomRight.x);
  }

  if (rect1.height >= 0) {
    if (rect2.height >= 0) {
      intersectRect.topLeft.y = Math.max(rect1Points.topLeft.y, rect2Points.topLeft.y);
      intersectRect.bottomRight.y = Math.min(rect1Points.bottomRight.y, rect2Points.bottomRight.y);
    } else {
      intersectRect.topLeft.y = Math.max(rect1Points.topLeft.y, rect2Points.bottomRight.y);
      intersectRect.bottomRight.y = Math.min(rect1Points.bottomRight.y, rect2Points.topLeft.y);
    }
  } else if (rect2.height >= 0) {
    intersectRect.topLeft.y = Math.min(rect1Points.topLeft.y, rect2Points.bottomRight.y);
    intersectRect.bottomRight.y = Math.max(rect1Points.bottomRight.y, rect2Points.topLeft.y);
  } else {
    intersectRect.topLeft.y = Math.min(rect1Points.topLeft.y, rect2Points.topLeft.y);
    intersectRect.bottomRight.y = Math.max(rect1Points.bottomRight.y, rect2Points.bottomRight.y);
  }

  // Returns top-left and bottom-right points of intersected rectangle
  return intersectRect;

}
function getIntersectionRect (rect1, rect2) {
  const intersectRect = {
    topLeft: {},
    bottomRight: {}
  };

  if (!doesIntersect(rect1, rect2)) {
    return;
  }

  const rect1Points = rectToPoints(rect1);
  const rect2Points = rectToPoints(rect2);

  if (rect1.width >= 0) {
    if (rect2.width >= 0) {
      intersectRect.topLeft.x = Math.max(rect1Points.topLeft.x, rect2Points.topLeft.x);
      intersectRect.bottomRight.x = Math.min(rect1Points.bottomRight.x, rect2Points.bottomRight.x);
    } else {
      intersectRect.topLeft.x = Math.max(rect1Points.topLeft.x, rect2Points.bottomRight.x);
      intersectRect.bottomRight.x = Math.min(rect1Points.bottomRight.x, rect2Points.topLeft.x);
    }
  } else if (rect2.width >= 0) {
    intersectRect.topLeft.x = Math.min(rect1Points.topLeft.x, rect2Points.bottomRight.x);
    intersectRect.bottomRight.x = Math.max(rect1Points.bottomRight.x, rect2Points.topLeft.x);
  } else {
    intersectRect.topLeft.x = Math.min(rect1Points.topLeft.x, rect2Points.topLeft.x);
    intersectRect.bottomRight.x = Math.max(rect1Points.bottomRight.x, rect2Points.bottomRight.x);
  }

  if (rect1.height >= 0) {
    if (rect2.height >= 0) {
      intersectRect.topLeft.y = Math.max(rect1Points.topLeft.y, rect2Points.topLeft.y);
      intersectRect.bottomRight.y = Math.min(rect1Points.bottomRight.y, rect2Points.bottomRight.y);
    } else {
      intersectRect.topLeft.y = Math.max(rect1Points.topLeft.y, rect2Points.bottomRight.y);
      intersectRect.bottomRight.y = Math.min(rect1Points.bottomRight.y, rect2Points.topLeft.y);
    }
  } else if (rect2.height >= 0) {
    intersectRect.topLeft.y = Math.min(rect1Points.topLeft.y, rect2Points.bottomRight.y);
    intersectRect.bottomRight.y = Math.max(rect1Points.bottomRight.y, rect2Points.topLeft.y);
  } else {
    intersectRect.topLeft.y = Math.min(rect1Points.topLeft.y, rect2Points.topLeft.y);
    intersectRect.bottomRight.y = Math.max(rect1Points.bottomRight.y, rect2Points.bottomRight.y);
  }

  // Returns top-left and bottom-right points of intersected rectangle
  return intersectRect;

}
function getIntersectionRect ( rect1 , rect2 ) { const intersectRect = { topLeft : { } , bottomRight : { } } ; if ( ! doesIntersect ( rect1 , rect2 ) ) { return ; } const rect1Points = rectToPoints ( rect1 ) ; const rect2Points = rectToPoints ( rect2 ) ; if ( rect1 . width >= 0 ) { if ( rect2 . width >= 0 ) { intersectRect . topLeft . x = Math . max ( rect1Points . topLeft . x , rect2Points . topLeft . x ) ; intersectRect . bottomRight . x = Math . min ( rect1Points . bottomRight . x , rect2Points . bottomRight . x ) ; } else { intersectRect . topLeft . x = Math . max ( rect1Points . topLeft . x , rect2Points . bottomRight . x ) ; intersectRect . bottomRight . x = Math . min ( rect1Points . bottomRight . x , rect2Points . topLeft . x ) ; } } else if ( rect2 . width >= 0 ) { intersectRect . topLeft . x = Math . min ( rect1Points . topLeft . x , rect2Points . bottomRight . x ) ; intersectRect . bottomRight . x = Math . max ( rect1Points . bottomRight . x , rect2Points . topLeft . x ) ; } else { intersectRect . topLeft . x = Math . min ( rect1Points . topLeft . x , rect2Points . topLeft . x ) ; intersectRect . bottomRight . x = Math . max ( rect1Points . bottomRight . x , rect2Points . bottomRight . x ) ; } if ( rect1 . height >= 0 ) { if ( rect2 . height >= 0 ) { intersectRect . topLeft . y = Math . max ( rect1Points . topLeft . y , rect2Points . topLeft . y ) ; intersectRect . bottomRight . y = Math . min ( rect1Points . bottomRight . y , rect2Points . bottomRight . y ) ; } else { intersectRect . topLeft . y = Math . max ( rect1Points . topLeft . y , rect2Points . bottomRight . y ) ; intersectRect . bottomRight . y = Math . min ( rect1Points . bottomRight . y , rect2Points . topLeft . y ) ; } } else if ( rect2 . height >= 0 ) { intersectRect . topLeft . y = Math . min ( rect1Points . topLeft . y , rect2Points . bottomRight . y ) ; intersectRect . bottomRight . y = Math . max ( rect1Points . bottomRight . y , rect2Points . topLeft . y ) ; } else { intersectRect . topLeft . y = Math . min ( rect1Points . topLeft . y , rect2Points . topLeft . y ) ; intersectRect . bottomRight . y = Math . max ( rect1Points . bottomRight . y , rect2Points . bottomRight . y ) ; } // Returns top-left and bottom-right points of intersected rectangle return intersectRect ; }
Returns intersection points of two non-rotated rectangles
Returns intersection points of two non - rotated rectangles