using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TrueCraft.API
{
///
/// Represents a tuple of 3D coordinates.
///
public struct Coordinates3D : IEquatable
{
///
/// The X component of the coordinates.
///
public int X;
///
/// The Y component of the coordinates.
///
public int Y;
///
/// The Z component of the coordinates.
///
public int Z;
///
/// Creates a new trio of coordinates from the specified value.
///
/// The value for the components of the coordinates.
public Coordinates3D(int value)
{
X = Y = Z = value;
}
///
/// Creates a new trio of coordinates from the specified values.
///
/// The X component of the coordinates.
/// The Y component of the coordinates.
/// The Z component of the coordinates.
public Coordinates3D(int x = 0, int y = 0, int z = 0)
{
X = x;
Y = y;
Z = z;
}
///
/// Creates a new trio of coordinates by copying another.
///
/// The coordinates to copy.
public Coordinates3D(Coordinates3D v)
{
X = v.X;
Y = v.Y;
Z = v.Z;
}
///
/// Converts this Coordinates3D to a string in the format <x, y, z>.
///
///
public override string ToString()
{
return string.Format("<{0},{1},{2}>", X, Y, Z);
}
#region Math
///
/// Clamps the coordinates to within the specified value.
///
/// Value.
public void Clamp(int value)
{
// TODO: Fix for negative values
if (Math.Abs(X) > value)
X = value * (X < 0 ? -1 : 1);
if (Math.Abs(Y) > value)
Y = value * (Y < 0 ? -1 : 1);
if (Math.Abs(Z) > value)
Z = value * (Z < 0 ? -1 : 1);
}
///
/// Calculates the distance between two Coordinates3D objects.
///
public double DistanceTo(Coordinates3D other)
{
return Math.Sqrt(Square(other.X - X) +
Square(other.Y - Y) +
Square(other.Z - Z));
}
///
/// Calculates the square of a num.
///
private int Square(int num)
{
return num * num;
}
///
/// Finds the distance of this Coordinate3D from Coordinates3D.Zero
///
public double Distance
{
get
{
return DistanceTo(Zero);
}
}
///
/// Returns the component-wise minimum of two 3D coordinates.
///
/// The first coordinates.
/// The second coordinates.
///
public static Coordinates3D Min(Coordinates3D value1, Coordinates3D value2)
{
return new Coordinates3D(
Math.Min(value1.X, value2.X),
Math.Min(value1.Y, value2.Y),
Math.Min(value1.Z, value2.Z)
);
}
///
/// Returns the component-wise maximum of two 3D coordinates.
///
/// The first coordinates.
/// The second coordinates.
///
public static Coordinates3D Max(Coordinates3D value1, Coordinates3D value2)
{
return new Coordinates3D(
Math.Max(value1.X, value2.X),
Math.Max(value1.Y, value2.Y),
Math.Max(value1.Z, value2.Z)
);
}
#endregion
#region Operators
public static bool operator !=(Coordinates3D a, Coordinates3D b)
{
return !a.Equals(b);
}
public static bool operator ==(Coordinates3D a, Coordinates3D b)
{
return a.Equals(b);
}
public static Coordinates3D operator +(Coordinates3D a, Coordinates3D b)
{
return new Coordinates3D(a.X + b.X, a.Y + b.Y, a.Z + b.Z);
}
public static Coordinates3D operator -(Coordinates3D a, Coordinates3D b)
{
return new Coordinates3D(a.X - b.X, a.Y - b.Y, a.Z - b.Z);
}
public static Coordinates3D operator -(Coordinates3D a)
{
return new Coordinates3D(-a.X, -a.Y, -a.Z);
}
public static Coordinates3D operator *(Coordinates3D a, Coordinates3D b)
{
return new Coordinates3D(a.X * b.X, a.Y * b.Y, a.Z * b.Z);
}
public static Coordinates3D operator /(Coordinates3D a, Coordinates3D b)
{
return new Coordinates3D(a.X / b.X, a.Y / b.Y, a.Z / b.Z);
}
public static Coordinates3D operator %(Coordinates3D a, Coordinates3D b)
{
return new Coordinates3D(a.X % b.X, a.Y % b.Y, a.Z % b.Z);
}
public static Coordinates3D operator +(Coordinates3D a, int b)
{
return new Coordinates3D(a.X + b, a.Y + b, a.Z + b);
}
public static Coordinates3D operator -(Coordinates3D a, int b)
{
return new Coordinates3D(a.X - b, a.Y - b, a.Z - b);
}
public static Coordinates3D operator *(Coordinates3D a, int b)
{
return new Coordinates3D(a.X * b, a.Y * b, a.Z * b);
}
public static Coordinates3D operator /(Coordinates3D a, int b)
{
return new Coordinates3D(a.X / b, a.Y / b, a.Z / b);
}
public static Coordinates3D operator %(Coordinates3D a, int b)
{
return new Coordinates3D(a.X % b, a.Y % b, a.Z % b);
}
public static Coordinates3D operator +(int a, Coordinates3D b)
{
return new Coordinates3D(a + b.X, a + b.Y, a + b.Z);
}
public static Coordinates3D operator -(int a, Coordinates3D b)
{
return new Coordinates3D(a - b.X, a - b.Y, a - b.Z);
}
public static Coordinates3D operator *(int a, Coordinates3D b)
{
return new Coordinates3D(a * b.X, a * b.Y, a * b.Z);
}
public static Coordinates3D operator /(int a, Coordinates3D b)
{
return new Coordinates3D(a / b.X, a / b.Y, a / b.Z);
}
public static Coordinates3D operator %(int a, Coordinates3D b)
{
return new Coordinates3D(a % b.X, a % b.Y, a % b.Z);
}
#endregion
#region Conversion operators
public static explicit operator Coordinates3D(Coordinates2D a)
{
return new Coordinates3D(a.X, 0, a.Z);
}
public static explicit operator Coordinates3D(Vector3 a)
{
return new Coordinates3D((int)a.X,
(int)a.Y,
(int)a.Z);
}
public static explicit operator Coordinates3D(Size s)
{
return new Coordinates3D((int)s.Width,
(int)s.Height,
(int)s.Depth);
}
#endregion
#region Constants
///
/// A trio of 3D coordinates with components set to 0.0.
///
public static readonly Coordinates3D Zero = new Coordinates3D(0);
///
/// A trio of 3D coordinates with components set to 0.0.
///
public static readonly Coordinates3D One = new Coordinates3D(1);
///
/// A trio of 3D coordinates facing up.
///
public static readonly Coordinates3D Up = new Coordinates3D(0, 1, 0);
///
/// A trio of 3D coordinates facing down.
///
public static readonly Coordinates3D Down = new Coordinates3D(0, -1, 0);
///
/// A trio of 3D coordinates facing left.
///
public static readonly Coordinates3D Left = new Coordinates3D(-1, 0, 0);
///
/// A trio of 3D coordinates facing right.
///
public static readonly Coordinates3D Right = new Coordinates3D(1, 0, 0);
///
/// A trio of 3D coordinates facing backwards.
///
public static readonly Coordinates3D Backwards = new Coordinates3D(0, 0, -1);
///
/// A trio of 3D coordinates facing forwards.
///
public static readonly Coordinates3D Forwards = new Coordinates3D(0, 0, 1);
///
/// A trio of 3D coordinates facing to the east.
///
public static readonly Coordinates3D East = new Coordinates3D(1, 0, 0);
///
/// A trio of 3D coordinates facing to the west.
///
public static readonly Coordinates3D West = new Coordinates3D(-1, 0, 0);
///
/// A trio of 3D coordinates facing to the north.
///
public static readonly Coordinates3D North = new Coordinates3D(0, 0, -1);
///
/// A trio of 3D coordinates facing to the south.
///
public static readonly Coordinates3D South = new Coordinates3D(0, 0, 1);
///
/// A trio of 3D coordinates facing the X axis at unit length.
///
public static readonly Coordinates3D OneX = new Coordinates3D(1, 0, 0);
///
/// A trio of 3D coordinates facing the Y axis at unit length.
///
public static readonly Coordinates3D OneY = new Coordinates3D(0, 1, 0);
///
/// A trio of 3D coordinates facing the Z axis at unit length.
///
public static readonly Coordinates3D OneZ = new Coordinates3D(0, 0, 1);
#endregion
///
/// Determines whether this 3D coordinates and another are equal.
///
/// The other coordinates.
///
public bool Equals(Coordinates3D other)
{
return other.X.Equals(X) && other.Y.Equals(Y) && 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(Coordinates3D)) return false;
return Equals((Coordinates3D)obj);
}
///
/// Returns the hash code for this 3D coordinates.
///
///
public override int GetHashCode()
{
unchecked
{
int result = X.GetHashCode();
result = (result * 397) ^ Y.GetHashCode();
result = (result * 397) ^ Z.GetHashCode();
return result;
}
}
}
}