summaryrefslogtreecommitdiff
path: root/utils/point2d.ts
blob: 45bee2e816a7e96ba6f2928d52a8b0f222a39c43 (plain)
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
export type Vec2 = {
  x: number;
  y: number;
};

export const l2Norm = (p1: Vec2, p2: Vec2) =>
  Math.sqrt(Math.pow(p2.x - p1.x, 2) + Math.pow(p2.y - p1.y, 2));

export const isDiagAdj = (p1: Vec2, p2: Vec2) => l2Norm(p1, p2) <= Math.sqrt(2);

export const neighbors = (p1: Vec2, max: Vec2) => {
  const ns: Vec2[] = [];
  const { x, y } = p1;
  for (let dy = -1; dy <= 1; dy++) {
    for (let dx = -1; dx <= 1; dx++) {
      const [newX, newY] = [dx + x, dy + y];
      if (
        !(dy === dx && dy === 0) &&
        newX >= 0 &&
        newX < max.x &&
        newY < max.y &&
        newY >= 0
      ) {
        ns.push({ x: newX, y: newY });
      }
    }
  }
  return ns;
};

export const;