RectangleF.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ Net / Net / 3.5.50727.3053 / DEVDIV / depot / DevDiv / releases / whidbey / netfxsp / ndp / fx / src / CommonUI / System / Drawing / Advanced / RectangleF.cs / 1 / RectangleF.cs

                            //------------------------------------------------------------------------------ 
// 
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// 
//----------------------------------------------------------------------------- 

namespace System.Drawing { 
 
    using System.Diagnostics;
 
    using System;
    using System.IO;
    using Microsoft.Win32;
    using System.ComponentModel; 
    using System.Drawing.Internal;
    using System.Globalization; 
 

    ///  
    /// 
    ///    
    ///       Stores the location and size of a rectangular region. For
    ///       more advanced region functions use a  
    ///       object.
    ///     
    ///  
    [Serializable]
    public struct RectangleF { 

        /// 
        /// 
        ///    Initializes a new instance of the  
        ///    class.
        ///  
        public static readonly RectangleF Empty = new RectangleF(); 

        private float x; 
        private float y;
        private float width;
        private float height;
 
        /// 
        ///  
        ///     
        ///       Initializes a new instance of the 
        ///       class with the specified location and size. 
        ///    
        /// 
        public RectangleF(float x, float y, float width, float height) {
            this.x = x; 
            this.y = y;
            this.width = width; 
            this.height = height; 
        }
 
        /// 
        /// 
        ///    
        ///       Initializes a new instance of the  
        ///       class with the specified location
        ///       and size. 
        ///     
        /// 
        public RectangleF(PointF location, SizeF size) { 
            this.x = location.X;
            this.y = location.Y;
            this.width = size.Width;
            this.height = size.Height; 
        }
 
        ///  
        /// 
        ///     
        ///       Creates a new  with
        ///       the specified location and size.
        ///    
        ///  
        // !! Not in C++ version
        public static RectangleF FromLTRB(float left, float top, float right, float bottom) { 
            return new RectangleF(left, 
                                 top,
                                 right - left, 
                                 bottom - top);
        }

        ///  
        /// 
        ///     
        ///       Gets or sets the coordinates of the upper-left corner of 
        ///       the rectangular region represented by this .
        ///     
        /// 
        [Browsable(false)]
        public PointF Location {
            get { 
                return new PointF(X, Y);
            } 
            set { 
                X = value.X;
                Y = value.Y; 
            }
        }

        ///  
        /// 
        ///     
        ///       Gets or sets the size of this . 
        ///    
        ///  
        [Browsable(false)]
        public SizeF Size {
            get {
                return new SizeF(Width, Height); 
            }
            set { 
                this.Width = value.Width; 
                this.Height = value.Height;
            } 
        }

        /// 
        ///  
        ///    
        ///       Gets or sets the x-coordinate of the 
        ///       upper-left corner of the rectangular region defined by this . 
        ///    
        ///  
        public float X {
            get {
                return x;
            } 
            set {
                x = value; 
            } 
        }
 
        /// 
        /// 
        ///    
        ///       Gets or sets the y-coordinate of the 
        ///       upper-left corner of the rectangular region defined by this .
        ///     
        ///  
        public float Y {
            get { 
                return y;
            }
            set {
                y = value; 
            }
        } 
 
        /// 
        ///  
        ///    
        ///       Gets or sets the width of the rectangular
        ///       region defined by this .
        ///     
        /// 
        public float Width { 
            get { 
                return width;
            } 
            set {
                width = value;
            }
        } 

        ///  
        ///  
        ///    
        ///       Gets or sets the height of the 
        ///       rectangular region defined by this .
        ///    
        /// 
        public float Height { 
            get {
                return height; 
            } 
            set {
                height = value; 
            }
        }

