問題描述
給定兩個方位角,我如何找到它們之間的最小角度?
Given two bearing, how do I find the smallest angle between them?
例如,如果第一個航向是 340 度,第二個航向是 10 度,那么最小的角度就是 30 度.
So for example if 1 heading is 340 degrees and the second is 10 degrees the smallest angle will be 30 degrees.
我附上了一張圖片來說明我的意思.我試過從另一個中減去一個,但由于圓圈的環繞效果,這不起作用.我也嘗試過使用負度數(180 - 359 是 -180 到 0),但是在嘗試計算正數和負數之間的角度時會搞砸.
I've attached a picture to show what I mean. I've tried subtracting one from the other but that didn't work because of the wrap around effect of a circle. I've also tried using negative degrees (180 - 359 being -180 to 0) but that got messed up when trying to calculate the angle between positive and negative number.
我確信必須有一種更簡單的方法來處理大量 if
語句.
I'm sure there must be an easier way that having lots of if
statements.
感謝您的幫助.亞當
順便說一句.這是一個導航問題,所以圓的半徑是未知的.
BTW. This is a navigation question so the radius of the circle is unknown.
推薦答案
float getDifference(float a1, float a2) {
return Math.min((a1-a2)<0?a1-a2+360:a1-a2, (a2-a1)<0?a2-a1+360:a2-a1)
}
這篇關于找到兩個軸承之間的角度的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!