Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

4.0 KB
Raw Blame
export interface WarpShape {
  cx: number
  rimY: number
  throatY: number
  r0: number
}

export interface WarpPoint {
  x: number
  y: number
}

interface ProjectParams {
  shape: WarpShape
  r: number
  angle: number
  flatten: number
  lineY: number
}

interface IsBehindWallParams {
  shape: WarpShape
  radii: number[]
  r: number
  angle: number
}

interface LocateCellParams {
  shape: WarpShape
  radii: number[]
  spokeCount: number
  x: number
  y: number
  flatten: number
  lineY: number
}

export interface WarpCell {
  ring: number
  spoke: number
}

interface FrontRadiusParams {
  shape: WarpShape
  y: number
}

interface RingRadiiParams {
  r0: number
  maxRadius: number
}

interface InvertWarpParams {
  shape: WarpShape
  x: number
  y: number
}

interface TangentAngleParams {
  shape: WarpShape
  r: number
  angle: number
}

interface WarpPolar {
  r: number
  angle: number
}

const TILT = 0.42
const THROAT_FLATTENING = 0.6

export const ringRadii = ({ r0, maxRadius }: RingRadiiParams): number[] => {
  const radii: number[] = []
  for (let i = 0; ; i++) {
    const r = r0 * (1 + 0.1 * i + 0.012 * i * i)
    radii.push(r)
    if (r > maxRadius) return radii
  }
}

const ringEllipse = ({ rimY, throatY, r0 }: WarpShape, r: number) => ({
  centerY: rimY + (throatY - rimY) * (r0 / r),
  ry: TILT * (r - THROAT_FLATTENING * r0),
})

export const frontRadius = ({ shape: { rimY, throatY, r0 }, y }: FrontRadiusParams): number => {
  const b = rimY - TILT * THROAT_FLATTENING * r0 - y
  return (-b + Math.sqrt(b * b - 4 * TILT * (throatY - rimY) * r0)) / (2 * TILT)
}

export const projectWarp = ({ shape, r, angle, flatten, lineY }: ProjectParams): WarpPoint => {
  const { centerY, ry } = ringEllipse(shape, r)
  const y = centerY + ry * Math.sin(angle)
  return { x: shape.cx + r * Math.cos(angle), y: lineY + (y - lineY) * (1 - flatten) }
}

export const isBehindWall = ({ shape, radii, r, angle }: IsBehindWallParams): boolean => {
  if (Math.sin(angle) <= 0) return false
  const { x, y } = projectWarp({ shape, r, angle, flatten: 0, lineY: 0 })
  const dx = x - shape.cx
  return radii.some((inner) => {
    if (inner >= r || Math.abs(dx) >= inner) return false
    const { centerY, ry } = ringEllipse(shape, inner)
    return centerY + ry * Math.sqrt(1 - (dx / inner) ** 2) > y + 0.5
  })
}

export const locateCell = ({
  shape,
  radii,
  spokeCount,
  x,
  y,
  flatten,
  lineY,
}: LocateCellParams): WarpCell | null => {
  if (flatten >= 1) return null
  const unflattenedY = lineY + (y - lineY) / (1 - flatten)
  const dx = x - shape.cx
  const outer = radii.findIndex((r) => {
    const { centerY, ry } = ringEllipse(shape, r)
    return (dx / r) ** 2 + ((unflattenedY - centerY) / ry) ** 2 <= 1
  })
  if (outer <= 0) return null
  const { centerY, ry } = ringEllipse(shape, radii[outer])
  const angle = Math.atan2((unflattenedY - centerY) / ry, dx / radii[outer])
  const turn = (angle + Math.PI * 2) % (Math.PI * 2)
  return { ring: outer - 1, spoke: Math.floor((turn / (Math.PI * 2)) * spokeCount) % spokeCount }
}

// bisects for the ring that passes through the point. points inside the throat stay on the throat ring
export const invertWarp = ({ shape, x, y }: InvertWarpParams): WarpPolar => {
  const dx = x - shape.cx
  const isOutside = (r: number) => {
    const { centerY, ry } = ringEllipse(shape, r)
    return (dx / r) ** 2 + ((y - centerY) / ry) ** 2 > 1
  }
  let inner = shape.r0
  let outer = isOutside(inner) ? shape.r0 * 64 : inner
  while (outer - inner > 0.01) {
    const mid = (inner + outer) / 2
    if (isOutside(mid)) inner = mid
    else outer = mid
  }
  const { centerY, ry } = ringEllipse(shape, outer)
  return { r: outer, angle: Math.atan2((y - centerY) / ry, dx / outer) }
}

export const tangentAngle = ({ shape, r, angle }: TangentAngleParams): number => {
  const { ry } = ringEllipse(shape, r)
  return Math.atan2(ry * Math.cos(angle), -r * Math.sin(angle))
}

export const easeInOutCubic = (t: number) => (t < 0.5 ? 4 * t * t * t : 1 - (-2 * t + 2) ** 3 / 2)

export const easeOutCubic = (t: number) => 1 - (1 - t) ** 3