 | |  |  | 有的人用改分辩率软件,更改了手机的分辩率,为了让所有手机通用一种点击方法,计算偏差,可以用这段代码实现。 两种方法如下:
第一种方法:function calculateFunction(inputValue) {
function leastSquaresFit(x, y) {
var n = x.length;
var sumX = 0;
var sumY = 0;
var sumXY = 0;
var sumX2 = 0;
for (var i = 0; i < n; i++) {
sumX += x;
sumY += y;
sumXY += x * y;
sumX2 += x * x;
}
var denominator = (n * sumX2 - sumX * sumX);
if (denominator === 0) {
return [0, 0];
}
var a = (n * sumXY - sumX * sumY) / denominator;
var b = (sumY - a * sumX) / n;
return [a, b];
}
function linearFunction(x, a, b) {
return a * x + b;
}
function clipValue(value, maxValue) {
return Math.min(value, maxValue);
}
var x = [94, 155, 233, 405, 591, 766, 844, 930, 208];
var y = [0, 65, 157, 380, 580, 810, 916, 1021, 140];
var maxValue = 1080;
var coefficients = leastSquaresFit(x, y);
var a = coefficients[0];
var b = coefficients[1];
if (typeof inputValue === 'number') {
var predictedY = linearFunction(inputValue, a, b);
predictedY = clipValue(predictedY, maxValue);
return predictedY;
} else if (typeof inputValue === 'number' && inputValue >= 0 && inputValue <= maxValue) {
if (a === 0) {
if (b === inputValue) {
return null;
} else {
return NaN;
}
}
var predictedX = (inputValue - b) / a;
return predictedX;
} else {
throw new Error('输入不合法,请输入一个数字且范围在 0 到 ' + maxValue + ' 之间');
}
}
var result1 = calculateFunction(100);
console.log("当 x = 550 时,计算得到的 y 值为: " + result1);
var result2 = calculateFunction(1);
console.log("当 y = 0 时,计算得到的 x 值为: " + result2);
手机改了分辨率后有误差用公式计算加减,### 函数 calculateFunction 这个函数的主要目的是根据输入的数值进行线性拟合计算,可以正向计算(根据 x 值计算 y 值)或反向计算(根据 y 值计算 x 值)。 内部函数leastSquaresFit(x, y)
- 这个函数使用最小二乘法来拟合给定的数据点
x 和 y ,并返回线性函数 y = ax + b 的系数 a 和 b 。 n 是数据点的数量。sumX 、sumY 、sumXY 和 sumX2 分别是 x 的和、y 的和、x 和 y 的乘积之和、x 的平方和。- 通过这些和计算得到
a 和 b 的值。 - 如果分母为零,为了避免除以零的错误,返回
[0, 0] 。
linearFunction(x, a, b)
- 这是一个简单的线性函数,根据给定的
x 值以及拟合得到的系数 a 和 b ,计算 y 的值。
clipValue(value, maxValue)
- 这个函数用于确保返回的
y 值不超过预先设定的最大值 maxValue 。 - 使用
Math.min 来比较输入的 value 和 maxValue ,返回较小的那个值。
主要逻辑- 首先,定义了
x 和 y 的数据数组,以及 maxValue 为 1080。 - 然后,使用
leastSquaresFit 函数计算出拟合直线的系数 a 和 b 。 - 根据
inputValue 的类型和范围进行不同的计算:- 如果
inputValue 是一个数字,计算对应的 y 值。首先通过 linearFunction 计算,然后通过 clipValue 确保 y 值不超过 1080。 - 如果
inputValue 是一个数字且在 0 到 maxValue 之间,尝试反向计算对应的 x 值。但如果 a 为 0,意味着直线平行于 x 轴,根据 b 是否等于 inputValue 判断是否有多解或无解。 - 如果输入不符合要求,抛出一个错误信息。
示例调用注意事项代码中反向计算的部分有一个逻辑错误,else if (typeof inputValue === 'number' && inputValue >= 0 && inputValue <= maxValue) 这部分条件判断似乎与前面的条件重复,只要 typeof inputValue === 'number' ,就不需要再次判断 inputValue 是否在 0 到 maxValue 之间。因此这部分代码可以修改为: else if (inputValue >= 0 && inputValue <= maxValue) {
- 反向计算时,如果
a 为 0,则 b 的值就是 y 在所有 x 值上的常数值。如果 b 等于 inputValue ,那么理论上对于所有的 x 值都可以得到这个 y 值,因此返回 null 表示有多个解。如果 b 不等于 inputValue ,则没有解,返回 NaN 。
当然看上去很复杂,其实有更简单的方法: 第二种方法: function adjustPos(origPos, origRes, newRes) {
var scaleFactor = newRes / origRes;
var adjustedPos = origPos * scaleFactor;
return adjustedPos;
}
function adjustPercent(origX, origY, origWidth, origHeight, newWidth, newHeight) {
var adjX = adjustPos(origX * origWidth, origWidth, newWidth);
var adjY = adjustPos(origY * origHeight, origHeight, newHeight);
var adjXPercent = adjX / newWidth;
var adjYPercent = adjY / newHeight;
return [adjXPercent, adjYPercent];
}
function clickAdjusted(origX, origY, origWidth, origHeight, newWidth, newHeight) {
var [adjX, adjY] = adjustPercent(origX, origY, origWidth, origHeight, newWidth, newHeight);
console.log("调整后的点击位置为: (" + adjX + ", " + adjY + ")");
auto.clickPercent(adjX, adjY);
}
var origX = 0.6076;
var origY = 0.1885;
var origWidth = 1080;
var origHeight = 1920;
var newWidth = 720;
var newHeight = 1280;
clickAdjusted(origX, origY, origWidth, origHeight, newWidth, newHeight);
这段代码的主要作用是根据手机屏幕的不同分辨率,调整点击的位置,以便在不同设备上实现相同的点击效果。以下是对代码的详细解释: 调整位置的函数 adjustPos : - 该函数接受原始位置(
origPos )、原始分辨率(origRes )和新分辨率(newRes )作为参数。 它通过计算比例因子(scaleFactor ),然后将原始位置乘以这个因子来得到调整后的点击位置(adjustedPos )。 function adjustPos(origPos, origRes, newRes) { var scaleFactor = newRes / origRes;
var adjustedPos = origPos * scaleFactor;
return adjustedPos;
}
调整百分比坐标的函数 adjustPercent : - 该函数接受原始的百分比坐标(
origX 和 origY )、原始分辨率宽度和高度、以及新分辨率的宽度和高度。 - 它首先通过
adjustPos 函数计算调整后的 x 和 y 坐标。 然后,它将计算出的坐标转换回绝对百分比,得到实际在新分辨率下的点击位置。 function adjustPercent(origX, origY, origWidth, origHeight, newWidth, newHeight) { var adjX = adjustPos(origX * origWidth, origWidth, newWidth);
var adjY = adjustPos(origY * origHeight, origHeight, newHeight);
var adjXPercent = adjX / newWidth;
var adjYPercent = adjY / newHeight;
return [adjXPercent, adjYPercent];
}
示例使用部分: - 定义原始的百分比坐标和分辨率。
使用 adjustPercent 函数计算在新分辨率下的调整坐标,并打印出来。 var origX = 0.6076; var origY = 0.1885; var origWidth = 1080; var origHeight = 1920; var newWidth = 720; var newHeight = 1280; var [adjX, adjY] = adjustPercent(origX, origY, origWidth, origHeight, newWidth, newHeight); console.log(“调整后的点击位置为: (“ + adjX + “, “ + adjY + “)”);
自动点击方法 autoClick : - 这个函数是一个占位符,用于执行实际的点击操作。
当前的实现只是简单输出点击的位置。 function autoClick(xPercent, yPercent) { console.log("在屏幕上点击: (" + xPercent + ", " + yPercent + ")");
}
调用自动点击方法:
这个代码的主要目的在于使得在不同的屏幕分辨率下,点击的行为能够保持一致,从而提高了在不同设备上的用户体验。你提到的 auto.clickPercent(0.401, 0.2012) 是你自己使用的点击方法,可以直接替换最后一行的 autoClick 调用
| |  | |  |
|