// Copyright (c) 2010-2014 SharpDX - Alexandre Mutel
//
// 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.
using System;
using System.Globalization;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using SharpDX.Mathematics.Interop;
namespace SharpDX
{
///
/// Define a RectangleF. This structure is slightly different from System.Drawing.RectangleF as it is
/// internally storing Left,Top,Right,Bottom instead of Left,Top,Width,Height.
///
[StructLayout(LayoutKind.Sequential)]
public struct RectangleF : IEquatable
{
///
/// The left.
///
public float Left;
///
/// The top.
///
public float Top;
///
/// The right.
///
public float Right;
///
/// The bottom.
///
public float Bottom;
///
/// An empty rectangle.
///
public static readonly RectangleF Empty;
///
/// An infinite rectangle. See remarks.
///
///
/// http://msdn.microsoft.com/en-us/library/windows/desktop/dd372261%28v=vs.85%29.aspx
/// Any properties that involve computations, like , or
/// may return incorrect results - .
///
public static readonly RectangleF Infinite;
static RectangleF()
{
Empty = new RectangleF();
Infinite = new RectangleF
{
Left = float.NegativeInfinity,
Top = float.NegativeInfinity,
Right = float.PositiveInfinity,
Bottom = float.PositiveInfinity
};
}
///
/// Initializes a new instance of the struct.
///
/// The left.
/// The top.
/// The width.
/// The height.
public RectangleF(float x, float y, float width, float height)
{
Left = x;
Top = y;
Right = x + width;
Bottom = y + height;
}
///
/// Gets or sets the X position.
///
/// The X position.
public float X
{
get
{
return Left;
}
set
{
Right = value + Width;
Left = value;
}
}
///
/// Gets or sets the Y position.
///
/// The Y position.
public float Y
{
get
{
return Top;
}
set
{
Bottom = value + Height;
Top = value;
}
}
///
/// Gets or sets the width.
///
/// The width.
public float Width
{
get { return Right - Left; }
set { Right = Left + value; }
}
///
/// Gets or sets the height.
///
/// The height.
public float Height
{
get { return Bottom - Top; }
set { Bottom = Top + value; }
}
///
/// Gets or sets the location.
///
///
/// The location.
///
public Vector2 Location
{
get
{
return new Vector2(X, Y);
}
set
{
X = value.X;
Y = value.Y;
}
}
///
/// Gets the Point that specifies the center of the rectangle.
///
///
/// The center.
///
public Vector2 Center
{
get
{
return new Vector2(X + (Width / 2), Y + (Height / 2));
}
}
///
/// Gets a value that indicates whether the rectangle is empty.
///
///
/// true if [is empty]; otherwise, false.
///
public bool IsEmpty
{
get
{
return (Width == 0.0f) && (Height == 0.0f) && (X == 0.0f) && (Y == 0.0f);
}
}
///
/// Gets or sets the size of the rectangle.
///
/// The size of the rectangle.
public Size2F Size
{
get
{
return new Size2F(Width, Height);
}
set
{
Width = value.Width;
Height = value.Height;
}
}
///
/// Gets the position of the top-left corner of the rectangle.
///
/// The top-left corner of the rectangle.
public Vector2 TopLeft { get { return new Vector2(Left, Top); } }
///
/// Gets the position of the top-right corner of the rectangle.
///
/// The top-right corner of the rectangle.
public Vector2 TopRight { get { return new Vector2(Right, Top); } }
///
/// Gets the position of the bottom-left corner of the rectangle.
///
/// The bottom-left corner of the rectangle.
public Vector2 BottomLeft { get { return new Vector2(Left, Bottom); } }
///
/// Gets the position of the bottom-right corner of the rectangle.
///
/// The bottom-right corner of the rectangle.
public Vector2 BottomRight { get { return new Vector2(Right, Bottom); } }
/// Changes the position of the rectangle.
/// The values to adjust the position of the rectangle by.
public void Offset(Point amount)
{
Offset(amount.X, amount.Y);
}
/// Changes the position of the rectangle.
/// The values to adjust the position of the rectangle by.
public void Offset(Vector2 amount)
{
Offset(amount.X, amount.Y);
}
/// Changes the position of the rectangle.
/// Change in the x-position.
/// Change in the y-position.
public void Offset(float offsetX, float offsetY)
{
X += offsetX;
Y += offsetY;
}
/// Pushes the edges of the rectangle out by the horizontal and vertical values specified.
/// Value to push the sides out by.
/// Value to push the top and bottom out by.
public void Inflate(float horizontalAmount, float verticalAmount)
{
X -= horizontalAmount;
Y -= verticalAmount;
Width += horizontalAmount * 2;
Height += verticalAmount * 2;
}
/// Determines whether this rectangle contains a specified Point.
/// The Point to evaluate.
/// [OutAttribute] true if the specified Point is contained within this rectangle; false otherwise.
public void Contains(ref Vector2 value, out bool result)
{
result = (value.X >= Left && value.X <= Right && value.Y >= Top && value.Y <= Bottom);
}
/// Determines whether this rectangle entirely contains a specified rectangle.
/// The rectangle to evaluate.
public bool Contains(Rectangle value)
{
return (X <= value.X) && (value.Right <= Right) && (Y <= value.Y) && (value.Bottom <= Bottom);
}
/// Determines whether this rectangle entirely contains a specified rectangle.
/// The rectangle to evaluate.
/// [OutAttribute] On exit, is true if this rectangle entirely contains the specified rectangle, or false if not.
public void Contains(ref RectangleF value, out bool result)
{
result = (X <= value.X) && (value.Right <= Right) && (Y <= value.Y) && (value.Bottom <= Bottom);
}
///
/// Checks, if specified point is inside .
///
/// X point coordinate.
/// Y point coordinate.
/// true if point is inside , otherwise false.
public bool Contains(float x, float y)
{
return (x >= Left && x <= Right && y >= Top && y <= Bottom);
}
///
/// Checks, if specified is inside .
///
/// Coordinate .
/// true if is inside , otherwise false.
public bool Contains(Vector2 vector2D)
{
return Contains(vector2D.X, vector2D.Y);
}
///
/// Checks, if specified is inside .
///
/// Coordinate .
/// true if is inside , otherwise false.
public bool Contains(Point point)
{
return Contains(point.X, point.Y);
}
/// Determines whether a specified rectangle intersects with this rectangle.
/// The rectangle to evaluate.
public bool Intersects(RectangleF value)
{
bool result;
Intersects(ref value, out result);
return result;
}
///
/// Determines whether a specified rectangle intersects with this rectangle.
///
/// The rectangle to evaluate
/// [OutAttribute] true if the specified rectangle intersects with this one; false otherwise.
public void Intersects(ref RectangleF value, out bool result)
{
result = (value.X < Right) && (X < value.Right) && (value.Y < Bottom) && (Y < value.Bottom);
}
///
/// Creates a rectangle defining the area where one rectangle overlaps with another rectangle.
///
/// The first Rectangle to compare.
/// The second Rectangle to compare.
/// The intersection rectangle.
public static RectangleF Intersect(RectangleF value1, RectangleF value2)
{
RectangleF result;
Intersect(ref value1, ref value2, out result);
return result;
}
/// Creates a rectangle defining the area where one rectangle overlaps with another rectangle.
/// The first rectangle to compare.
/// The second rectangle to compare.
/// [OutAttribute] The area where the two first parameters overlap.
public static void Intersect(ref RectangleF value1, ref RectangleF value2, out RectangleF result)
{
float newLeft = (value1.X > value2.X) ? value1.X : value2.X;
float newTop = (value1.Y > value2.Y) ? value1.Y : value2.Y;
float newRight = (value1.Right < value2.Right) ? value1.Right : value2.Right;
float newBottom = (value1.Bottom < value2.Bottom) ? value1.Bottom : value2.Bottom;
if ((newRight > newLeft) && (newBottom > newTop))
{
result = new RectangleF(newLeft, newTop, newRight - newLeft, newBottom - newTop);
}
else
{
result = Empty;
}
}
///
/// Creates a new rectangle that exactly contains two other rectangles.
///
/// The first rectangle to contain.
/// The second rectangle to contain.
/// The union rectangle.
public static RectangleF Union(RectangleF value1, RectangleF value2)
{
RectangleF result;
Union(ref value1, ref value2, out result);
return result;
}
///
/// Creates a new rectangle that exactly contains two other rectangles.
///
/// The first rectangle to contain.
/// The second rectangle to contain.
/// [OutAttribute] The rectangle that must be the union of the first two rectangles.
public static void Union(ref RectangleF value1, ref RectangleF value2, out RectangleF result)
{
var left = Math.Min(value1.Left, value2.Left);
var right = Math.Max(value1.Right, value2.Right);
var top = Math.Min(value1.Top, value2.Top);
var bottom = Math.Max(value1.Bottom, value2.Bottom);
result = new RectangleF(left, top, right - left, bottom - top);
}
///
/// Determines whether the specified is equal to this instance.
///
/// The to compare with this instance.
///
/// true if the specified is equal to this instance; otherwise, false.
///
public override bool Equals(object obj)
{
if(!(obj is RectangleF))
return false;
var strongValue = (RectangleF)obj;
return Equals(ref strongValue);
}
///
/// Determines whether the specified is equal to this instance.
///
/// The to compare with this instance.
///
/// true if the specified is equal to this instance; otherwise, false.
///
public bool Equals(ref RectangleF other)
{
return MathUtil.NearEqual(other.Left, Left) &&
MathUtil.NearEqual(other.Right, Right) &&
MathUtil.NearEqual(other.Top, Top) &&
MathUtil.NearEqual(other.Bottom, Bottom);
}
///
/// Determines whether the specified is equal to this instance.
///
/// The to compare with this instance.
///
/// true if the specified is equal to this instance; otherwise, false.
///
[MethodImpl((MethodImplOptions)0x100)] // MethodImplOptions.AggressiveInlining
public bool Equals(RectangleF other)
{
return Equals(ref other);
}
///
/// Returns a hash code for this instance.
///
///
/// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.
///
public override int GetHashCode()
{
unchecked
{
int result = Left.GetHashCode();
result = (result * 397) ^ Top.GetHashCode();
result = (result * 397) ^ Right.GetHashCode();
result = (result * 397) ^ Bottom.GetHashCode();
return result;
}
}
public override string ToString()
{
return string.Format(CultureInfo.InvariantCulture, "X:{0} Y:{1} Width:{2} Height:{3}", X, Y, Width, Height);
}
///
/// Implements the operator ==.
///
/// The left.
/// The right.
/// The result of the operator.
[MethodImpl((MethodImplOptions)0x100)] // MethodImplOptions.AggressiveInlining
public static bool operator ==(RectangleF left, RectangleF right)
{
return left.Equals(ref right);
}
///
/// Implements the operator !=.
///
/// The left.
/// The right.
/// The result of the operator.
[MethodImpl((MethodImplOptions)0x100)] // MethodImplOptions.AggressiveInlining
public static bool operator !=(RectangleF left, RectangleF right)
{
return !left.Equals(ref right);
}
///
/// Performs an explicit conversion to structure.
///
/// Performs direct float to int conversion, any fractional data is truncated.
/// The source value.
/// A converted structure.
public static explicit operator Rectangle(RectangleF value)
{
return new Rectangle((int)value.X, (int)value.Y, (int)value.Width, (int)value.Height);
}
///
/// Performs an explicit conversion to structure.
///
/// Performs direct float to int conversion, any fractional data is truncated.
/// The source value.
/// A converted structure.
public static implicit operator RawRectangle(RectangleF value)
{
return new RawRectangle((int)value.X, (int)value.Y, (int)value.Right, (int)value.Bottom);
}
///
/// Performs an explicit conversion to structure.
///
/// Performs direct float to int conversion, any fractional data is truncated.
/// The source value.
/// A converted structure.
public static implicit operator RawRectangleF(RectangleF value)
{
return new RawRectangleF(value.X, value.Y, value.Right, value.Bottom);
}
}
}