我有兩個歸一化的角度(在 0 到 360 度的范圍內),并且想使用 0 到 1 的分數來比較它們。
例如:
var angle1 = 90;
var angle2 = 100;
// Something like this, but with respect to the direction the angles pointing to
var score = 0.9;
我想出了以下代碼:
function modulo(x, y){
return (x % y y) % y;
}
// Calculates the closest distance between the two angles
var relativeAngle = angle2 - angle1;
relativeAngle = modulo((relativeAngle 180), 360) - 180;
// Converts the result to a score and logs the result
console.log(1 - (Math.abs(relativeAngle) / 360));
我遇到的問題是 0, 90 給出與 0, 270 (0.75) 相同的結果,這是合乎邏輯的,但對于比較指向相反方向的角度沒有用處。
我正在尋找的是一種使用從 0 到 1 的分數來比較角度與方向的方法。有誰知道我如何讓它起作用?提前致謝!
編輯1,一個例子:

在這個影像中,綠色、黃色和紅色的角度彼此更接近,因此它們應該具有更高的相似性。
如果我猜:
紅綠 = 0.9 紅橙 = 0.8
然而,紫色盡可能遠離藍色。
所以相似性應該是這樣的:
紫藍 = 0.1
編輯 2,演算法應該如何作業:

uj5u.com熱心網友回復:
根據您的描述,您似乎想以某種方式將平均方向和分離度提煉成一個單一的值。由于混疊(您在問題中暗示了這一點),我不確定這是否可能。
比較您描述的一對角度只是計算它們的最小分離角度。
但是,按照您描述的方式比較兩組角度,還需要考慮兩組之間相對方向的差異。由于每對可以轉換為[分離,方向]對,因此可以根據笛卡爾距離對兩對進行相對評分。
// Smallest difference between angles as on a circle
// range [0, 1)
const angle = (a,b) => {
const results = a > b ? a - b : b - a;
return (results > 180 ? 360 - results : results) / 180;
}
// Midway between two angles as on a circle
// range [0, 1)
const direction = (a,b) => {
const large = (a > b ? a - b : b - a) > 180;
const results = (a b) / 2;
return (results (large ? (results >= 180 ? -180 : 180 ) : 0)) / 180;
};
// Cartesian distance score
// range [0, 1)
// x is angle, y is direction
const distance = (x0, y0, x1, y1) => {
// direction wraps around so account for that
if((y0 > y1 ? y0 - y1 : y1 - y0) > 0.5) y0 = 1;
// the `*2` is because the wrap-around distance is never more than half the interval.
return Math.sqrt((x0-x1)**2 ((y0-y1)*2)**2) / Math.SQRT2;
}
// Difference score for two angles, a and b
const diff = (a,b) => angle(a, b);
// Difference score for two pairs of angles [[a0, b0], [a1, b1]]
const diff2 = (a0, b0, a1, b1) => distance(
angle(a0, b0), direction(a0, b0),
angle(a1, b1), direction(a1, b1)
);
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/374630.html
標籤:javascript math geometry angle
