例如:角度 10 與 270 ~ 390 交叉,因為 270 ~ 390 包括 270 ~ 360 和 0 ~ 30。
InRange(0, 180, 90);//true;
InRange(180, 270, 90);//false;
InRange(270, 390, 10);//false; <== but this is in range , How to fix?
還有我的方法:
public bool InRange(int start, int end, int angle)
{
if (angle >= start && angle <= end)
{
return true;
}
return false;
}
uj5u.com熱心網友回復:
規范化時,使用%(或我自己的Mod,以便重新發明輪子),if因為它們比回圈使用while.
class Program
{
static int Mod(int x, int n)
{
if (x < 0)
return -Mod(-x, n);
if (n < 0)
return Mod(x, -n);
int m = x / n;
return x - m * n;
}
// Normalize x to be between left inclusive and right exclusive.
static int Normalize(int left, int right, int x)
{
int period = right - left;
// int temp = x % period;
int temp = Mod(x, period); // reinvent the wheel!
if (temp < left)
return temp period;
if (temp >= right)
return temp - period;
return temp;
}
// Test whether x between start and end inclusive
static bool InRange(int start, int end, int x)
{
start = Normalize(0, 360, start);
end = Normalize(0, 360, end);
x = Normalize(0, 360, x);
if (start <= end)
return start <= x && x <= end;
else
return !(end < x && x < start);
}
static void Main()
{
System.Console.WriteLine(InRange(0, 180, 90)); // must be true
System.Console.WriteLine(InRange(180, 270, 90)); // must be false
System.Console.WriteLine(InRange(270, 390, 10)); // must be true
System.Console.WriteLine(InRange(270, 180, 90)); // must be true
System.Console.WriteLine(InRange(270, 270, -90)); // must be true
}
}
uj5u.com熱心網友回復:
確保范圍標準化 (390>270)
求中角m = (270 390)/2和半角h = (390-270)/2
將所需角度差的余弦值a和中間m 值的余弦值與半角余弦值進行比較h (如果您的語言需要,將度數轉換為弧度)。這種方法有助于避免周期性等問題
if cos(a - m) >= cos(h)
{a inside the range}

uj5u.com熱心網友回復:
將所有角度移動到一致的圓內:
static bool InR(int f, int t, int w){
f%=360;
t%=360;
w%=360;
while(f<0) f = 360;
while(t<f) t = 360;
while(w<f) w = 360;
return f <= w && w <= t;
}
uj5u.com熱心網友回復:
讓我假設0° ≤ start < 360°和0° < end - start ≤ 360°。
有兩種情況:
end < 360°: 條件是start ≤ angle mod 360° ≤ end。end ≥ 360°: 條件是not (end - 360° ≤ angle mod 360° ≤ start)。
注意:通常的%運算子不會對負引數執行真正的模運算。
uj5u.com熱心網友回復:
當心!
以下內容不在范圍內:
InRange(270, 390, 10);//false;
因為:
if (10 >= 270 && 10<= 390)=> 假
因為
10 is not bigger then 270
下次你應該除錯你的程式,以便快速找到錯誤。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/464885.html
上一篇:如何繪制相對于零的時間序列?
下一篇:在矩形網格上制作和連接六邊形
