using System;
namespace RidR
{
///
/// Represents an ordered pair of integer x- and y-coordinates that defines a point in a two-dimensional plane.
///
public struct Position
{
///
/// Gets a value indicating whether this Point is empty.
///
public bool IsEmpty
{
get { return X == 0 && Y == 0; }
}
///
/// Gets or sets the x-coordinate of this point.
///
public int X
{ get; set; }
///
/// Gets or sets the y-coordiante of this point.
///
public int Y
{ get; set; }
///
/// Initializes a new instance of the Position class with the specified coordinates.
///
/// The x coordinate for this position
/// The y coordinate for this position
public Position(int x, int y)
: this()
{
X = x;
Y = y;
}
///
/// Initializes a new instance of the Position class with the specified position.
///
/// The position to copy
public Position(Position p)
: this()
{
X = p.X;
Y = p.Y;
}
///
/// Specifies whether this Position contains the same coordinates as the specified Object.
///
/// The Object to test
public override bool Equals(object obj)
{
return obj is Position && this == (Position)obj;
}
///
/// Returns a hash code for this Position.
///
public override int GetHashCode()
{
return X ^ Y;
}
///
/// The Manhatten distance between two Position instances.
///
/// The first position
/// The second position
public static int ManhattenDistance(Position a, Position b)
{
return Math.Abs(a.X - b.X) + Math.Abs(a.Y - b.Y);
}
///
/// The distance between two Position instances.
///
/// The first position
/// The second position
public static float Distance(Position a, Position b)
{
return (float)Math.Sqrt((a.X - b.X) * (a.X - b.X) + (a.Y - b.Y) * (a.Y - b.Y));
}
///
/// Converts this Position to a human-readable string
///
public override string ToString()
{
return X + " " + Y;
}
#region Operators
///
/// Compares two Position objects. The result specifies whether the values of the X and Y
/// properties of the two Positions objects are equal.
///
/// A Position to compare
/// A Position to compare
public static bool operator ==(Position lhs, Position rhs)
{
return lhs.X == rhs.X && lhs.Y == rhs.Y;
}
///
/// Compares two Position objects. The result specifies whether the values of the X and Y
/// properties of the two Positions objects are unequal.
///
/// A Position to compare
/// A Position to compare
public static bool operator !=(Position lhs, Position rhs)
{
return !(lhs == rhs);
}
///
/// Translates a Position with another Position
///
/// The position to translate
/// The position to add
public static Position operator +(Position lhs, Position rhs)
{
return new Position(lhs.X + rhs.X, lhs.Y + rhs.Y);
}
///
/// Translates a Position with the negative of another Position
///
/// The position to translate
/// The position to subtract
public static Position operator -(Position lhs, Position rhs)
{
return new Position(lhs.X - rhs.X, lhs.Y - rhs.Y);
}
///
/// Scales this position as a vector.
///
/// The scalar value
/// The position to scale
public static Position operator *(int scalar, Position p)
{
return p * scalar;
}
///
/// Scales this position as a vector.
///
/// The position to scale
/// The scalar value
public static Position operator *(Position p, int scalar)
{
return new Position(p.X * scalar, p.Y * scalar);
}
///
/// Scales the position as a vector with an inverted scalar.
///
/// The position to scale
/// The scalar to invert
public static Position operator /(Position p, int devider)
{
return new Position(p.X / devider, p.Y / devider);
}
///
/// Returns the negative of this position
///
/// The current position
public static Position operator -(Position p)
{
return new Position(-p.X, -p.Y);
}
#endregion
}
}