// ----------------------------------------------------------------------- // // Copyright 2014 MIT Licence. See licence.md for more information. // // ----------------------------------------------------------------------- namespace Perspex { using System.Globalization; /// /// Defines a point. /// public struct Point { /// /// The X position. /// private double x; /// /// The Y position. /// private double y; /// /// Initializes a new instance of the structure. /// /// The X position. /// The Y position. public Point(double x, double y) { this.x = x; this.y = y; } /// /// Gets the X position. /// public double X { get { return this.x; } } /// /// Gets the Y position. /// public double Y { get { return this.y; } } /// /// Converts the to a . /// /// The point. public static implicit operator Vector(Point p) { return new Vector(p.x, p.y); } /// /// Checks for equality between two s. /// /// The first point. /// The second point. /// True if the points are equal; otherwise false. public static bool operator ==(Point left, Point right) { return left.X == right.X && left.Y == right.Y; } /// /// Checks for unequality between two s. /// /// The first point. /// The second point. /// True if the points are unequal; otherwise false. public static bool operator !=(Point left, Point right) { return !(left == right); } /// /// Adds two points. /// /// The first point. /// The second point. /// A point that is the result of the addition. public static Point operator +(Point a, Point b) { return new Point(a.x + b.x, a.y + b.y); } /// /// Adds a vector to a point. /// /// The point. /// The vector. /// A point that is the result of the addition. public static Point operator +(Point a, Vector b) { return new Point(a.x + b.X, a.y + b.Y); } /// /// Subtracts two points. /// /// The first point. /// The second point. /// A point that is the result of the subtraction. public static Point operator -(Point a, Point b) { return new Point(a.x - b.x, a.y - b.y); } /// /// Subtracts a vector from a point. /// /// The point. /// The vector. /// A point that is the result of the subtraction. public static Point operator -(Point a, Vector b) { return new Point(a.x - b.X, a.y - b.Y); } /// /// Applies a matrix to a point. /// /// The point. /// The matrix. /// The resulting point. public static Point operator *(Point point, Matrix matrix) { return new Point( (point.X * matrix.M11) + (point.Y * matrix.M21) + matrix.M31, (point.X * matrix.M12) + (point.Y * matrix.M22) + matrix.M32); } /// /// Checks for equality between a point and an object. /// /// The object. /// /// True if is a point that equals the current point. /// public override bool Equals(object obj) { if (obj is Point) { var other = (Point)obj; return this.X == other.X && this.Y == other.Y; } return false; } /// /// Returns a hash code for a . /// /// The hash code. public override int GetHashCode() { unchecked { int hash = 17; hash = (hash * 23) + this.x.GetHashCode(); hash = (hash * 23) + this.y.GetHashCode(); return hash; } } /// /// Returns the string representation of the point. /// /// The string representation of the point. public override string ToString() { return string.Format(CultureInfo.InvariantCulture, "{0}, {1}", this.x, this.y); } /// /// Returns a new point with the specified X coordinate. /// /// The X coordinate. /// The new point. public Point WithX(double x) { return new Point(x, this.y); } /// /// Returns a new point with the specified Y coordinate. /// /// The Y coordinate. /// The new point. public Point WithY(double y) { return new Point(this.x, y); } } }