Skip to content

Instantly share code, notes, and snippets.

@joshwashywash
Last active September 16, 2023 05:27
Show Gist options
  • Save joshwashywash/d58004e6c953e812270c2ccb6b0ae939 to your computer and use it in GitHub Desktop.
Save joshwashywash/d58004e6c953e812270c2ccb6b0ae939 to your computer and use it in GitHub Desktop.
circular interpolation
type Point = {
  x: number;
  y: number;
};

const interpolate = (center: Point) => (amount: number) => (pt: Point) => {
  return (n: number) => {
    const angle = n * amount;
    const c = Math.cos(angle);
    const s = Math.sin(angle);
    const x = pt.x - center.x;
    const y = pt.y - center.y;
    pt.x = x * c - y * s + center.x;
    pt.y = x * s + y * c + center.y;
  };
};

const point: Point = {
  x: 1,
  y: 0,
};

const center: Point = {
  x: 0,
  y: 0,
};

const i = interpolate(center)(0.5 * Math.PI)(point);
i(1);
console.log(point); // {x: 0, y: 1}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment