All files / core/domain/utils/geometry quadraticCurveToSquare.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 27 28 29 30 314x                       4x                                    
import getPointOnQuadraticCurve from "./getPointOnQuadraticCurve";
import { Edge, Point, Rectangle } from "./interfaces";
 
/**
 * Transforms a graph edge of type 'curve' with x1, y1, x2, y2,
 * control point and size into an axis-aligned square.
 *
 * @param  {object} e  A graph edge with at least two points
 *                     (x1, y1), (x2, y2) and a size.
 * @param  {object} cp A control point (x,y).
 * @return {object}    A square: two points (x1, y1), (x2, y2) and height.
 */
export default function quadraticCurveToSquare(e: Edge, cp: Point): Rectangle {
  const pt = getPointOnQuadraticCurve(0.5, e.x1, e.y1, e.x2, e.y2, cp.x, cp.y);
 
  // Bounding box of the two points and the point at the middle of the
  // curve:
  const minX = Math.min(e.x1, e.x2, pt.x);
  const maxX = Math.max(e.x1, e.x2, pt.x);
  const minY = Math.min(e.y1, e.y2, pt.y);
  const maxY = Math.max(e.y1, e.y2, pt.y);
 
  return {
    x1: minX - e.size,
    y1: minY - e.size,
    x2: maxX + e.size,
    y2: minY - e.size,
    height: maxY - minY + e.size * 2
  };
}