#region --- License --- // Copyright (c) Open Toolkit library. // This file is subject to the terms and conditions defined in // file 'License.txt', which is part of this source code package. #endregion using System; using System.Runtime.InteropServices; namespace OpenTK { /// /// Defines a 2d box (rectangle). /// [StructLayout(LayoutKind.Sequential)] public struct Box2d : IEquatable { /// /// The left boundary of the structure. /// public double Left; /// /// The right boundary of the structure. /// public double Right; /// /// The top boundary of the structure. /// public double Top; /// /// The bottom boundary of the structure. /// public double Bottom; /// /// Constructs a new Box2d with the specified dimensions. /// /// An OpenTK.Vector2d describing the top-left corner of the Box2d. /// An OpenTK.Vector2d describing the bottom-right corner of the Box2d. public Box2d(Vector2d topLeft, Vector2d bottomRight) { Left = topLeft.X; Top = topLeft.Y; Right = bottomRight.X; Bottom = bottomRight.Y; } /// /// Constructs a new Box2d with the specified dimensions. /// /// The position of the left boundary. /// The position of the top boundary. /// The position of the right boundary. /// The position of the bottom boundary. public Box2d(double left, double top, double right, double bottom) { Left = left; Top = top; Right = right; Bottom = bottom; } /// /// Creates a new Box2d with the specified dimensions. /// /// The position of the top boundary. /// The position of the left boundary. /// The position of the right boundary. /// The position of the bottom boundary. /// A new OpenTK.Box2d with the specfied dimensions. public static Box2d FromTLRB(double top, double left, double right, double bottom) { return new Box2d(left, top, right, bottom); } /// /// Creates a new Box2d with the specified dimensions. /// /// The position of the top boundary. /// The position of the left boundary. /// The width of the box. /// The height of the box. /// A new OpenTK.Box2d with the specfied dimensions. public static Box2d FromDimensions(double left, double top, double width, double height) { return new Box2d(left, top, left + width, top + height); } /// /// Creates a new Box2d with the specified dimensions. /// /// The position of the top left corner. /// The size of the box. /// A new OpenTK.Box2d with the specfied dimensions. public static Box2d FromDimensions(Vector2d position, Vector2d size) { return FromDimensions(position.X, position.Y, size.X, size.Y); } /// /// Gets a double describing the width of the Box2d structure. /// public double Width { get { return (double)System.Math.Abs(Right - Left); } } /// /// Gets a double describing the height of the Box2d structure. /// public double Height { get { return (double)System.Math.Abs(Bottom - Top); } } /// /// Returns whether the box contains the specified point on the closed region described by this Box2. /// /// The point to query. /// Whether this box contains the point. public bool Contains(Vector2d point) { return Contains(point, true); } /// /// Returns whether the box contains the specified point. /// /// The point to query. /// Whether to include the box boundary in the test region. /// Whether this box contains the point. public bool Contains(Vector2d point, bool closedRegion) { bool xOK = (closedRegion == Left <= Right) ? (point.X >= Left != point.X > Right) : (point.X > Left != point.X >= Right); bool yOK = (closedRegion == Top <= Bottom) ? (point.Y >= Top != point.Y > Bottom) : (point.Y > Top != point.Y >= Bottom); return xOK && yOK; } /// /// Returns a Box2d translated by the given amount. /// public Box2d Translated(Vector2d point) { return new Box2d(Left + point.X, Top + point.Y, Right + point.X, Bottom + point.Y); } /// /// Translates this Box2d by the given amount. /// public void Translate(Vector2d point) { Left += point.X; Right += point.X; Top += point.Y; Bottom += point.Y; } /// /// Equality comparator. /// public static bool operator ==(Box2d left, Box2d right) { return left.Bottom == right.Bottom && left.Top == right.Top && left.Left == right.Left && left.Right == right.Right; } /// /// Inequality comparator. /// public static bool operator !=(Box2d left, Box2d right) { return !(left == right); } /// /// Functional equality comparator. /// public bool Equals(Box2d other) { return this == other; } /// /// Implements Object.Equals. /// public override bool Equals(object obj) { return obj is Box2d && Equals((Box2d) obj); } /// /// Gets the hash code for this Box2d. /// public override int GetHashCode() { unchecked { var hashCode = this.Left.GetHashCode(); hashCode = (hashCode * 397) ^ this.Right.GetHashCode(); hashCode = (hashCode * 397) ^ this.Top.GetHashCode(); hashCode = (hashCode * 397) ^ this.Bottom.GetHashCode(); return hashCode; } } private static string listSeparator = System.Globalization.CultureInfo.CurrentCulture.TextInfo.ListSeparator; /// /// Returns a describing the current instance. /// /// public override string ToString() { return String.Format("({0}{4} {1}) - ({2}{4} {3})", Left, Top, Right, Bottom, listSeparator); } } }