// AForge Core Library // AForge.NET framework // http://www.aforgenet.com/framework/ // // Copyright © AForge.NET, 2007-2011 // contacts@aforgenet.com // namespace CandyboxPlugin.Geometry { /// /// Structure for representing a pair of coordinates of integer type. /// /// /// The structure is used to store a pair of integer coordinates. /// /// Sample usage: /// /// // assigning coordinates in the constructor /// IntPoint p1 = new IntPoint( 10, 20 ); /// // creating a point and assigning coordinates later /// IntPoint p2; /// p2.X = 30; /// p2.Y = 40; /// // calculating distance between two points /// float distance = p1.DistanceTo( p2 ); /// /// /// [Serializable] public struct IntPoint : IComparable { /// /// X coordinate. /// /// public int X; /// /// Y coordinate. /// /// public int Y; /// /// Initializes a new instance of the structure. /// /// /// X axis coordinate. /// Y axis coordinate. /// public IntPoint(int x, int y) { this.X = x; this.Y = y; } /// /// Calculate Euclidean distance between two points. /// /// /// Point to calculate distance to. /// /// Returns Euclidean distance between this point and /// points. /// public float DistanceTo(IntPoint anotherPoint) { int dx = X - anotherPoint.X; int dy = Y - anotherPoint.Y; return (float)System.Math.Sqrt(dx * dx + dy * dy); } /// /// Calculate squared Euclidean distance between two points. /// /// /// Point to calculate distance to. /// /// Returns squared Euclidean distance between this point and /// points. /// public float SquaredDistanceTo(Point anotherPoint) { float dx = X - anotherPoint.X; float dy = Y - anotherPoint.Y; return dx * dx + dy * dy; } /// /// Addition operator - adds values of two points. /// /// /// First point for addition. /// Second point for addition. /// /// Returns new point which coordinates equal to sum of corresponding /// coordinates of specified points. /// public static IntPoint operator +(IntPoint point1, IntPoint point2) { return new IntPoint(point1.X + point2.X, point1.Y + point2.Y); } /// /// Addition operator - adds values of two points. /// /// /// First point for addition. /// Second point for addition. /// /// Returns new point which coordinates equal to sum of corresponding /// coordinates of specified points. /// public static IntPoint Add(IntPoint point1, IntPoint point2) { return new IntPoint(point1.X + point2.X, point1.Y + point2.Y); } /// /// Subtraction operator - subtracts values of two points. /// /// /// Point to subtract from. /// Point to subtract. /// /// Returns new point which coordinates equal to difference of corresponding /// coordinates of specified points. /// public static IntPoint operator -(IntPoint point1, IntPoint point2) { return new IntPoint(point1.X - point2.X, point1.Y - point2.Y); } /// /// Subtraction operator - subtracts values of two points. /// /// /// Point to subtract from. /// Point to subtract. /// /// Returns new point which coordinates equal to difference of corresponding /// coordinates of specified points. /// public static IntPoint Subtract(IntPoint point1, IntPoint point2) { return new IntPoint(point1.X - point2.X, point1.Y - point2.Y); } /// /// Addition operator - adds scalar to the specified point. /// /// /// Point to increase coordinates of. /// Value to add to coordinates of the specified point. /// /// Returns new point which coordinates equal to coordinates of /// the specified point increased by specified value. /// public static IntPoint operator +(IntPoint point, int valueToAdd) { return new IntPoint(point.X + valueToAdd, point.Y + valueToAdd); } /// /// Addition operator - adds scalar to the specified point. /// /// /// Point to increase coordinates of. /// Value to add to coordinates of the specified point. /// /// Returns new point which coordinates equal to coordinates of /// the specified point increased by specified value. /// public static IntPoint Add(IntPoint point, int valueToAdd) { return new IntPoint(point.X + valueToAdd, point.Y + valueToAdd); } /// /// Subtraction operator - subtracts scalar from the specified point. /// /// /// Point to decrease coordinates of. /// Value to subtract from coordinates of the specified point. /// /// Returns new point which coordinates equal to coordinates of /// the specified point decreased by specified value. /// public static IntPoint operator -(IntPoint point, int valueToSubtract) { return new IntPoint(point.X - valueToSubtract, point.Y - valueToSubtract); } /// /// Subtraction operator - subtracts scalar from the specified point. /// /// /// Point to decrease coordinates of. /// Value to subtract from coordinates of the specified point. /// /// Returns new point which coordinates equal to coordinates of /// the specified point decreased by specified value. /// public static IntPoint Subtract(IntPoint point, int valueToSubtract) { return new IntPoint(point.X - valueToSubtract, point.Y - valueToSubtract); } /// /// Multiplication operator - multiplies coordinates of the specified point by scalar value. /// /// /// Point to multiply coordinates of. /// Multiplication factor. /// /// Returns new point which coordinates equal to coordinates of /// the specified point multiplied by specified value. /// public static IntPoint operator *(IntPoint point, int factor) { return new IntPoint(point.X * factor, point.Y * factor); } /// /// Multiplication operator - multiplies coordinates of the specified point by scalar value. /// /// /// Point to multiply coordinates of. /// Multiplication factor. /// /// Returns new point which coordinates equal to coordinates of /// the specified point multiplied by specified value. /// public static IntPoint Multiply(IntPoint point, int factor) { return new IntPoint(point.X * factor, point.Y * factor); } /// /// Division operator - divides coordinates of the specified point by scalar value. /// /// /// Point to divide coordinates of. /// Division factor. /// /// Returns new point which coordinates equal to coordinates of /// the specified point divided by specified value. /// public static IntPoint operator /(IntPoint point, int factor) { return new IntPoint(point.X / factor, point.Y / factor); } /// /// Division operator - divides coordinates of the specified point by scalar value. /// /// /// Point to divide coordinates of. /// Division factor. /// /// Returns new point which coordinates equal to coordinates of /// the specified point divided by specified value. /// public static IntPoint Divide(IntPoint point, int factor) { return new IntPoint(point.X / factor, point.Y / factor); } /// /// Equality operator - checks if two points have equal coordinates. /// /// /// First point to check. /// Second point to check. /// /// Returns if coordinates of specified /// points are equal. /// public static bool operator ==(IntPoint point1, IntPoint point2) { return ((point1.X == point2.X) && (point1.Y == point2.Y)); } /// /// Inequality operator - checks if two points have different coordinates. /// /// /// First point to check. /// Second point to check. /// /// Returns if coordinates of specified /// points are not equal. /// public static bool operator !=(IntPoint point1, IntPoint point2) { return ((point1.X != point2.X) || (point1.Y != point2.Y)); } /// /// Check if this instance of equal to the specified one. /// /// /// Another point to check equalty to. /// /// Return if objects are equal. /// public override bool Equals(object obj) { return (obj is IntPoint) ? (this == (IntPoint)obj) : false; } /// /// Get hash code for this instance. /// /// /// Returns the hash code for this instance. /// public override int GetHashCode() { return X.GetHashCode() + Y.GetHashCode(); } /// /// Implicit conversion to . /// /// /// Integer point to convert to single precision point. /// /// Returns new single precision point which coordinates are implicitly converted /// to floats from coordinates of the specified integer point. /// public static implicit operator Point(IntPoint point) { return new Point(point.X, point.Y); } /// /// Get string representation of the class. /// /// /// Returns string, which contains values of the point in readable form. /// public override string ToString() { return string.Format(System.Globalization.CultureInfo.InvariantCulture, "{0}, {1}", X, Y); } /// /// Calculate Euclidean norm of the vector comprised of the point's /// coordinates - distance from (0, 0) in other words. /// /// /// Returns point's distance from (0, 0) point. /// public float EuclideanNorm() { return (float)System.Math.Sqrt(X * X + Y * Y); } /// /// Compares the current instance with another object of the same type and returns an integer that indicates whether the current instance precedes, follows, or occurs in the same position in the sort order as the other object. /// /// An object to compare with this instance. /// A value that indicates the relative order of the objects being compared. The return value has these meanings: Value Meaning Less than zero This instance precedes in the sort order. Zero This instance occurs in the same position in the sort order as . Greater than zero This instance follows in the sort order. public int CompareTo(IntPoint other) { int line = this.Y.CompareTo(other.Y); if (line == 0) return this.X.CompareTo(other.X); return line; } } }