#region License // // The Open Toolkit Library License // // Copyright (c) 2006 - 2009 the Open Toolkit library. // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights to // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of // the Software, and to permit persons to whom the Software is furnished to do // so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in all // copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR // OTHER DEALINGS IN THE SOFTWARE. // #endregion using System; using System.Collections.Generic; using System.Text; namespace OpenTK { /// /// Represents a rectangular region on a two-dimensional plane. /// public struct Rectangle : IEquatable { #region Fields Point location; Size size; #endregion #region Constructors /// /// Constructs a new Rectangle instance. /// /// The top-left corner of the Rectangle. /// The width and height of the Rectangle. public Rectangle(Point location, Size size) : this() { Location = location; Size = size; } /// /// Constructs a new Rectangle instance. /// /// The x coordinate of the Rectangle. /// The y coordinate of the Rectangle. /// The width coordinate of the Rectangle. /// The height coordinate of the Rectangle. public Rectangle(int x, int y, int width, int height) : this(new Point(x, y), new Size(width, height)) { } #endregion #region Public Members /// /// Gets or sets the x coordinate of the Rectangle. /// public int X { get { return Location.X; } set { Location = new Point (value, Y); } } /// /// Gets or sets the y coordinate of the Rectangle. /// public int Y { get { return Location.Y; } set { Location = new Point (X, value); } } /// /// Gets or sets the width of the Rectangle. /// public int Width { get { return Size.Width; } set { Size = new Size (value, Height); } } /// /// Gets or sets the height of the Rectangle. /// public int Height { get { return Size.Height; } set { Size = new Size(Width, value); } } /// /// Gets or sets a representing the x and y coordinates /// of the Rectangle. /// public Point Location { get { return location; } set { location = value; } } /// /// Gets or sets a representing the width and height /// of the Rectangle. /// public Size Size { get { return size; } set { size = value; } } /// /// Gets the y coordinate of the top edge of this Rectangle. /// public int Top { get { return Y; } } /// /// Gets the x coordinate of the right edge of this Rectangle. /// public int Right { get { return X + Width; } } /// /// Gets the y coordinate of the bottom edge of this Rectangle. /// public int Bottom { get { return Y + Height; } } /// /// Gets the x coordinate of the left edge of this Rectangle. /// public int Left { get { return X; } } /// /// Gets a that indicates whether this /// Rectangle is equal to the empty Rectangle. /// public bool IsEmpty { get { return Location.IsEmpty && Size.IsEmpty; } } /// /// Defines the empty Rectangle. /// public static readonly Rectangle Zero = new Rectangle(); /// /// Defines the empty Rectangle. /// public static readonly Rectangle Empty = new Rectangle(); /// /// Constructs a new instance with the specified edges. /// /// The left edge of the Rectangle. /// The top edge of the Rectangle. /// The right edge of the Rectangle. /// The bottom edge of the Rectangle. /// A new Rectangle instance with the specified edges. public static Rectangle FromLTRB(int left, int top, int right, int bottom) { return new Rectangle(new Point(left, top), new Size(right - left, bottom - top)); } /// /// Tests whether this instance contains the specified Point. /// /// The to test. /// True if this instance contains point; false otherwise. /// The left and top edges are inclusive. The right and bottom edges /// are exclusive. public bool Contains(Point point) { return point.X >= Left && point.X < Right && point.Y >= Top && point.Y < Bottom; } /// /// Tests whether this instance contains the specified Rectangle. /// /// The to test. /// True if this instance contains rect; false otherwise. /// The left and top edges are inclusive. The right and bottom edges /// are exclusive. public bool Contains(Rectangle rect) { return Contains(rect.Location) && Contains(rect.Location + rect.Size); } /// /// Compares two instances for equality. /// /// The first instance. /// The second instance. /// True, if left is equal to right; false otherwise. public static bool operator ==(Rectangle left, Rectangle right) { return left.Equals(right); } /// /// Compares two instances for inequality. /// /// The first instance. /// The second instance. /// True, if left is not equal to right; false otherwise. public static bool operator !=(Rectangle left, Rectangle right) { return !left.Equals(right); } /// /// Converts an OpenTK.Rectangle instance to a System.Drawing.Rectangle. /// /// /// The instance to convert. /// /// /// A instance equivalent to rect. /// public static implicit operator System.Drawing.Rectangle(Rectangle rect) { return new System.Drawing.Rectangle(rect.Location, rect.Size); } /// /// Converts a System.Drawing.Rectangle instance to an OpenTK.Rectangle. /// /// /// The instance to convert. /// /// /// A instance equivalent to point. /// public static implicit operator Rectangle(System.Drawing.Rectangle rect) { return new Rectangle(rect.Location, rect.Size); } /// /// Converts an OpenTK.Rectangle instance to a System.Drawing.RectangleF. /// /// /// The instance to convert. /// /// /// A instance equivalent to rect. /// public static implicit operator System.Drawing.RectangleF(Rectangle rect) { return new System.Drawing.RectangleF(rect.Location, rect.Size); } /// /// Indicates whether this instance is equal to the specified object. /// /// The object instance to compare to. /// True, if both instances are equal; false otherwise. public override bool Equals(object obj) { if (obj is Rectangle) return Equals((Rectangle)obj); return false; } /// /// Returns the hash code for this instance. /// /// A that represents the hash code for this instance./> public override int GetHashCode() { return Location.GetHashCode() & Size.GetHashCode(); } /// /// Returns a that describes this instance. /// /// A that describes this instance. public override string ToString() { return String.Format("{{{0}, {1}}}-{{{2},{3}}}", Location, Size); } #endregion #region IEquatable Members /// /// Indicates whether this instance is equal to the specified Rectangle. /// /// The instance to compare to. /// True, if both instances are equal; false otherwise. public bool Equals(Rectangle other) { return Location.Equals(other.Location) && Size.Equals(other.Size); } #endregion } }