var checkStraightLine = function (coordinates) { const [x1, y1] = coordinates[0]; const [x2, y2] = coordinates[1];
// 检查是否为垂直线 if (x1 === x2) { // 检查后续点的 x 坐标是否与前两个点相同 return coordinates.every((point) => point[0] === x1); }
// 如果不是垂直线,计算斜率 a 和截距 b const a = (y2 - y1) / (x2 - x1); // 斜率 const b = y1 - a * x1; // 截距
// 检查后续点是否满足 y = ax + b for (let i = 2; i < coordinates.length; i++) { const [x, y] = coordinates[i]; if (y !== a * x + b) { returnfalse; // 如果有任何一点不满足直线方程,返回 false } }