正如评论中提到的,度数和旋转是避免混淆双精度值的一个很好的例子,尤其是在 API 之间。
我拔出了我们目前正在使用的Radians
和类,它们就在这里。Degrees
现在看看它们(经过这么长时间)我想清理它们(尤其是评论/文档)并确保它们经过适当的测试。谢天谢地,我已经设法在日程安排中抽出时间来这样做。无论如何,使用这些需要您自担风险,我不能保证这里的所有数学运算是否正确,因为我很确定我们实际上并没有使用/测试我们编写的所有功能。
弧度
/// <summary>
/// Defines an angle in Radians
/// </summary>
public struct Radians
{
public static readonly Radians ZERO_PI = 0;
public static readonly Radians ONE_PI = System.Math.PI;
public static readonly Radians TWO_PI = ONE_PI * 2;
public static readonly Radians HALF_PI = ONE_PI * 0.5;
public static readonly Radians QUARTER_PI = ONE_PI * 0.25;
#region Public Members
/// <summary>
/// Angle value
/// </summary>
public double Value;
/// <summary>
/// Finds the Cosine of the angle
/// </summary>
public double Cos
{
get
{
return System.Math.Cos(this);
}
}
/// <summary>
/// Finds the Sine of the angle
/// </summary>
public double Sin
{
get
{
return System.Math.Sin(this);
}
}
#endregion
/// <summary>
/// Constructor
/// </summary>
/// <param name="value">angle value in radians</param>
public Radians(double value)
{
this.Value = value;
}
/// <summary>
/// Gets the angle in degrees
/// </summary>
/// <returns>Returns the angle in degrees</returns>
public Degrees GetDegrees()
{
return this;
}
public Radians Reduce()
{
double radian = this.Value;
bool IsNegative = radian < 0;
radian = System.Math.Abs(radian);
while (radian >= System.Math.PI * 2)
{
radian -= System.Math.PI * 2;
}
if (IsNegative && radian != 0)
{
radian = System.Math.PI * 2 - radian;
}
return radian;
}
#region operator overloading
/// <summary>
/// Conversion of Degrees to Radians
/// </summary>
/// <param name="deg"></param>
/// <returns></returns>
public static implicit operator Radians(Degrees deg)
{
return new Radians(deg.Value * System.Math.PI / 180);
}
/// <summary>
/// Conversion of integer to Radians
/// </summary>
/// <param name="i"></param>
/// <returns></returns>
public static implicit operator Radians(int i)
{
return new Radians((double)i);
}
/// <summary>
/// Conversion of float to Radians
/// </summary>
/// <param name="f"></param>
/// <returns></returns>
public static implicit operator Radians(float f)
{
return new Radians((double)f);
}
/// <summary>
/// Conversion of double to Radians
/// </summary>
/// <param name="dbl"></param>
/// <returns></returns>
public static implicit operator Radians(double dbl)
{
return new Radians(dbl);
}
/// <summary>
/// Conversion of Radians to double
/// </summary>
/// <param name="rad"></param>
/// <returns></returns>
public static implicit operator double(Radians rad)
{
return rad.Value;
}
/// <summary>
/// Add Radians and a double
/// </summary>
/// <param name="rad"></param>
/// <param name="dbl"></param>
/// <returns></returns>
public static Radians operator +(Radians rad, double dbl)
{
return new Radians(rad.Value + dbl);
}
/// <summary>
/// Add Radians to Radians
/// </summary>
/// <param name="rad1"></param>
/// <param name="rad2"></param>
/// <returns></returns>
public static Radians operator +(Radians rad1, Radians rad2)
{
return new Radians(rad1.Value + rad2.Value);
}
/// <summary>
/// Add Radians and Degrees
/// </summary>
/// <param name="rad"></param>
/// <param name="deg"></param>
/// <returns></returns>
public static Radians operator +(Radians rad, Degrees deg)
{
return new Radians(rad.Value + deg.GetRadians().Value);
}
/// <summary>
/// Sets Radians value negative
/// </summary>
/// <param name="rad"></param>
/// <returns></returns>
public static Radians operator -(Radians rad)
{
return new Radians(-rad.Value);
}
/// <summary>
/// Subtracts a double from Radians
/// </summary>
/// <param name="rad"></param>
/// <param name="dbl"></param>
/// <returns></returns>
public static Radians operator -(Radians rad, double dbl)
{
return new Radians(rad.Value - dbl);
}
/// <summary>
/// Subtracts Radians from Radians
/// </summary>
/// <param name="rad1"></param>
/// <param name="rad2"></param>
/// <returns></returns>
public static Radians operator -(Radians rad1, Radians rad2)
{
return new Radians(rad1.Value - rad2.Value);
}
/// <summary>
/// Subtracts Degrees from Radians
/// </summary>
/// <param name="rad"></param>
/// <param name="deg"></param>
/// <returns></returns>
public static Radians operator -(Radians rad, Degrees deg)
{
return new Radians(rad.Value - deg.GetRadians().Value);
}
#endregion
public override string ToString()
{
return String.Format("{0}", this.Value);
}
public static Radians Convert(object value)
{
if (value is Radians)
return (Radians)value;
if (value is Degrees)
return (Degrees)value;
return System.Convert.ToDouble(value);
}
}
度数
public struct Degrees
{
public double Value;
public Degrees(double value) { this.Value = value; }
public Radians GetRadians()
{
return this;
}
public Degrees Reduce()
{
return this.GetRadians().Reduce();
}
public double Cos
{
get
{
return System.Math.Cos(this.GetRadians());
}
}
public double Sin
{
get
{
return System.Math.Sin(this.GetRadians());
}
}
#region operator overloading
public static implicit operator Degrees(Radians rad)
{
return new Degrees(rad.Value * 180 / System.Math.PI);
}
public static implicit operator Degrees(int i)
{
return new Degrees((double)i);
}
public static implicit operator Degrees(float f)
{
return new Degrees((double)f);
}
public static implicit operator Degrees(double d)
{
return new Degrees(d);
}
public static implicit operator double(Degrees deg)
{
return deg.Value;
}
public static Degrees operator +(Degrees deg, int i)
{
return new Degrees(deg.Value + i);
}
public static Degrees operator +(Degrees deg, double dbl)
{
return new Degrees(deg.Value + dbl);
}
public static Degrees operator +(Degrees deg1, Degrees deg2)
{
return new Degrees(deg1.Value + deg2.Value);
}
public static Degrees operator +(Degrees deg, Radians rad)
{
return new Degrees(deg.Value + rad.GetDegrees().Value);
}
public static Degrees operator -(Degrees deg)
{
return new Degrees(-deg.Value);
}
public static Degrees operator -(Degrees deg, int i)
{
return new Degrees(deg.Value - i);
}
public static Degrees operator -(Degrees deg, double dbl)
{
return new Degrees(deg.Value - dbl);
}
public static Degrees operator -(Degrees deg1, Degrees deg2)
{
return new Degrees(deg1.Value - deg2.Value);
}
public static Degrees operator -(Degrees deg, Radians rad)
{
return new Degrees(deg.Value - rad.GetDegrees().Value);
}
#endregion
public override string ToString()
{
return String.Format("{0}", this.Value);
}
public static Degrees Convert(object value)
{
if (value is Degrees)
return (Degrees)value;
if (value is Radians)
return (Radians)value;
return System.Convert.ToDouble(value);
}
}
一些示例用法
这些在使用 API 时确实受益匪浅。虽然在内部,您的组织可能会决定严格使用度数或弧度以避免混淆,但至少对于这些类,您可以使用最有意义的类型。例如,公开使用的 API 或 GUI API 可以使用Degrees
,而您的大量数学/三角函数或内部使用可能使用Radians
. 考虑以下类/打印功能:
public class MyRadiansShape
{
public Radians Rotation { get; set; }
}
public class MyDegreesShape
{
public Degrees Rotation { get; set; }
}
public static void PrintRotation(Degrees degrees, Radians radians)
{
Console.WriteLine(String.Format("Degrees: {0}, Radians: {1}", degrees.Value, radians.Value));
}
是的,代码很做作(而且非常模棱两可),但没关系!只是去展示它如何帮助减少意外的混淆。
var radiansShape = new MyRadiansShape() { Rotation = Math.PI / 2}; //prefer "Radians.HALF_PI" instead, but just as an example
var degreesShape = new MyDegreesShape() { Rotation = 90 };
PrintRotation(radiansShape.Rotation, radiansShape.Rotation);
PrintRotation(degreesShape.Rotation, degreesShape.Rotation);
PrintRotation(radiansShape.Rotation + degreesShape.Rotation, radiansShape.Rotation + degreesShape.Rotation);
//Degrees: 90, Radians: 1.5707963267949
//Degrees: 90, Radians: 1.5707963267949
//Degrees: 180, Radians: 3.14159265358979
然后它们对于实现基于角度的其他数学概念非常有用,例如极坐标:
double distance = 5;
Polar polarCoordinate = new Polar(distance, (degreesShape.Rotation - radiansShape.Rotation) + Radians.QUARTER_PI);
Console.WriteLine("Polar Coordinate Angle: " + (Degrees)polarCoordinate.Angle); //because it's easier to read degrees!
//Polar Coordinate Angle: 45
最后,您可以Point2D
通过隐式转换来实现一个类(或使用 System.Windows.Point)Polar
:
Point2D cartesianCoordinate = polarCoordinate;
Console.WriteLine(cartesianCoordinate.X + ", " + cartesianCoordinate.Y);
//3.53553390593274, 3.53553390593274
正如我所说,我想再通过这些类,并可能消除double
隐式转换Radians
以避免一些可能的极端情况混淆和编译器歧义。这些实际上在我们创建 static ONE_PI
,HALF_PI
(等等)字段之前就已经存在,并且我们正在从Math.PI
double 的某个倍数进行转换。
编辑:这是Polar
作为附加隐式转换演示的类。它利用了Radians
类(以及它的隐式转换)以及它和Point2D
类上的辅助方法。我没有在这里包含它,但是Polar
该类可以轻松地实现与该类交互的运算符,Point2D
但这些与本讨论无关。
public struct Polar
{
public double Radius;
public Radians Angle;
public double X { get { return Radius * Angle.Cos; } }
public double Y { get { return Radius * Angle.Sin; } }
public Polar(double radius, Radians angle)
{
this.Radius = radius;
this.Angle = angle;
}
public Polar(Point2D point)
: this(point.Magnitude(), point.GetAngleFromOrigin())
{
}
public Polar(Point2D point, double radius)
: this(radius, point.GetAngleFromOrigin())
{
}
public Polar(Point2D point, Point2D origin)
: this(point - origin)
{
}
public Point2D ToCartesian()
{
return new Point2D(X, Y);
}
public static implicit operator Point2D(Polar polar)
{
return polar.ToCartesian();
}
public static implicit operator Polar(Point2D vector)
{
return new Polar(vector);
}
}