All files / utils math.ts

100% Statements 9/9
100% Branches 0/0
100% Functions 3/3
100% Lines 9/9

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 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101                                                                          3x 3x 3x 3x                                                     3x 3x 3x 3x                                                       2638x    
/**
 * Mathematical utility functions for Black Trigram
 * 
 * Provides shared mathematical operations used across combat, training,
 * and physics systems to ensure consistency and follow DRY principles.
 * 
 * **Korean**: 수학 유틸리티 함수
 * 
 * @module utils/math
 * @korean 수학유틸리티
 */
 
/**
 * Calculate 3D Euclidean distance between two positions
 * 
 * Uses the standard 3D distance formula: √(dx² + dy² + dz²)
 * This is used throughout the combat and training systems to calculate
 * distance between combatants, ensuring consistent distance calculations.
 * 
 * **Korean**: 3D 유클리드 거리 계산
 * 
 * @param pos1 - First position as [x, y, z] tuple
 * @param pos2 - Second position as [x, y, z] tuple
 * @returns The 3D Euclidean distance in meters
 * 
 * @example
 * const distance = calculateDistance3D([0, 0, 0], [3, 4, 0]);
 * // Returns: 5.0 (3-4-5 triangle)
 * 
 * @public
 * @category Math Utilities
 * @korean 3D거리계산
 */
export function calculateDistance3D(
  pos1: [number, number, number],
  pos2: [number, number, number]
): number {
  const dx = pos1[0] - pos2[0];
  const dy = pos1[1] - pos2[1];
  const dz = pos1[2] - pos2[2];
  return Math.sqrt(dx * dx + dy * dy + dz * dz);
}
 
/**
 * Calculate squared 3D distance between two positions
 * 
 * Optimized version that avoids the expensive square root operation.
 * Useful when comparing distances (A > B) where the square root is unnecessary.
 * 
 * **Korean**: 3D 거리 제곱 계산
 * 
 * @param pos1 - First position as [x, y, z] tuple
 * @param pos2 - Second position as [x, y, z] tuple
 * @returns The squared 3D distance in meters²
 * 
 * @example
 * const distSq = calculateDistance3DSquared([0, 0, 0], [3, 4, 0]);
 * // Returns: 25.0
 * 
 * @public
 * @category Math Utilities
 * @korean 3D거리제곱계산
 */
export function calculateDistance3DSquared(
  pos1: [number, number, number],
  pos2: [number, number, number]
): number {
  const dx = pos1[0] - pos2[0];
  const dy = pos1[1] - pos2[1];
  const dz = pos1[2] - pos2[2];
  return dx * dx + dy * dy + dz * dz;
}
 
/**
 * Convert degrees to radians
 * 
 * Used throughout animation systems for bone rotations where angles are
 * specified in degrees for readability but need to be converted to radians
 * for Three.js rendering.
 * 
 * **Korean**: 각도를 라디안으로 변환
 * 
 * @param degrees - Angle in degrees (0-360)
 * @returns Angle in radians (0-2π)
 * 
 * @example
 * const rightAngle = toRadians(90);
 * // Returns: approximately 1.5708 (π/2)
 * 
 * @example
 * const straightAngle = toRadians(180);
 * // Returns: approximately 3.1416 (π)
 * 
 * @public
 * @category Math Utilities
 * @korean 각도변환
 */
export function toRadians(degrees: number): number {
  return degrees * (Math.PI / 180);
}