        ///  
        /// 
        ///     
        ///       Gets the x-coordinate of the upper-left corner of the 
        ///       rectangular region defined by this  .
        ///     
        /// 
        [Browsable(false)]
        public float Left {
            get { 
                return X;
            } 
        } 
        /// 
        ///  
        ///    
        ///       Gets the y-coordinate of the upper-left corner of the
        ///       rectangular region defined by this .
        ///     
        /// 
        [Browsable(false)] 
        public float Top { 
            get {
                return Y; 
            }
        }

        ///  
        /// 
        ///     
        ///       Gets the x-coordinate of the lower-right corner of the 
        ///       rectangular region defined by this .
        ///     
        /// 
        [Browsable(false)]
        public float Right {
            get { 
                return X + Width;
            } 
        } 

        ///  
        /// 
        ///    
        ///       Gets the y-coordinate of the lower-right corner of the
        ///       rectangular region defined by this . 
        ///    
        ///  
        [Browsable(false)] 
        public float Bottom {
            get { 
                return Y + Height;
            }
        }
 
        /// 
        ///  
        ///     
        ///       Tests whether this  has a  or a  of 0.
        ///     
        /// 
        [Browsable(false)]
        public bool IsEmpty {
            get { 
                return (Width <= 0 )|| (Height <= 0);
            } 
        } 

        ///  
        /// 
        ///    
        ///       Tests whether  is a  with the same location and size of this
        ///    . 
        ///    
        ///  
        public override bool Equals(object obj) { 
            if (!(obj is RectangleF))
                return false; 

            RectangleF comp = (RectangleF)obj;

            return (comp.X == this.X) && 
                   (comp.Y == this.Y) &&
                   (comp.Width == this.Width) && 
                   (comp.Height == this.Height); 
        }
 
        /// 
        /// 
        ///    
        ///       Tests whether two  
        ///       objects have equal location and size.
        ///     
        ///  
        public static bool operator ==(RectangleF left, RectangleF right) {
            return (left.X == right.X 
                     && left.Y == right.Y
                     && left.Width == right.Width
                     && left.Height == right.Height);
        } 

        ///  
        ///  
        ///    
        ///       Tests whether two  
        ///       objects differ in location or size.
        ///    
        /// 
        public static bool operator !=(RectangleF left, RectangleF right) { 
            return !(left == right);
        } 
 
        /// 
        ///  
        ///    
        ///       Determines if the specfied point is contained within the
        ///       rectangular region defined by this  .
        ///     
        /// 
        public bool Contains(float x, float y) { 
            return this.X <= x && 
            x < this.X + this.Width &&
            this.Y <= y && 
            y < this.Y + this.Height;
        }

        ///  
        /// 
        ///     
        ///       Determines if the specfied point is contained within the 
        ///       rectangular region defined by this  .
        ///     
        /// 
        public bool Contains(PointF pt) {
            return Contains(pt.X, pt.Y);
        } 

        ///  
        ///  
        ///    
        ///       Determines if the rectangular region represented by 
        ///     is entirely contained within the rectangular region represented by
        ///       this  .
        ///    
        ///  
        public bool Contains(RectangleF rect) {
            return (this.X <= rect.X) && 
                   ((rect.X + rect.Width) <= (this.X + this.Width)) && 
                   (this.Y <= rect.Y) &&
                   ((rect.Y + rect.Height) <= (this.Y + this.Height)); 
        }

        // !! Not in C++ version
        ///  
        /// 
        ///    Gets the hash code for this . 
        ///  
        public override int GetHashCode() {
            return (int)((UInt32)X ^ 
            (((UInt32)Y << 13) | ((UInt32)Y >> 19)) ^
            (((UInt32)Width << 26) | ((UInt32)Width >>  6)) ^
            (((UInt32)Height <<  7) | ((UInt32)Height >> 25)));
        } 

        ///  
        ///  
        ///    
        ///       Inflates this  
        ///       by the specified amount.
        ///    
        /// 
        public void Inflate(float x, float y) { 
            this.X -= x;
            this.Y -= y; 
            this.Width += 2*x; 
            this.Height += 2*y;
        } 

