import type { Vec2 } from "./types";

export function seededRandom(seed: number) {
  let s = seed % 2147483647;
  if (s <= 0) s += 2147483646;
  return () => {
      s = (s * 16807) % 2147483647;
      return (s - 1) / 2147483646;
  };
}

/**
 * Bresenham's line algorithm to get all points between two coordinates.
 */
export function raycast(x0: number, y0: number, x1: number, y1: number): Vec2[] {
  const points: Vec2[] = [];
  let startX = x0;
  let startY = y0;
  
  const dx = Math.abs(x1 - x0);
  const dy = Math.abs(y1 - y0);
  const sx = (x0 < x1) ? 1 : -1;
  const sy = (y0 < y1) ? 1 : -1;
  let err = dx - dy;

  while(true) {
      points.push({ x: startX, y: startY });
      if (startX === x1 && startY === y1) break;
      const e2 = 2 * err;
      if (e2 > -dy) { err -= dy; startX += sx; }
      if (e2 < dx) { err += dx; startY += sy; }
  }
  return points;
}

export function manhattan(a: Vec2, b: Vec2): number {
  return Math.abs(a.x - b.x) + Math.abs(a.y - b.y);
}

export function lerp(start: number, end: number, t: number): number {
  return start * (1 - t) + end * t;
}
