/* 文件名:Math.nut 路径:Tool/Math.nut 创建日期:2025-10-21 18:10 文件用途:数学工具 */ class Math { /** * 取随机值 左闭右闭 * @function * @param {integer} Min * @param {integer} Max * @returns {integer} */ function Rand(Min, Max) { local In = rand(); local Ret = (Min + (Max - Min + 1) * (In / (RAND_MAX + 1).tofloat())).tointeger(); return Ret; } /** * 随机返回数组中的任意一个数 * @function * @param {array} arr * @returns {any} */ function GetRandomElementFromArray(arr) { if (arr.len() == 0) { return null; } // 生成一个0到数组长度-1之间的随机索引 local randomIndex = Math.Rand(0, arr.len() - 1); return arr[randomIndex]; } /** * 通过坐标获得两点旋转角度 * @function * @param {integer} x1 * @param {integer} y1 * @param {integer} x2 * @param {integer} y2 * @returns {float} */ function GetRorateAngleByPos(x1, y1, x2, y2) { return (atan2((y2 - y1).tofloat(), (x2 - x1).tofloat()) * 180) / PI; } /** * * @function * @param {integer} x1 * @param {aintegerny} y1 * @param {integer} x2 * @param {integer} y2 * @returns {float} */ function GetDistanceByPos(x1, y1, x2, y2) { return sqrt(pow((x2 - x1).tofloat(), 2) + pow((y2 - y1).tofloat(), 2)); } /** * 获得两值随时间的匀速运动值 * @function * @param {any} sv * @param {any} ev * @param {float} currentRate * @param {float} maxRate * @returns {*} */ function GetUniformVelocity(sv, ev, currentRate, maxRate) { local rate = currentRate.tofloat() / maxRate.tofloat(); local varyValue = ev - sv; return sv + varyValue * rate; } /** * 获得两值随时间的变速运动值 * @function * @param {any} sv * @param {any} ev * @param {float} currentRate * @param {float} maxRate * @param {any} increaseFeature * @returns {*} */ function GetAccel(sv, ev, currentRate, maxRate, increaseFeature) { local rate = currentRate.tofloat() / maxRate.tofloat(); local varyValue = ev - sv; local increaseRate = 1.0; if (increaseFeature) { increaseRate = pow(50, rate) / 50.0; //慢->快 } else { // 修正后的减速逻辑计算,例如采用线性变换结合幂次运算来实现更合理的减速效果 // 先将rate映射到一个更合适的范围(这里从[0,1]映射到[0.1, 1],可调整) local mappedRate = rate * 0.9 + 0.1; increaseRate = pow(mappedRate, 2); // 幂次可调整,这里取2来让减速更明显,可根据实际情况修改 } return sv + varyValue * increaseRate; } /** * 计算两个矩形是否相交 * @function * @param {any} x1 * @param {any} y1 * @param {any} width1 * @param {any} height1 * @param {any} x2 * @param {any} y2 * @param {any} width2 * @param {any} height2 * @returns {boolean} */ function IsIntersectRect(x1, y1, width1, height1, x2, y2, width2, height2) { // 计算矩形1的边界 local right1 = x1 + width1; local bottom1 = y1 + height1; // 计算矩形2的边界 local right2 = x2 + width2; local bottom2 = y2 + height2; // 检查是否有重叠 return !(right1 < x2 || bottom1 < y2 || x1 > right2 || y1 > bottom2); } }