从角度数组顺时针和逆时针获取最接近的角度

我从原点有四个角度的 div 角,假设 θ 与这些角度比较,我还有一个新角度,现在我需要逆时针和顺时针最接近 θ 的角度。


θ = -26 // new angle

a = [-15, 15, -165, -195]; // array of angles


anticlockangle = ? // closest angle anticlockwise to θ from array

clockangle = ? // closest angle clockwise to θ from array


Smart猫小萌
浏览 156回答 4
4回答

噜噜哒

在我看来,这是最优化的解决方案。我们按顺序对数组进行排序,然后根据 find 和我们的条件查找数组的下一个值。const θ = 90 // new anglelet a = [-15, 15, -165, -195]; // array of anglea.sort((a, b) => a-b);let anticlockangle;for (i = θ; i > -360; i -= 1) {&nbsp; if (a.find(element => element === i)) {&nbsp; &nbsp; anticlockangle = i;&nbsp; &nbsp; break;&nbsp; }}if (anticlockangle === undefined) {&nbsp;anticlockangle = a[a.length - 1];&nbsp;}let clockangle;for (i = θ; i < 360; i += 1) {&nbsp; if (a.find(element => element === i)) {&nbsp; &nbsp; clockangle = i;&nbsp; &nbsp; break;&nbsp; }}if (clockangle === undefined) {&nbsp; clockangle = a[0];}console.log(anticlockangle);console.log(clockangle);

四季花海

好的,根据 ag-dev 在修订历史中的第一个答案,我想出了以下内容,θ = -26 // new anglea = [-15, 15, -165, -195]; // array of anglesanticlockangle = getClosestAngle(θ,a,false);clockangle = getClosestAngle(θ,a,true);console.log(anticlockangle);console.log(clockangle);&nbsp;function getClosestAngle(θ, arr, is_clock) {&nbsp; &nbsp; arr.sort();&nbsp; &nbsp; return is_clock ? arr.find(element => element < θ) : arr.find(element => element > θ);&nbsp; }

元芳怎么了