        /// 
        /// 
        ///    Inflates this  by the specified amount. 
        /// 
        public void Inflate(SizeF size) { 
            Inflate(size.Width, size.Height); 
        }
 
        /// 
        /// 
        ///    
        ///       Creates a  
        ///       that is inflated by the specified amount.
        ///     
        ///  
        // !! Not in C++
        public static RectangleF Inflate(RectangleF rect, float x, float y) { 
            RectangleF r = rect;
            r.Inflate(x, y);
            return r;
        } 

        ///  
        ///  Creates a Rectangle that represents the intersection between this Rectangle and rect. 
        /// 
        public void Intersect(RectangleF rect) 
        {
            RectangleF result = RectangleF.Intersect(rect, this);

            this.X = result.X; 
            this.Y = result.Y;
            this.Width = result.Width; 
            this.Height = result.Height; 
        }
 
        /// 
        /// 
        ///    Creates a rectangle that represents the intersetion between a and
        ///    b. If there is no intersection, null is returned. 
        /// 
        public static RectangleF Intersect(RectangleF a, RectangleF b) { 
            float x1 = Math.Max(a.X, b.X); 
            float x2 = Math.Min(a.X + a.Width, b.X + b.Width);
            float y1 = Math.Max(a.Y, b.Y); 
            float y2 = Math.Min(a.Y + a.Height, b.Y + b.Height);

            if (x2 >= x1
                && y2 >= y1) { 

                return new RectangleF(x1, y1, x2 - x1, y2 - y1); 
            } 
            return RectangleF.Empty;
        } 

        /// 
        /// 
        ///    Determines if this rectangle intersets with rect. 
        /// 
        public bool IntersectsWith(RectangleF rect) { 
            return (rect.X < this.X + this.Width) && 
                   (this.X < (rect.X + rect.Width)) &&
                   (rect.Y < this.Y + this.Height) && 
                   (this.Y < rect.Y + rect.Height);
        }

        ///  
        /// 
        ///    Creates a rectangle that represents the union between a and 
        ///    b. 
        /// 
        public static RectangleF Union(RectangleF a, RectangleF b) { 
            float x1 = Math.Min(a.X, b.X);
            float x2 = Math.Max(a.X + a.Width, b.X + b.Width);
            float y1 = Math.Min(a.Y, b.Y);
            float y2 = Math.Max(a.Y + a.Height, b.Y + b.Height); 

            return new RectangleF(x1, y1, x2 - x1, y2 - y1); 
        } 

        ///  
        /// 
        ///    Adjusts the location of this rectangle by the specified amount.
        /// 
        public void Offset(PointF pos) { 
            Offset(pos.X, pos.Y);
        } 
 
        /// 
        ///  
        ///    Adjusts the location of this rectangle by the specified amount.
        /// 
        public void Offset(float x, float y) {
            this.X += x; 
            this.Y += y;
        } 
 
        /**
         * Convert the current rectangle object into 
         * a GDI+ GPRECTF structure.
         */
        internal GPRECTF ToGPRECTF() {
            return new GPRECTF(X, Y, Width, Height); 
        }
 
        ///  
        /// 
        ///    Converts the specified  to a 
        /// .
        /// 
        public static implicit operator RectangleF(Rectangle r) {
            return new RectangleF(r.X, r.Y, r.Width, r.Height); 
        }
 
        ///  
        /// 
        ///    Converts the  and  of this  to a 
        ///    human-readable string.
        /// 
        public override string ToString() {
            return "{X=" + X.ToString(CultureInfo.CurrentCulture) + ",Y=" + Y.ToString(CultureInfo.CurrentCulture) + 
            ",Width=" + Width.ToString(CultureInfo.CurrentCulture) +
            ",Height=" + Height.ToString(CultureInfo.CurrentCulture) + "}"; 
        } 
   }
} 

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
//------------------------------------------------------------------------------ 
// 
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// 
//----------------------------------------------------------------------------- 

