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

25% Statements 2/8
100% Branches 0/0
0% Functions 0/1
25% Lines 2/8

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 274x               4x                                    
import getSelfLoopControlPoints from "./getSelfLoopControlPoints";
import { Node } from "./interfaces";
/**
 * Transforms a graph self loop into an axis-aligned square.
 *
 * @param  {object} n A graph node with a point (x, y) and a size.
 * @return {object}   A square: two points (x1, y1), (x2, y2) and height.
 */
export default function selfLoopToSquare(n: Node) {
  // Fitting to the curve is too costly, we compute a larger bounding box
  // using the control points:
  const cp = getSelfLoopControlPoints(n.x, n.y, n.size);
 
  // Bounding box of the point and the two control points:
  const minX = Math.min(n.x, cp.x1, cp.x2);
  const maxX = Math.max(n.x, cp.x1, cp.x2);
  const minY = Math.min(n.y, cp.y1, cp.y2);
  const maxY = Math.max(n.y, cp.y1, cp.y2);
  return {
    x1: minX - n.size,
    y1: minY - n.size,
    x2: maxX + n.size,
    y2: minY - n.size,
    height: maxY - minY + n.size * 2
  };
}