All files / core/domain/utils/geometry axisCollision.js

100% Statements 13/13
100% Branches 2/2
100% Functions 1/1
100% Lines 12/12

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                      24x 24x   24x 96x 96x 96x 96x     24x 24x 24x 24x 24x    
import projection from "./projection";
 
/**
 * Check whether two rectangles collide on one particular axis.
 *
 * @param  {object}   An axis' coordinates (x, y).
 * @param  {array}    Rectangle A's corners.
 * @param  {array}    Rectangle B's corners.
 * @return {boolean}  True if the rectangles collide on the axis.
 */
export default function axisCollision(a, c1, c2) {
  const sc1 = [];
  const sc2 = [];
 
  for (let ci = 0; ci < 4; ci++) {
    const p1 = projection(c1[ci], a);
    const p2 = projection(c2[ci], a);
    sc1.push(p1.x * a.x + p1.y * a.y);
    sc2.push(p2.x * a.x + p2.y * a.y);
  }
 
  const maxc1 = Math.max(...sc1);
  const maxc2 = Math.max(...sc2);
  const minc1 = Math.min(...sc1);
  const minc2 = Math.min(...sc2);
  return minc2 <= maxc1 && maxc2 >= minc1;
}