using System;
namespace Chernobyl.Measures
{
///
/// A rotation in degrees. 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
/// Radians.
///
[Serializable]
public struct Degree : IEquatable, IComparable
{
///
/// Constructor.
///
/// The amount in degrees.
public Degree(float value) : this()
{
Value = value;
}
///
/// Constructor.
///
/// The amount in radians.
public Degree(Radian value) : this()
{
const float radiansToDegrees = 180.0f / (float)Math.PI;
Value = value * radiansToDegrees;
}
///
/// Converts from Radians to Degrees.
///
/// The Radian to convert to Degrees
/// The representative Degree.
public static implicit operator Degree(Radian radians)
{
return new Degree(radians);
}
///
/// Converts from s to a floating point
/// representation of degrees.
///
/// The Degree to convert to float
/// The representative float.
public static implicit operator float(Degree degrees)
{
return degrees.Value;
}
///
/// Allows for a float to be explicitly casted to a degree.
///
/// The value to be casted.
/// The casted object.
public static explicit operator Degree(float value)
{
return new Degree(value);
}
///
/// Adds two Degrees.
///
/// The left side of the operation.
/// The right side of the operation.
/// The result of the operation.
public static Degree operator +(Degree left, Degree right)
{
return new Degree(left.Value + right.Value);
}
///
/// Subtracts two Degrees.
///
/// The left side of the operation.
/// The right side of the operation.
/// The result of the operation.
public static Degree operator -(Degree left, Degree right)
{
return new Degree(left.Value - right.Value);
}
///
/// Multiplies two Degrees.
///
/// The left side of the operation.
/// The right side of the operation.
/// The result of the operation.
public static Degree operator *(Degree left, Degree right)
{
return new Degree(left.Value * right.Value);
}
///
/// Divides two Degrees.
///
/// The left side of the operation.
/// The right side of the operation.
/// The result of the operation.
public static Degree operator /(Degree left, Degree right)
{
return new Degree(left.Value / right.Value);
}
///
/// Negates a Degree.
///
/// The item to negate.
/// The result of the operation.
public static Degree operator -(Degree item)
{
return new Degree(-item.Value);
}
///
/// Checks if two Degrees 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 ==(Degree left, Degree right)
{
return left.Equals(right);
}
///
/// Checks if two Degrees 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 !=(Degree left, Degree right)
{
return !(left == right);
}
///
/// Gets the hash code of the Degree.
///
/// 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(Degree other)
{
return (Value == other.Value);
}
///
/// Checks this Degree for equality with object passed in.
///
/// The object to compare to.
/// True if the object is equal to this Degree, false
/// if otherwise.
public override bool Equals(object obj)
{
return (obj is Degree && Equals((Degree)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(Degree other)
{
return Value.CompareTo(other.Value);
}
///
/// Returns a that represents this instance. For
/// example, 32.0 degrees will be returned as "32.0°".
///
///
/// A that represents this instance.
///
public override string ToString()
{
return Value.ToString() + '°';
}
///
/// The value of this Degree in degrees.
///
public float Value { get; private set; }
}
}