//
// System.Drawing.RectangleF.cs
//
// Author:
// Mike Kestner (mkestner@speakeasy.net)
//
// (C) 2001 Mike Kestner
//
using System;
using System.ComponentModel;
namespace System.Drawing {
[Serializable]
public struct RectangleF {
// Private position and size fields.
private PointF loc;
private SizeF sz;
///
/// Empty Shared Field
///
///
///
/// An uninitialized RectangleF Structure.
///
public static readonly RectangleF Empty;
///
/// FromLTRB Shared Method
///
///
///
/// Produces a RectangleF structure from left, top, right,
/// and bottom coordinates.
///
public static RectangleF FromLTRB (float left, float top,
float right, float bottom)
{
return new RectangleF (left, top, right - left,
bottom - top);
}
///
/// Inflate Shared Method
///
///
///
/// Produces a new RectangleF by inflating an existing
/// RectangleF by the specified coordinate values.
///
public static RectangleF Inflate (RectangleF r,
float x, float y)
{
RectangleF ir = new RectangleF (r.Location, r.Size);
ir.Inflate (x, y);
return ir;
}
///
/// Inflate Method
///
///
///
/// Inflates the RectangleF by a specified width and height.
///
public void Inflate (float width, float height)
{
Inflate (new SizeF (width, height));
}
///
/// Inflate Method
///
///
///
/// Inflates the RectangleF by a specified Size.
///
public void Inflate (SizeF sz)
{
Offset(sz.Width, sz.Height);
SizeF ds = new SizeF (sz.Width * 2, sz.Height * 2);
this.sz += ds;
}
///
/// Intersect Shared Method
///
///
///
/// Produces a new RectangleF by intersecting 2 existing
/// RectangleFs. Returns null if there is no intersection.
///
public static RectangleF Intersect (RectangleF r1,
RectangleF r2)
{
RectangleF r = new RectangleF (r1.Location, r1.Size);
r.Intersect (r2);
return r;
}
///
/// Intersect Method
///
///
///
/// Replaces the RectangleF with the intersection of itself
/// and another RectangleF.
///
public void Intersect (RectangleF r)
{
if (!IntersectsWith (r)) {
loc = PointF.Empty;
sz = SizeF.Empty;
}
X = Math.Max (Left, r.Left);
Y = Math.Max (Top, r.Top);
Width = Math.Min (Right, r.Right) - X;
Height = Math.Min (Bottom, r.Bottom) - Y;
}
///
/// Union Shared Method
///
///
///
/// Produces a new RectangleF from the union of 2 existing
/// RectangleFs.
///
public static RectangleF Union (RectangleF r1, RectangleF r2)
{
return FromLTRB (Math.Min (r1.Left, r2.Left),
Math.Min (r1.Top, r2.Top),
Math.Max (r1.Right, r2.Right),
Math.Max (r1.Bottom, r2.Bottom));
}
///
/// Equality Operator
///
///
///
/// Compares two RectangleF objects. The return value is
/// based on the equivalence of the Location and Size
/// properties of the two RectangleFs.
///
public static bool operator == (RectangleF r1, RectangleF r2)
{
return ((r1.Location == r2.Location) &&
(r1.Size == r2.Size));
}
///
/// Inequality Operator
///
///
///
/// Compares two RectangleF objects. The return value is
/// based on the equivalence of the Location and Size
/// properties of the two RectangleFs.
///
public static bool operator != (RectangleF r1, RectangleF r2)
{
return ((r1.Location != r2.Location) ||
(r1.Size != r2.Size));
}
///
/// Rectangle to RectangleF Conversion
///
///
///
/// Converts a Rectangle object to a RectangleF.
///
public static implicit operator RectangleF (Rectangle r)
{
return new RectangleF (r.Location, r.Size);
}
// -----------------------
// Public Constructors
// -----------------------
///
/// RectangleF Constructor
///
///
///
/// Creates a RectangleF from PointF and SizeF values.
///
public RectangleF (PointF loc, SizeF sz)
{
this.loc = loc;
this.sz = sz;
}
///
/// RectangleF Constructor
///
///
///
/// Creates a RectangleF from a specified x,y location and
/// width and height values.
///
public RectangleF (float x, float y, float width, float height)
{
loc = new PointF (x, y);
sz = new SizeF (width, height);
}
///
/// Bottom Property
///
///
///
/// The Y coordinate of the bottom edge of the RectangleF.
/// Read only.
///
[Browsable (false)]
public float Bottom {
get {
return Y + Height;
}
}
///
/// Height Property
///
///
///
/// The Height of the RectangleF.
///
public float Height {
get {
return sz.Height;
}
set {
sz.Height = value;
}
}
///
/// IsEmpty Property
///
///
///
/// Indicates if the width or height are zero. Read only.
///
[Browsable (false)]
public bool IsEmpty {
get {
return ((sz.Width == 0) || (sz.Height == 0));
}
}
///
/// Left Property
///
///
///
/// The X coordinate of the left edge of the RectangleF.
/// Read only.
///
[Browsable (false)]
public float Left {
get {
return X;
}
}
///
/// Location Property
///
///
///
/// The Location of the top-left corner of the RectangleF.
///
[Browsable (false)]
public PointF Location {
get {
return loc;
}
set {
loc = value;
}
}
///
/// Right Property
///
///
///
/// The X coordinate of the right edge of the RectangleF.
/// Read only.
///
[Browsable (false)]
public float Right {
get {
return X + Width;
}
}
///
/// Size Property
///
///
///
/// The Size of the RectangleF.
///
[Browsable (false)]
public SizeF Size {
get {
return sz;
}
set {
sz = value;
}
}
///
/// Top Property
///
///
///
/// The Y coordinate of the top edge of the RectangleF.
/// Read only.
///
[Browsable (false)]
public float Top {
get {
return Y;
}
}
///
/// Width Property
///
///
///
/// The Width of the RectangleF.
///
public float Width {
get {
return sz.Width;
}
set {
sz.Width = value;
}
}
///
/// X Property
///
///
///
/// The X coordinate of the RectangleF.
///
public float X {
get {
return loc.X;
}
set {
loc.X = value;
}
}
///
/// Y Property
///
///
///
/// The Y coordinate of the RectangleF.
///
public float Y {
get {
return loc.Y;
}
set {
loc.Y = value;
}
}
///
/// Contains Method
///
///
///
/// Checks if an x,y coordinate lies within this RectangleF.
///
public bool Contains (float x, float y)
{
return ((x >= Left) && (x <= Right) &&
(y >= Top) && (y <= Bottom));
}
///
/// Contains Method
///
///
///
/// Checks if a Point lies within this RectangleF.
///
public bool Contains (PointF pt)
{
return Contains (pt.X, pt.Y);
}
///
/// Contains Method
///
///
///
/// Checks if a RectangleF lies entirely within this
/// RectangleF.
///
public bool Contains (RectangleF rect)
{
return (rect == Intersect (this, rect));
}
///
/// Equals Method
///
///
///
/// Checks equivalence of this RectangleF and an object.
///
public override bool Equals (object o)
{
if (!(o is RectangleF))
return false;
return (this == (RectangleF) o);
}
///
/// GetHashCode Method
///
///
///
/// Calculates a hashing value.
///
public override int GetHashCode ()
{
return loc.GetHashCode()^sz.GetHashCode();
}
///
/// IntersectsWith Method
///
///
///
/// Checks if a RectangleF intersects with this one.
///
public bool IntersectsWith (RectangleF r)
{
return !((Left > r.Right) || (Right < r.Left) ||
(Top > r.Bottom) || (Bottom < r.Top));
}
///
/// Offset Method
///
///
///
/// Moves the RectangleF a specified distance.
///
public void Offset (float dx, float dy)
{
X += dx;
Y += dy;
}
///
/// Offset Method
///
///
///
/// Moves the RectangleF a specified distance.
///
public void Offset (PointF pt)
{
Offset(pt.X, pt.Y);
}
///
/// ToString Method
///
///
///
/// Formats the RectangleF in (x,y,w,h) notation.
///
public override string ToString ()
{
return String.Format ("[{0},{1},{2},{3}]",
X, Y, Width, Height);
}
}
}