Help Understanding Function
- by Fred F.
What does the following function perform?
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;
}
This is how it is used with ATan2. I believe the actually values passed to ATan2 are always positive.
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
}