All files / src/packlets/geometry index.ts

100% Statements 8/8
100% Branches 4/4
100% Functions 4/4
100% Lines 8/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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50                                              2x   56x       51x       49x                 2x   32x 32x 32x      
/**
 * @packageDocumentation
 *
 * Minimal 2D geometry primitives for the editor.
 */
 
export interface Point {
  x: number;
  y: number;
}
 
export interface Dimension {
  width: number;
  height: number;
}
 
export interface Rect {
  x: number;
  y: number;
  width: number;
  height: number;
}
 
export const Rect = {
  center(r: Rect): Point {
    return { x: r.x + r.width / 2, y: r.y + r.height / 2 };
  },
 
  contains(r: Rect, p: Point): boolean {
    return p.x >= r.x && p.x < r.x + r.width && p.y >= r.y && p.y < r.y + r.height;
  },
 
  expand(r: Rect, amount: number): Rect {
    return {
      x: r.x - amount,
      y: r.y - amount,
      width: r.width + amount * 2,
      height: r.height + amount * 2,
    };
  },
};
 
export const Point = {
  distance(a: Point, b: Point): number {
    const dx = a.x - b.x;
    const dy = a.y - b.y;
    return Math.sqrt(dx * dx + dy * dy);
  },
};