// ----------------------------------------------------------------------- // // 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; } } /// /// 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); } public static Point operator +(Point a, Point b) { return new Point(a.x + b.x, a.y + b.y); } public static Point operator +(Point a, Vector b) { return new Point(a.x + b.X, a.y + b.Y); } public static Point operator -(Point a, Point b) { return new Point(a.x - b.x, a.y - b.y); } public static Point operator -(Point a, Vector b) { return new Point(a.x - b.X, a.y - b.Y); } public static Point operator *(Point point, Matrix matrix) { return new Point( (point.X * matrix.M11) + (point.Y * matrix.M21) + matrix.OffsetX, (point.X * matrix.M12) + (point.Y * matrix.M22) + matrix.OffsetY); } public static implicit operator Vector(Point p) { return new Vector(p.x, p.y); } 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; } 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); } public Point WithX(double x) { return new Point(x, this.y); } public Point WithY(double y) { return new Point(this.x, y); } } }