// // Copyright (c) James South and contributors. // Licensed under the Apache License, Version 2.0. // namespace ImageProcessor { using System; using System.ComponentModel; /// /// Represents an ordered pair of integer x- and y-coordinates that defines a point in /// a two-dimensional plane. /// /// /// This struct is fully mutable. This is done (against the guidelines) for the sake of performance, /// as it avoids the need to create new values for modification operations. /// public struct Point : IEquatable { /// /// Represents a that has X and Y values set to zero. /// public static readonly Point Empty = default(Point); /// /// Initializes a new instance of the struct. /// /// The horizontal position of the point. /// The vertical position of the point. public Point(int x, int y) { this.X = x; this.Y = y; } /// /// The x-coordinate of this . /// public int X { get; set; } /// /// The y-coordinate of this . /// public int Y { get; set; } /// /// Gets a value indicating whether this is empty. /// [EditorBrowsable(EditorBrowsableState.Never)] public bool IsEmpty => this.X == 0 && this.Y == 0; /// /// Compares two objects. The result specifies whether the values /// of the or properties of the two /// objects are equal. /// /// /// The on the left side of the operand. /// /// /// The on the right side of the operand. /// /// /// True if the current left is equal to the parameter; otherwise, false. /// public static bool operator ==(Point left, Point right) { return left.Equals(right); } /// /// Compares two objects. The result specifies whether the values /// of the or properties of the two /// objects are unequal. /// /// /// The on the left side of the operand. /// /// /// The on the right side of the operand. /// /// /// True if the current left is unequal to the parameter; otherwise, false. /// public static bool operator !=(Point left, Point right) { return !left.Equals(right); } /// /// Indicates whether this instance and a specified object are equal. /// /// /// The object to compare with the current instance. /// /// /// true if and this instance are the same type and represent the /// same value; otherwise, false. /// public override bool Equals(object obj) { if (!(obj is Point)) { return false; } Point other = (Point)obj; return other.X == this.X && other.Y == this.Y; } /// /// Returns the hash code for this instance. /// /// /// A 32-bit signed integer that is the hash code for this instance. /// public override int GetHashCode() { return this.GetHashCode(this); } /// /// Returns the fully qualified type name of this instance. /// /// /// A containing a fully qualified type name. /// public override string ToString() { if (this.IsEmpty) { return "Point [ Empty ]"; } return $"Point [ X={this.X}, Y={this.Y} ]"; } /// /// Indicates whether the current object is equal to another object of the same type. /// /// /// True if the current object is equal to the parameter; otherwise, false. /// /// An object to compare with this object. public bool Equals(Point other) { return this.X.Equals(other.X) && this.Y.Equals(other.Y); } /// /// Returns the hash code for this instance. /// /// /// The instance of to return the hash code for. /// /// /// A 32-bit signed integer that is the hash code for this instance. /// private int GetHashCode(Point point) { unchecked { int hashCode = point.X.GetHashCode(); hashCode = (hashCode * 397) ^ point.Y.GetHashCode(); return hashCode; } } } }