import { Position } from '../models/Position';

/**
 * 生成指定范围内的随机整数
 */
export function randomInt(min: number, max: number): number {
    return Math.floor(Math.random() * (max - min + 1)) + min;
}

/**
 * 生成随机浮点数
 */
export function randomFloat(min: number, max: number): number {
    return Math.random() * (max - min) + min;
}

/**
 * 在圆形区域内生成随机位置
 * @param center 圆心
 * @param radius 半径
 */
export function randomPositionInCircle(center: Position, radius: number): Position {
    // 使用极坐标转换，确保均匀分布
    const angle = Math.random() * 2 * Math.PI;
    const r = Math.sqrt(Math.random()) * radius;
    
    return {
        x: Math.round(center.x + r * Math.cos(angle)),
        y: Math.round(center.y + r * Math.sin(angle))
    };
}

/**
 * 计算两点之间的欧几里得距离
 */
export function distance(pos1: Position, pos2: Position): number {
    const dx = pos1.x - pos2.x;
    const dy = pos1.y - pos2.y;
    return Math.sqrt(dx * dx + dy * dy);
}

/**
 * 计算两点之间的曼哈顿距离
 */
export function manhattanDistance(pos1: Position, pos2: Position): number {
    return Math.abs(pos1.x - pos2.x) + Math.abs(pos1.y - pos2.y);
}

/**
 * 限制数值在指定范围内
 */
export function clamp(value: number, min: number, max: number): number {
    return Math.max(min, Math.min(max, value));
}
