using System; namespace TrueCraft.API { /// /// Represents a tuple of 2D coordinates. /// public struct Coordinates2D : IEquatable { /// /// The X component of the coordinates. /// public int X; /// /// The Y component of the coordinates. /// public int Z; /// /// Creates a new pair of coordinates from the specified value. /// /// The value for the components of the coordinates. public Coordinates2D(int value) { X = Z = value; } /// /// Creates a new pair of coordinates from the specified values. /// /// The X component of the coordinates. /// The Y component of the coordinates. public Coordinates2D(int x, int z) { X = x; Z = z; } /// /// Creates a new pair of coordinates by copying another. /// /// The coordinates to copy. public Coordinates2D(Coordinates2D v) { X = v.X; Z = v.Z; } /// /// Returns the string representation of this 2D coordinates. /// /// public override string ToString() { return string.Format("<{0},{1}>", X, Z); } #region Math /// /// Calculates the distance between two Coordinates2D objects. /// public double DistanceTo(Coordinates2D other) { return Math.Sqrt(Square(other.X - X) + Square(other.Z - Z)); } /// /// Calculates the square of a num. /// private int Square(int num) { return num * num; } /// /// Finds the distance of this Coordinates2D from Coordinates2D.Zero /// public double Distance { get { return DistanceTo(Zero); } } /// /// Returns the component-wise minimum of two 2D coordinates. /// /// The first coordinates. /// The second coordinates. /// public static Coordinates2D Min(Coordinates2D value1, Coordinates2D value2) { return new Coordinates2D( Math.Min(value1.X, value2.X), Math.Min(value1.Z, value2.Z) ); } /// /// Returns the component-wise maximum of two 2D coordinates. /// /// The first coordinates. /// The second coordinates. /// public static Coordinates2D Max(Coordinates2D value1, Coordinates2D value2) { return new Coordinates2D( Math.Max(value1.X, value2.X), Math.Max(value1.Z, value2.Z) ); } #endregion #region Operators public static bool operator !=(Coordinates2D a, Coordinates2D b) { return !a.Equals(b); } public static bool operator ==(Coordinates2D a, Coordinates2D b) { return a.Equals(b); } public static Coordinates2D operator +(Coordinates2D a, Coordinates2D b) { return new Coordinates2D(a.X + b.X, a.Z + b.Z); } public static Coordinates2D operator -(Coordinates2D a, Coordinates2D b) { return new Coordinates2D(a.X - b.X, a.Z - b.Z); } public static Coordinates2D operator -(Coordinates2D a) { return new Coordinates2D( -a.X, -a.Z); } public static Coordinates2D operator *(Coordinates2D a, Coordinates2D b) { return new Coordinates2D(a.X * b.X, a.Z * b.Z); } public static Coordinates2D operator /(Coordinates2D a, Coordinates2D b) { return new Coordinates2D(a.X / b.X, a.Z / b.Z); } public static Coordinates2D operator %(Coordinates2D a, Coordinates2D b) { return new Coordinates2D(a.X % b.X, a.Z % b.Z); } public static Coordinates2D operator +(Coordinates2D a, int b) { return new Coordinates2D(a.X + b, a.Z + b); } public static Coordinates2D operator -(Coordinates2D a, int b) { return new Coordinates2D(a.X - b, a.Z - b); } public static Coordinates2D operator *(Coordinates2D a, int b) { return new Coordinates2D(a.X * b, a.Z * b); } public static Coordinates2D operator /(Coordinates2D a, int b) { return new Coordinates2D(a.X / b, a.Z / b); } public static Coordinates2D operator %(Coordinates2D a, int b) { return new Coordinates2D(a.X % b, a.Z % b); } public static Coordinates2D operator +(int a, Coordinates2D b) { return new Coordinates2D(a + b.X, a + b.Z); } public static Coordinates2D operator -(int a, Coordinates2D b) { return new Coordinates2D(a - b.X, a - b.Z); } public static Coordinates2D operator *(int a, Coordinates2D b) { return new Coordinates2D(a * b.X, a * b.Z); } public static Coordinates2D operator /(int a, Coordinates2D b) { return new Coordinates2D(a / b.X, a / b.Z); } public static Coordinates2D operator %(int a, Coordinates2D b) { return new Coordinates2D(a % b.X, a % b.Z); } public static explicit operator Coordinates2D(Coordinates3D a) { return new Coordinates2D(a.X, a.Z); } #endregion #region Constants /// /// A pair of 2D coordinates with components set to 0.0. /// public static readonly Coordinates2D Zero = new Coordinates2D(0); /// /// A pair of 2D coordinates with components set to 1.0. /// public static readonly Coordinates2D One = new Coordinates2D(1); /// /// A pair of 2D coordinates facing forwards. /// public static readonly Coordinates2D Forward = new Coordinates2D(0, 1); /// /// A pair of 2D coordinates facing backwards. /// public static readonly Coordinates2D Backward = new Coordinates2D(0, -1); /// /// A pair of 2D coordinates facing left. /// public static readonly Coordinates2D Left = new Coordinates2D(-1, 0); /// /// A pair of 2D coordinates facing right. /// public static readonly Coordinates2D Right = new Coordinates2D(1, 0); /// /// A trio of 3D coordinates facing to the east. /// public static readonly Coordinates2D East = new Coordinates2D(1, 0); /// /// A trio of 3D coordinates facing to the west. /// public static readonly Coordinates2D West = new Coordinates2D(-1, 0); /// /// A trio of 3D coordinates facing to the north. /// public static readonly Coordinates2D North = new Coordinates2D(0, -1); /// /// A trio of 3D coordinates facing to the south. /// public static readonly Coordinates2D South = new Coordinates2D(0, 1); #endregion /// /// Determines whether this 2D coordinates and another are equal. /// /// The other coordinates. /// public bool Equals(Coordinates2D other) { return other.X.Equals(X) && other.Z.Equals(Z); } /// /// Determines whether this and another object are equal. /// /// The other object. /// public override bool Equals(object obj) { if (ReferenceEquals(null, obj)) return false; if (obj.GetType() != typeof(Coordinates2D)) return false; return Equals((Coordinates2D)obj); } /// /// Returns the hash code for this 2D coordinates. /// /// public override int GetHashCode() { unchecked { int result = X.GetHashCode(); result = (result * 397) ^ Z.GetHashCode(); return result; } } } }