This is performance critical. I measured and determined that using the sqrt
is faster then using the cos
method.
I am aware that this code only works for some points, so that is not an issue.
Point
is System.Drawing.Point
. _offset
is also of type Point
.
I assumed, and the profiler seemed to confirm, that the try
/catch
will not slow down the code unless an exception occurs. Please correct me if that is wrong.
/// <summary>
/// Convert from polar coordinates to rectangular coordinates.
/// Only works for points to the left of the origin.
/// </summary>
/// <param name="radius">The radius of the point in pixels.</param>
/// <param name="theta">The angle of the point in radians.</param>
/// <returns>The point in rectangular coordinates.</returns>
internal Point PolarToRectangular(
double radius,
double theta)
{
try
{
double sin = Math.Sin(theta);
// This is faster then:
// double cos = Math.Cos(theta);
double cos = -Math.Sqrt(1 - (sin * sin));
Int32 x = _offset.X + (Int32)Math.Round(radius * cos);
Int32 y = _offset.Y + (Int32)Math.Round(radius * sin);
return new Point(x, y);
}
catch (OverflowException ex)
{
ex.Data.Add("Screen polar Radius", radius);
ex.Data.Add("Screen polar Theta", theta);
throw;
}
}