All files / utils/geometry axisAlignedTopPoints.ts

100% Statements 8/8
100% Branches 12/12
100% Functions 1/1
100% Lines 7/7

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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45                      2x   8x     6x 2x                 4x 2x                 2x                
import { Rectangle } from "./interfaces";
 
/**
 * Compute top points of an axis-aligned rectangle. This is useful in
 * cases when the rectangle has been rotated (left, right or bottom up) and
 * later operations need to know the top points.
 *
 * @param  {object} An axis-aligned rectangle defined by two points
 *                  (x1, y1), (x2, y2) and height.
 * @return {object} A rectangle: two points (x1, y1), (x2, y2) and height.
 */
export default function axisAlignedTopPoints(r: Rectangle): Rectangle {
  // Basic
  if (r.y1 === r.y2 && r.x1 < r.x2) return r;
 
  // Rotated to right
  if (r.x1 === r.x2 && r.y2 > r.y1)
    return {
      x1: r.x1 - r.height,
      y1: r.y1,
      x2: r.x1,
      y2: r.y1,
      height: r.height
    };
 
  // Rotated to left
  if (r.x1 === r.x2 && r.y2 < r.y1)
    return {
      x1: r.x1,
      y1: r.y2,
      x2: r.x2 + r.height,
      y2: r.y2,
      height: r.height
    };
 
  // Bottom's up
  return {
    x1: r.x2,
    y1: r.y1 - r.height,
    x2: r.x1,
    y2: r.y1 - r.height,
    height: r.height
  };
}