// MIT License - Copyright (C) The Mono.Xna Team
// This file is subject to the terms and conditions defined in
// file 'LICENSE.txt', which is part of this source code package.
using System;
using System.Diagnostics;
using System.Runtime.Serialization;
namespace CommonLang.Geometry
{
///
/// Describes a 2D-point.
///
public struct Point : IEquatable
{
#region Private Fields
private static readonly Point zeroPoint = new Point();
#endregion
#region Public Fields
///
/// The x coordinate of this .
///
public int X;
///
/// The y coordinate of this .
///
public int Y;
#endregion
#region Properties
///
/// Returns a with coordinates 0, 0.
///
public static Point Zero
{
get { return zeroPoint; }
}
#endregion
#region Internal Properties
internal string DebugDisplayString
{
get
{
return string.Concat(
this.X.ToString(), " ",
this.Y.ToString()
);
}
}
#endregion
#region Constructors
///
/// Constructs a point wit X and Y from two values.
///
/// The x coordinate in 2d-space.
/// The y coordinate in 2d-space.
public Point(int x, int y)
{
this.X = x;
this.Y = y;
}
///
/// Constructs a point with X and Y set to the same value.
///
/// The x and y coordinates in 2d-space.
public Point(int value)
{
this.X = value;
this.Y = value;
}
#endregion
#region Operators
///
/// Adds two points.
///
/// Source on the left of the add sign.
/// Source on the right of the add sign.
/// Sum of the points.
public static Point operator +(Point value1, Point value2)
{
return new Point(value1.X + value2.X, value1.Y + value2.Y);
}
///
/// Subtracts a from a .
///
/// Source on the left of the sub sign.
/// Source on the right of the sub sign.
/// Result of the subtraction.
public static Point operator -(Point value1, Point value2)
{
return new Point(value1.X - value2.X, value1.Y - value2.Y);
}
///
/// Multiplies the components of two points by each other.
///
/// Source on the left of the mul sign.
/// Source on the right of the mul sign.
/// Result of the multiplication.
public static Point operator *(Point value1, Point value2)
{
return new Point(value1.X * value2.X, value1.Y * value2.Y);
}
///
/// Divides the components of a by the components of another .
///
/// Source on the left of the div sign.
/// Divisor on the right of the div sign.
/// The result of dividing the points.
public static Point operator /(Point source, Point divisor)
{
return new Point(source.X / divisor.X, source.Y / divisor.Y);
}
///
/// Compares whether two instances are equal.
///
/// instance on the left of the equal sign.
/// instance on the right of the equal sign.
/// true if the instances are equal; false otherwise.
public static bool operator ==(Point a, Point b)
{
return a.Equals(b);
}
///
/// Compares whether two instances are not equal.
///
/// instance on the left of the not equal sign.
/// instance on the right of the not equal sign.
/// true if the instances are not equal; false otherwise.
public static bool operator !=(Point a, Point b)
{
return !a.Equals(b);
}
#endregion
#region Public methods
///
/// Compares whether current instance is equal to specified .
///
/// The to compare.
/// true if the instances are equal; false otherwise.
public override bool Equals(object obj)
{
return (obj is Point) && Equals((Point)obj);
}
///
/// Compares whether current instance is equal to specified .
///
/// The to compare.
/// true if the instances are equal; false otherwise.
public bool Equals(Point other)
{
return ((X == other.X) && (Y == other.Y));
}
///
/// Gets the hash code of this .
///
/// Hash code of this .
public override int GetHashCode()
{
return X ^ Y;
}
///
/// Returns a representation of this in the format:
/// {X:[] Y:[]}
///
/// representation of this .
public override string ToString()
{
return "{X:" + X + " Y:" + Y + "}";
}
///
/// Gets a representation for this object.
///
/// A representation for this object.
public Vector2 ToVector2()
{
return new Vector2(X, Y);
}
#endregion
}
}