// // Copyright (c) James South and contributors. // Licensed under the Apache License, Version 2.0. // namespace ImageProcessor { using System; using System.ComponentModel; /// /// Stores a set of four integers that represent the location and size of a rectangle. /// /// /// 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 Rectangle : IEquatable { /// /// Represents a that has X, Y, Width, and Height values set to zero. /// public static readonly Rectangle Empty = default(Rectangle); /// /// Initializes a new instance of the struct. /// /// The horizontal position of the rectangle. /// The vertical position of the rectangle. /// The width of the rectangle. /// The height of the rectangle. public Rectangle(int x, int y, int width, int height) { this.X = x; this.Y = y; this.Width = width; this.Height = height; } /// /// Initializes a new instance of the struct. /// /// /// The which specifies the rectangles point in a two-dimensional plane. /// /// /// The which specifies the rectangles height and width. /// public Rectangle(Point point, Size size) { this.X = point.X; this.Y = point.Y; this.Width = size.Width; this.Height = size.Height; } /// /// The x-coordinate of this . /// public int X { get; set; } /// /// The y-coordinate of this . /// public int Y { get; set; } /// /// The width of this . /// public int Width { get; set; } /// /// The height of this . /// public int Height { get; set; } /// /// Gets a value indicating whether this is empty. /// [EditorBrowsable(EditorBrowsableState.Never)] public bool IsEmpty => this.X == 0 && this.Y == 0 && this.Width == 0 && this.Height == 0; /// /// Gets the y-coordinate of the top edge of this . /// public int Top => this.Y; /// /// Gets the x-coordinate of the right edge of this . /// public int Right => this.X + this.Width; /// /// Gets the y-coordinate of the bottom edge of this . /// public int Bottom => this.Y + this.Height; /// /// Gets the x-coordinate of the left edge of this . /// public int Left => this.X; /// /// Compares two objects. The result specifies whether the values /// of the , , , /// and the 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 ==(Rectangle left, Rectangle right) { return left.Equals(right); } /// /// Compares two objects. The result specifies whether the values /// of the , , , /// and the 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 !=(Rectangle left, Rectangle right) { return !left.Equals(right); } /// /// Determines if the specfied point is contained within the rectangular region defined by /// this . /// /// The x-coordinate of the given point. /// The y-coordinate of the given point. /// The public bool Contains(int x, int y) { return this.X <= x && x < this.X + this.Width && this.Y <= y && y < this.Y + this.Height; } /// /// Indicates whether this instance and a specified object are equal. /// /// /// True if and this instance are the same type and represent the same value; otherwise, false. /// /// The object to compare with the current instance. public override bool Equals(object obj) { if (!(obj is Rectangle)) { return false; } Rectangle other = (Rectangle)obj; return other.X == this.X && other.Y == this.Y && other.Width == this.Width && other.Height == this.Height; } /// /// 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 "Rectangle [ Empty ]"; } return $"Rectangle [ X={this.X}, Y={this.Y}, Width={this.Width}, Height={this.Height} ]"; } /// /// 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(Rectangle other) { return this.X.Equals(other.X) && this.Y.Equals(other.Y) && this.Width.Equals(other.Width) && this.Height.Equals(other.Height); } /// /// 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(Rectangle rectangle) { unchecked { int hashCode = rectangle.X.GetHashCode(); hashCode = (hashCode * 397) ^ rectangle.Y.GetHashCode(); hashCode = (hashCode * 397) ^ rectangle.Width.GetHashCode(); hashCode = (hashCode * 397) ^ rectangle.Height.GetHashCode(); return hashCode; } } } }