all files / src/ rect.js

1.64% Statements 1/61
0% Branches 0/46
0% Functions 0/6
1.64% Lines 1/61
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177                                                                                                                                                                                                                                                                                                                                                               
import lineSegment from './lineSegment.js';
 
function rectToLineSegments (rect) {
  const top = {
    start: {
      x: rect.left,
      y: rect.top
    },
    end: {
      x: rect.left + rect.width,
      y: rect.top
 
    }
  };
  const right = {
    start: {
      x: rect.left + rect.width,
      y: rect.top
    },
    end: {
      x: rect.left + rect.width,
      y: rect.top + rect.height
 
    }
  };
  const bottom = {
    start: {
      x: rect.left + rect.width,
      y: rect.top + rect.height
    },
    end: {
      x: rect.left,
      y: rect.top + rect.height
 
    }
  };
  const left = {
    start: {
      x: rect.left,
      y: rect.top + rect.height
    },
    end: {
      x: rect.left,
      y: rect.top
 
    }
  };
  const lineSegments = [top, right, bottom, left];
 
 
  return lineSegments;
}
 
function distanceToPoint (rect, point) {
  let minDistance = 655535;
  const lineSegments = rectToLineSegments(rect);
 
  lineSegments.forEach(function (segment) {
    const distance = lineSegment.distanceToPoint(segment, point);
 
    if(distance < minDistance) {
      minDistance = distance;
    }
  });
 
  return minDistance;
}
 
// Returns top-left and bottom-right points of the rectangle
function rectToPoints (rect) {
  const rectPoints = {
    topLeft: {
      x: rect.left,
      y: rect.top
    },
    bottomRight: {
      x: rect.left + rect.width,
      y: rect.top + rect.height
    }
  };
 
  return rectPoints;
}
 
// Returns whether two non-rotated rectangles are intersected
function doesIntersect (rect1, rect2) {
  let intersectLeftRight;
  let intersectTopBottom;
 
  const rect1Points = rectToPoints(rect1);
  const rect2Points = rectToPoints(rect2);
 
  if (rect1.width >= 0) {
    if (rect2.width >= 0) {
      intersectLeftRight = !((rect1Points.bottomRight.x <= rect2Points.topLeft.x) || (rect2Points.bottomRight.x <= rect1Points.topLeft.x));
    } else {
      intersectLeftRight = !((rect1Points.bottomRight.x <= rect2Points.bottomRight.x) || (rect2Points.topLeft.x <= rect1Points.topLeft.x));
    }
  } else if (rect2.width >= 0) {
    intersectLeftRight = !((rect1Points.topLeft.x <= rect2Points.topLeft.x) || (rect2Points.bottomRight.x <= rect1Points.bottomRight.x));
  } else {
    intersectLeftRight = !((rect1Points.topLeft.x <= rect2Points.bottomRight.x) || (rect2Points.topLeft.x <= rect1Points.bottomRight.x));
  }
 
  if (rect1.height >= 0) {
    if (rect2.height >= 0) {
      intersectTopBottom = !((rect1Points.bottomRight.y <= rect2Points.topLeft.y) || (rect2Points.bottomRight.y <= rect1Points.topLeft.y));
    } else {
      intersectTopBottom = !((rect1Points.bottomRight.y <= rect2Points.bottomRight.y) || (rect2Points.topLeft.y <= rect1Points.topLeft.y));
    }
  } else if (rect2.height >= 0) {
    intersectTopBottom = !((rect1Points.topLeft.y <= rect2Points.topLeft.y) || (rect2Points.bottomRight.y <= rect1Points.bottomRight.y));
  } else {
    intersectTopBottom = !((rect1Points.topLeft.y <= rect2Points.bottomRight.y) || (rect2Points.top <= rect1Points.bottomRight.y));
  }
 
  return intersectLeftRight && intersectTopBottom;
}
 
// Returns intersection points of two non-rotated rectangles
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;
 
}
 
const rect = {
  distanceToPoint,
  getIntersectionRect
};
 
export default rect;