以下功能執行什麼功能?幫助瞭解功能
public static double CleanAngle(double angle) {
while (angle < 0)
angle += 2 * System.Math.PI;
while (angle > 2 * System.Math.PI)
angle -= 2 * System.Math.PI;
return angle;
}
這是如何與ATan2一起使用的。我相信傳給ATan2的實際價值總是正面的。
static void Main(string[] args) {
int q = 1;
//'x- and y-coordinates will always be positive values
//'therefore, do i need to "clean"?
foreach (Point oPoint in new Point[] { new Point(8,20), new Point(-8,20), new Point(8,-20), new Point(-8,-20)}) {
Debug.WriteLine(Math.Atan2(oPoint.Y, oPoint.X), "unclean " + q.ToString());
Debug.WriteLine(CleanAngle(Math.Atan2(oPoint.Y, oPoint.X)), "cleaned " + q.ToString());
q++;
}
//'output
//'unclean 1: 1.19028994968253
//'cleaned 1: 1.19028994968253
//'unclean 2: 1.95130270390726
//'cleaned 2: 1.95130270390726
//'unclean 3: -1.19028994968253
//'cleaned 3: 5.09289535749705
//'unclean 4: -1.95130270390726
//'cleaned 4: 4.33188260327232
}
UPDATE
謝謝大家對你的答案。對於每個答案還有另外一個問題。
他們爲什麼要「正常化」角度?這是代碼的一部分。
double _theta = Math.ATan2(oEnd.Y - _start.Y, oEnd.X - _start.X);
Point oCenter = new Point();
oCenter.X = (int)(_start.X + _distanceTravelled * Math.Cos(_theta));
oCenter.Y = (int)(_start.Y + _distanceTravelled * Math.Sin(_theta));
//'move barrage
this.Left = oCenter.X - this.Width/2;
this.Top = oCenter.Y - this.Height/2;
非常感謝你的。你的「身份」很好地解釋了這一點。 – 2010-04-10 20:37:07