namespace System.Drawing { 
 
    using System.Diagnostics;
 
    using System;
    using System.IO;
    using Microsoft.Win32;
    using System.ComponentModel; 
    using System.Drawing.Internal;
    using System.Globalization; 
 

    ///  
    /// 
    ///    
    ///       Stores the location and size of a rectangular region. For
    ///       more advanced region functions use a  
    ///       object.
    ///     
    ///  
    [Serializable]
    public struct RectangleF { 

        /// 
        /// 
        ///    Initializes a new instance of the  
        ///    class.
        ///  
        public static readonly RectangleF Empty = new RectangleF(); 

        private float x; 
        private float y;
        private float width;
        private float height;
 
        /// 
        ///  
        ///     
        ///       Initializes a new instance of the 
        ///       class with the specified location and size. 
        ///    
        /// 
        public RectangleF(float x, float y, float width, float height) {
            this.x = x; 
            this.y = y;
            this.width = width; 
            this.height = height; 
        }
 
        /// 
        /// 
        ///    
        ///       Initializes a new instance of the  
        ///       class with the specified location
        ///       and size. 
        ///     
        /// 
        public RectangleF(PointF location, SizeF size) { 
            this.x = location.X;
            this.y = location.Y;
            this.width = size.Width;
            this.height = size.Height; 
        }
 
        ///  
        /// 
        ///     
        ///       Creates a new  with
        ///       the specified location and size.
        ///    
        ///  
        // !! Not in C++ version
        public static RectangleF FromLTRB(float left, float top, float right, float bottom) { 
            return new RectangleF(left, 
                                 top,
                                 right - left, 
                                 bottom - top);
        }

        ///  
        /// 
        ///     
        ///       Gets or sets the coordinates of the upper-left corner of 
        ///       the rectangular region represented by this .
        ///     
        /// 
        [Browsable(false)]
        public PointF Location {
            get { 
                return new PointF(X, Y);
            } 
            set { 
                X = value.X;
                Y = value.Y; 
            }
        }

        ///  
        /// 
        ///     
        ///       Gets or sets the size of this . 
        ///    
        ///  
        [Browsable(false)]
        public SizeF Size {
            get {
                return new SizeF(Width, Height); 
            }
            set { 
                this.Width = value.Width; 
                this.Height = value.Height;
            } 
        }

        /// 
        ///  
        ///    
        ///       Gets or sets the x-coordinate of the 
        ///       upper-left corner of the rectangular region defined by this . 
        ///    
        ///  
        public float X {
            get {
                return x;
            } 
            set {
                x = value; 
            } 
        }
 
        /// 
        /// 
        ///    
        ///       Gets or sets the y-coordinate of the 
        ///       upper-left corner of the rectangular region defined by this .
        ///     
        ///  
        public float Y {
            get { 
                return y;
            }
            set {
                y = value; 
            }
        } 
 
        /// 
        ///  
        ///    
        ///       Gets or sets the width of the rectangular
        ///       region defined by this .
        ///     
        /// 
        public float Width { 
            get { 
                return width;
            } 
            set {
                width = value;
            }
        } 

        ///  
        ///  
        ///    
        ///       Gets or sets the height of the 
        ///       rectangular region defined by this .
        ///    
        /// 
        public float Height { 
            get {
                return height; 
            } 
            set {
                height = value; 
            }
        }

        ///  
        /// 
        ///     
        ///       Gets the x-coordinate of the upper-left corner of the 
        ///       rectangular region defined by this  .
        ///     
        /// 
        [Browsable(false)]
        public float Left {
            get { 
                return X;
            } 
        } 
        /// 
        ///  
        ///    
        ///       Gets the y-coordinate of the upper-left corner of the
        ///       rectangular region defined by this .
        ///     
        /// 
        [Browsable(false)] 
        public float Top { 
            get {
                return Y; 
            }
        }

