using System; namespace Chernobyl.Measures { /// /// A rotation in Radians. Used when a degree value is required. /// Often times, a person can mistake degrees for radians or vice /// versa and this can cause hard to detect bugs. This struct helps /// alleviate that problem. This struct can automatically convert to /// Degrees. /// [Serializable] public struct Radian : IEquatable, IComparable { /// /// Constructor. /// /// The amount in radians. public Radian(float value) : this() { Value = value; } /// /// Constructor. /// /// The amount in degrees. public Radian(Degree value) : this() { const float degreeToRadian = (float)Math.PI / 180.0f; Value = value * degreeToRadian; } /// /// Converts from Degrees to Radians. /// /// The Degree to convert to Radians. /// The representative Radian. public static implicit operator Radian(Degree number) { return new Radian(number); } /// /// Converts from s to a floating point /// representation of radians. /// /// The to convert to float /// The representative float. public static implicit operator float(Radian radians) { return radians.Value; } /// /// Allows for a float to be explicitly casted to a radian. /// /// The value to be casted. /// The casted object. public static explicit operator Radian(float value) { return new Radian(value); } /// /// Adds two Radians. /// /// The left side of the operation. /// The right side of the operation. /// The result of the operation. public static Radian operator +(Radian left, Radian right) { return new Radian(left.Value + right.Value); } /// /// Subtracts two Radians. /// /// The left side of the operation. /// The right side of the operation. /// The result of the operation. public static Radian operator -(Radian left, Radian right) { return new Radian(left.Value - right.Value); } /// /// Multiplies two Radians. /// /// The left side of the operation. /// The right side of the operation. /// The result of the operation. public static Radian operator *(Radian left, Radian right) { return new Radian(left.Value * right.Value); } /// /// Divides two Radians. /// /// The left side of the operation. /// The right side of the operation. /// The result of the operation. public static Radian operator /(Radian left, Radian right) { return new Radian(left.Value / right.Value); } /// /// Negates a Radian. /// /// The item to negate. /// The result of the operation. public static Radian operator -(Radian item) { return new Radian(-item.Value); } /// /// Checks if two Radians are equal to each other. /// /// The left side of the operation. /// The right side of the operation. /// True if the two are equal, false if otherwise. public static bool operator ==(Radian left, Radian right) { return left.Equals(right); } /// /// Checks if two Radians are equal to each other. /// /// The left side of the operation. /// The right side of the operation. /// True if the two are different, false if otherwise. public static bool operator !=(Radian left, Radian right) { return !(left == right); } /// /// Gets the hash code of the Radian. /// /// The hash code of the number. public override int GetHashCode() { return Value.GetHashCode(); } /// /// Indicates whether the current object is equal to another object of /// the same type. /// /// An object to compare with this object. /// /// true if the current object is equal to the /// parameter; otherwise, false. /// public bool Equals(Radian other) { return Value == other.Value; } /// /// Checks this Radian for equality with object passed in. /// /// The object to compare to. /// True if the object is equal to this Radian, false /// if otherwise. public override bool Equals(object obj) { return (obj is Radian && Equals((Radian)obj)); } /// /// Compares the current object with another object of the same type. /// /// An object to compare with this object. /// /// A 32-bit signed integer that indicates the relative order of the /// objects being compared. The return value has the following meanings: /// Value Less than zero - this object is less than the /// parameter. /// Zero - this object is equal to . /// Greater than zero - this object is greater than /// . /// public int CompareTo(Radian other) { return Value.CompareTo(other.Value); } /// /// Returns a that represents this instance. For /// example, 1.2 radians will be returned as "1.2 rad". /// /// /// A that represents this instance. /// public override string ToString() { return Value + " rad"; } /// /// The value of this Radian in radians. /// public float Value { get; private set; } } }