summaryrefslogtreecommitdiff
path: root/client/lib/utils/rotateVector.ts
blob: 82bb54dc6025ec01974e0ef0ff3e90bee7fa0160 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import type { Coord2D } from "../interfaces";

/**
 * ([[cos(θ), -sin(θ),])  ([x,)
 * ([sin(θ),  cos(θ)] ])  ( y])
 */
export const rotateVector = (vector: Coord2D, theta: number): Coord2D => {
  const rads = (theta * Math.PI) / 180;
  const [cos, sin] = [Math.cos(rads), Math.sin(rads)];

  return {
    x: vector.x * cos - vector.y * sin,
    y: vector.x * sin + vector.y * cos,
  };
};