        ///  
        /// 
        ///     
        ///       Gets the x-coordinate of the lower-right corner of the 
        ///       rectangular region defined by this .
        ///     
        /// 
        [Browsable(false)]
        public float Right {
            get { 
                return X + Width;
            } 
        } 

        ///  
        /// 
        ///    
        ///       Gets the y-coordinate of the lower-right corner of the
        ///       rectangular region defined by this . 
        ///    
        ///  
        [Browsable(false)] 
        public float Bottom {
            get { 
                return Y + Height;
            }
        }
 
        /// 
        ///  
        ///     
        ///       Tests whether this  has a  or a  of 0.
        ///     
        /// 
        [Browsable(false)]
        public bool IsEmpty {
            get { 
                return (Width <= 0 )|| (Height <= 0);
            } 
        } 

        ///  
        /// 
        ///    
        ///       Tests whether  is a  with the same location and size of this
        ///    . 
        ///    
        ///  
        public override bool Equals(object obj) { 
            if (!(obj is RectangleF))
                return false; 

            RectangleF comp = (RectangleF)obj;

            return (comp.X == this.X) && 
                   (comp.Y == this.Y) &&
                   (comp.Width == this.Width) && 
                   (comp.Height == this.Height); 
        }
 
        /// 
        /// 
        ///    
        ///       Tests whether two  
        ///       objects have equal location and size.
        ///     
        ///  
        public static bool operator ==(RectangleF left, RectangleF right) {
            return (left.X == right.X 
                     && left.Y == right.Y
                     && left.Width == right.Width
                     && left.Height == right.Height);
        } 

        ///  
        ///  
        ///    
        ///       Tests whether two  
        ///       objects differ in location or size.
        ///    
        /// 
        public static bool operator !=(RectangleF left, RectangleF right) { 
            return !(left == right);
        } 
 
        /// 
        ///  
        ///    
        ///       Determines if the specfied point is contained within the
        ///       rectangular region defined by this  .
        ///     
        /// 
        public bool Contains(float x, float y) { 
            return this.X <= x && 
            x < this.X + this.Width &&
            this.Y <= y && 
            y < this.Y + this.Height;
        }

        ///  
        /// 
        ///     
        ///       Determines if the specfied point is contained within the 
        ///       rectangular region defined by this  .
        ///     
        /// 
        public bool Contains(PointF pt) {
            return Contains(pt.X, pt.Y);
        } 

        ///  
        ///  
        ///    
        ///       Determines if the rectangular region represented by 
        ///     is entirely contained within the rectangular region represented by
        ///       this  .
        ///    
        ///  
        public bool Contains(RectangleF rect) {
            return (this.X <= rect.X) && 
                   ((rect.X + rect.Width) <= (this.X + this.Width)) && 
                   (this.Y <= rect.Y) &&
                   ((rect.Y + rect.Height) <= (this.Y + this.Height)); 
        }

        // !! Not in C++ version
        ///  
        /// 
        ///    Gets the hash code for this . 
        ///  
        public override int GetHashCode() {
            return (int)((UInt32)X ^ 
            (((UInt32)Y << 13) | ((UInt32)Y >> 19)) ^
            (((UInt32)Width << 26) | ((UInt32)Width >>  6)) ^
            (((UInt32)Height <<  7) | ((UInt32)Height >> 25)));
        } 

        ///  
        ///  
        ///    
        ///       Inflates this  
        ///       by the specified amount.
        ///    
        /// 
        public void Inflate(float x, float y) { 
            this.X -= x;
            this.Y -= y; 
            this.Width += 2*x; 
            this.Height += 2*y;
        } 

        /// 
        /// 
        ///    Inflates this  by the specified amount. 
        /// 
        public void Inflate(SizeF size) { 
            Inflate(size.Width, size.Height); 
        }
 