我的解决方案考虑了环绕并适用于任何角度。即使输入范围超出标准范围 0°...360° 和 -180° ... +180°(就像示例中的 -195 值一样)或者如果值是浮点数而不是整数。我现在修复了程序并用适当的 javascript 编写了它 - 并在https://playcode.io/上对其进行了测试θi = -26;ai = [-15, 15, -165, -195];// loop test: ai = [-161.5, -82.6, +53.7, +174.8]; // array of anglesa = [0, 0, 0, 0]for (i = 0; i < ai.length; i++){&nbsp; &nbsp; a[i] = getValueBetween0And360(ai[i]);}console.log(`a = ${ai} (${a})`);// loop test: for(θi = -400; θi <= +400; θi += 33.3) // new angle{&nbsp; &nbsp; θ = getValueBetween0And360(θi);&nbsp; &nbsp; //console.log(`θi = ${θi}, θ = ${θ}`);&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; ccw = ai[getIndexOfNearestAngle(a, θ, -1)]; // closest angle anticlockwise to θ from array&nbsp; &nbsp; cw = ai[getIndexOfNearestAngle(a, θ, +1)]; // closest angle clockwise to θ from array&nbsp; &nbsp; console.log(`θ = ${θi.toFixed(1)} (${θ.toFixed(1)}),\tccw = ${ccw.toFixed(1)} (${getValueBetween0And360(ccw).toFixed(1)}),\tcw = ${cw.toFixed(1)} (${getValueBetween0And360(cw).toFixed(1)})`);}function getValueBetween0And360(input){&nbsp; &nbsp; if (input < 0)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return (input + (Math.trunc(-input / 360) + 1.0) * 360.0) % 360.0;&nbsp; &nbsp; }&nbsp; &nbsp; return input % 360.0;}function getValueBetweenPlusMinus180(input){&nbsp; &nbsp; in360 = getValueBetween0And360(input);&nbsp; &nbsp; return 180 < in360&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;? in360 - 360&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: in360;}// sign = +1 for clock wise (cw), -1 for counter clock wise (ccw)// starting from angle θ towards found angle in array afunction getIndexOfNearestAngle(a, θ, sign){&nbsp; &nbsp; var iF = -1;&nbsp; &nbsp; var diffF = 1000;&nbsp; &nbsp; for (var i = 0; i < a.length; i++)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; var diff = sign * getDiffClockWise(a[i], θ);&nbsp; &nbsp; &nbsp; &nbsp; var diffPos = getValueBetween0And360(diff);&nbsp; &nbsp; &nbsp; &nbsp; //console.log(`start a[${i}] = ${a[i]}, diffPos = ${diffPos}, iF = ${iF}, diffF = ${diffF}, sign = ${sign}`);&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; if (diffPos < diffF)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; diffF = diffPos;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; iF = i;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; //console.log(`end&nbsp; &nbsp;a[${i}] = ${a[i]}, diffPos = ${diffPos}, iF = ${iF}, diffF = ${diffF}`);&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; return iF;}&nbsp;function getDiffClockWise(a, θ){&nbsp; &nbsp; //console.log(`diff = ${a - θ}, a = ${a}, θ = ${θ}`)&nbsp; &nbsp; return a - θ;}结果是:a = -15,15,-165,-195 (345,15,195,165)θ = -26.0 (334.0),&nbsp; ccw = -165.0 (195.0),&nbsp; &nbsp;cw = -15.0 (345.0)如果// loop test:在代码中没有替换,它会报告这些示例显示环绕和浮动工作:a = -161.5,-82.6,53.7,174.8 (198.5,277.4,53.7,174.8)θ = -400.0 (320.0), ccw = -82.6 (277.4),&nbsp; &nbsp; cw = 53.7 (53.7)θ = -366.7 (353.3), ccw = -82.6 (277.4),&nbsp; &nbsp; cw = 53.7 (53.7)θ = -333.4 (26.6),&nbsp; ccw = -82.6 (277.4),&nbsp; &nbsp; cw = 53.7 (53.7)θ = -300.1 (59.9),&nbsp; ccw = 53.7 (53.7),&nbsp; cw = 174.8 (174.8)θ = -266.8 (93.2),&nbsp; ccw = 53.7 (53.7),&nbsp; cw = 174.8 (174.8)θ = -233.5 (126.5), ccw = 53.7 (53.7),&nbsp; cw = 174.8 (174.8)θ = -200.2 (159.8), ccw = 53.7 (53.7),&nbsp; cw = 174.8 (174.8)θ = -166.9 (193.1), ccw = 174.8 (174.8),&nbsp; &nbsp; cw = -161.5 (198.5)θ = -133.6 (226.4), ccw = -161.5 (198.5),&nbsp; &nbsp;cw = -82.6 (277.4)θ = -100.3 (259.7), ccw = -161.5 (198.5),&nbsp; &nbsp;cw = -82.6 (277.4)θ = -67.0 (293.0),&nbsp; ccw = -82.6 (277.4),&nbsp; &nbsp; cw = 53.7 (53.7)θ = -33.7 (326.3),&nbsp; ccw = -82.6 (277.4),&nbsp; &nbsp; cw = 53.7 (53.7)θ = -0.4 (359.6),&nbsp; &nbsp;ccw = -82.6 (277.4),&nbsp; &nbsp; cw = 53.7 (53.7)θ = 32.9 (32.9),&nbsp; &nbsp; ccw = -82.6 (277.4),&nbsp; &nbsp; cw = 53.7 (53.7)θ = 66.2 (66.2),&nbsp; &nbsp; ccw = 53.7 (53.7),&nbsp; cw = 174.8 (174.8)θ = 99.5 (99.5),&nbsp; &nbsp; ccw = 53.7 (53.7),&nbsp; cw = 174.8 (174.8)θ = 132.8 (132.8),&nbsp; ccw = 53.7 (53.7),&nbsp; cw = 174.8 (174.8)θ = 166.1 (166.1),&nbsp; ccw = 53.7 (53.7),&nbsp; cw = 174.8 (174.8)θ = 199.4 (199.4),&nbsp; ccw = -161.5 (198.5),&nbsp; &nbsp;cw = -82.6 (277.4)θ = 232.7 (232.7),&nbsp; ccw = -161.5 (198.5),&nbsp; &nbsp;cw = -82.6 (277.4)θ = 266.0 (266.0),&nbsp; ccw = -161.5 (198.5),&nbsp; &nbsp;cw = -82.6 (277.4)θ = 299.3 (299.3),&nbsp; ccw = -82.6 (277.4),&nbsp; &nbsp; cw = 53.7 (53.7)θ = 332.6 (332.6),&nbsp; ccw = -82.6 (277.4),&nbsp; &nbsp; cw = 53.7 (53.7)θ = 365.9 (5.9),&nbsp; &nbsp; ccw = -82.6 (277.4),&nbsp; &nbsp; cw = 53.7 (53.7)θ = 399.2 (39.2),&nbsp; &nbsp;ccw = -82.6 (277.4),&nbsp; &nbsp; cw = 53.7 (53.7)

动漫人物

这不是最佳解决方案,但这样的事情应该会让你接近:const theta = -26const angles = [-15, 15, -165, -195]let lowestAngle = nulllet highestAngle = nulllet previousAngle = nulllet nextAngle = nullfor (let index = 0; index < a.length; index++) {&nbsp; const angle = angles[index]&nbsp; if (lowestAngle === null || angle < lowestAngle) {&nbsp; &nbsp; lowestAngle = angle&nbsp; }&nbsp; if (highestAngle === null || angle > highestAngle) {&nbsp; &nbsp; highestAngle = angle&nbsp; }&nbsp; if (angle < theta) {&nbsp; &nbsp; // If this is the first angle less than theta or if this angle is closer to theta than the current previous, save it&nbsp; &nbsp; if (previousAngle === null || angle > previousAngle) {&nbsp; &nbsp; &nbsp; previousAngle = angle&nbsp; &nbsp; }&nbsp; }&nbsp; else if (angle > theta) {&nbsp; &nbsp; // If this is the first angle greater than theta or if this angle is closer to theta than the current next, save it&nbsp; &nbsp; if (nextAngle === null || angle < nextAngle) {&nbsp; &nbsp; &nbsp; nextAngle = angle&nbsp; &nbsp; }&nbsp; }&nbsp; else {&nbsp; &nbsp; // angle matches theta...what do you want to do here?&nbsp; }}// No previous angle found; loop around to the highest angleif (previousAngle === null) {&nbsp; previousAngle = highestAngle}// No next angle found; loop around to the lowest angleif (nextAngle === null) {&nbsp; nextAngle = lowestAngle}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript