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