        /// 
        /// 
        ///    
        ///       Creates a  
        ///       that is inflated by the specified amount.
        ///     
        ///  
        // !! Not in C++
        public static RectangleF Inflate(RectangleF rect, float x, float y) { 
            RectangleF r = rect;
            r.Inflate(x, y);
            return r;
        } 

        ///  
        ///  Creates a Rectangle that represents the intersection between this Rectangle and rect. 
        /// 
        public void Intersect(RectangleF rect) 
        {
            RectangleF result = RectangleF.Intersect(rect, this);

            this.X = result.X; 
            this.Y = result.Y;
            this.Width = result.Width; 
            this.Height = result.Height; 
        }
 
        /// 
        /// 
        ///    Creates a rectangle that represents the intersetion between a and
        ///    b. If there is no intersection, null is returned. 
        /// 
        public static RectangleF Intersect(RectangleF a, RectangleF b) { 
            float x1 = Math.Max(a.X, b.X); 
            float x2 = Math.Min(a.X + a.Width, b.X + b.Width);
            float y1 = Math.Max(a.Y, b.Y); 
            float y2 = Math.Min(a.Y + a.Height, b.Y + b.Height);

            if (x2 >= x1
                && y2 >= y1) { 

                return new RectangleF(x1, y1, x2 - x1, y2 - y1); 
            } 
            return RectangleF.Empty;
        } 

        /// 
        /// 
        ///    Determines if this rectangle intersets with rect. 
        /// 
        public bool IntersectsWith(RectangleF rect) { 
            return (rect.X < this.X + this.Width) && 
                   (this.X < (rect.X + rect.Width)) &&
                   (rect.Y < this.Y + this.Height) && 
                   (this.Y < rect.Y + rect.Height);
        }

        ///  
        /// 
        ///    Creates a rectangle that represents the union between a and 
        ///    b. 
        /// 
        public static RectangleF Union(RectangleF a, RectangleF b) { 
            float x1 = Math.Min(a.X, b.X);
            float x2 = Math.Max(a.X + a.Width, b.X + b.Width);
            float y1 = Math.Min(a.Y, b.Y);
            float y2 = Math.Max(a.Y + a.Height, b.Y + b.Height); 

            return new RectangleF(x1, y1, x2 - x1, y2 - y1); 
        } 

        ///  
        /// 
        ///    Adjusts the location of this rectangle by the specified amount.
        /// 
        public void Offset(PointF pos) { 
            Offset(pos.X, pos.Y);
        } 
 
        /// 
        ///  
        ///    Adjusts the location of this rectangle by the specified amount.
        /// 
        public void Offset(float x, float y) {
            this.X += x; 
            this.Y += y;
        } 
 
        /**
         * Convert the current rectangle object into 
         * a GDI+ GPRECTF structure.
         */
        internal GPRECTF ToGPRECTF() {
            return new GPRECTF(X, Y, Width, Height); 
        }
 
        ///  
        /// 
        ///    Converts the specified  to a 
        /// .
        /// 
        public static implicit operator RectangleF(Rectangle r) {
            return new RectangleF(r.X, r.Y, r.Width, r.Height); 
        }
 
        ///  
        /// 
        ///    Converts the  and  of this  to a 
        ///    human-readable string.
        /// 
        public override string ToString() {
            return "{X=" + X.ToString(CultureInfo.CurrentCulture) + ",Y=" + Y.ToString(CultureInfo.CurrentCulture) + 
            ",Width=" + Width.ToString(CultureInfo.CurrentCulture) +
            ",Height=" + Height.ToString(CultureInfo.CurrentCulture) + "}"; 
        } 
   }
} 

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
                        

Link Menu

Network programming in C#, Network Programming in VB.NET, Network Programming in .NET
This book is available now!
Buy at Amazon US or
Buy at Amazon UK