All files / core/domain/utils/geometry lineToSquare.ts

20% Statements 1/5
100% Branches 0/0
0% Functions 0/1
20% Lines 1/5

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 46 47 48 49 50 51 52 53 54                    4x                                                                                      
import { Edge, Rectangle } from "./interfaces";
 
/**
 * Transforms a graph edge with x1, y1, x2, y2 and size into an
 * axis-aligned square.
 *
 * @param  {object} A graph edge with at least two points
 *                  (x1, y1), (x2, y2) and a size.
 * @return {object} A square: two points (x1, y1), (x2, y2) and height.
 */
export default function lineToSquare(e: Edge): Rectangle {
  if (e.y1 < e.y2) {
    // (e.x1, e.y1) on top
    if (e.x1 < e.x2) {
      // (e.x1, e.y1) on left
      return {
        x1: e.x1 - e.size,
        y1: e.y1 - e.size,
        x2: e.x2 + e.size,
        y2: e.y1 - e.size,
        height: e.y2 - e.y1 + e.size * 2
      };
    }
    // (e.x1, e.y1) on right
    return {
      x1: e.x2 - e.size,
      y1: e.y1 - e.size,
      x2: e.x1 + e.size,
      y2: e.y1 - e.size,
      height: e.y2 - e.y1 + e.size * 2
    };
  }
 
  // (e.x2, e.y2) on top
  if (e.x1 < e.x2) {
    // (e.x1, e.y1) on left
    return {
      x1: e.x1 - e.size,
      y1: e.y2 - e.size,
      x2: e.x2 + e.size,
      y2: e.y2 - e.size,
      height: e.y1 - e.y2 + e.size * 2
    };
  }
  // (e.x2, e.y2) on right
  return {
    x1: e.x2 - e.size,
    y1: e.y2 - e.size,
    x2: e.x1 + e.size,
    y2: e.y2 - e.size,
    height: e.y1 - e.y2 + e.size * 2
  };
}