All files / src/components/VirtualWalkthrough xr-scene-bounds.ts

100% Statements 14/14
100% Branches 4/4
100% Functions 2/2
100% Lines 14/14

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 32 33 34 35 36 37 38 39 40 41                    5x 13x 2x     11x 11x 11x 11x 5x     6x   6x       5x             4x 4x   4x    
/** A world-space XZ point clamped into the bounds circle, and how far it had to move to get there. */
export type ClampResult = { x: number; z: number; distance: number };
 
/**
 * Clamp a world-space XZ point into the circle of `radius` about (centerX, centerZ).
 *
 * `distance` is how far the point was moved and is 0 whenever the point was already inside, which
 * callers use as the "was this clamped?" signal. A non-positive radius means "no bounds" and leaves
 * the point untouched; so does a point at the exact center, which has no direction to project along.
 */
export const clampToCircle = (x: number, z: number, centerX: number, centerZ: number, radius: number): ClampResult => {
  if (radius <= 0) {
    return { x, z, distance: 0 };
  }
 
  const dx = x - centerX;
  const dz = z - centerZ;
  const dist = Math.sqrt(dx * dx + dz * dz);
  if (dist <= radius) {
    return { x, z, distance: 0 };
  }
 
  const scale = radius / dist;
 
  return { x: centerX + dx * scale, z: centerZ + dz * scale, distance: dist - radius };
};
 
/** Horizontal distance from a world-space XZ point to the circle's edge: negative inside, positive outside. */
export const distanceToCircleEdge = (
  x: number,
  z: number,
  centerX: number,
  centerZ: number,
  radius: number
): number => {
  const dx = x - centerX;
  const dz = z - centerZ;
 
  return Math.sqrt(dx * dx + dz * dz) - radius;
};