# Struct Rect

GoDiagram
v10.0.4
by Northwoods Software®

A Rect describes a rectangular two-dimensional area as a top-left point (x and y values) and a size (width and height values).

##### Syntax
``public struct Rect``
##### Remarks

Use the static functions Parse(String) and Stringify(Rect) to convert to and from a standard string representation that is independent of the current locale.

### Constructors

#### Rect(Point, Point)

Initializes a Rect that is exactly large enough to contain the given Points.

##### Declaration
``public Rect(Point a, Point b)``
##### Parameters
Type Name Description
Point a

The first Point to be contained.

Point b

The second Point to be contained.

#### Rect(Point, Size)

Initializes a Rect that is located and sized by the given Point and Rect.

##### Declaration
``public Rect(Point pt, Size sz)``
##### Parameters
Type Name Description
Point pt

The Point specifying the x-y coordinates.

Size sz

The Size specifying the width and height.

#### Rect(Double, Double, Double, Double)

Initializes a Rect with the given x, y, width, and height values. The default constructor produces the Rect(0,0,0,0).

##### Declaration
``public Rect(double x = 0, double y = 0, double w = 0, double h = 0)``
##### Parameters
Type Name Description
System.Double x

The x value.

System.Double y

The y value.

System.Double w

The width.

System.Double h

The height.

### Properties

#### Bottom

Gets or sets the y-axis value of the bottom of the Rect. This is equal to the sum of the y value and the height.

##### Declaration
``````[JsonIgnore]
public double Bottom { get; set; }``````
Type Description
System.Double

#### Center

Gets or sets the Point at the center of this Rect. Setting this property just shifts the X and Y values.

##### Declaration
``````[JsonIgnore]
public Point Center { get; set; }``````
Type Description
Point

#### CenterX

Gets or sets the horizontal center X coordinate of the Rect.

##### Declaration
``````[JsonIgnore]
public double CenterX { get; set; }``````
Type Description
System.Double

#### CenterY

Gets or sets the vertical center Y coordinate of the Rect.

##### Declaration
``````[JsonIgnore]
public double CenterY { get; set; }``````
Type Description
System.Double

#### Height

Gets or sets the height of the Rect. The value must not be negative.

##### Declaration
``public double Height { get; set; }``
Type Description
System.Double

#### Left

Gets or sets the leftmost value of the Rect. This is the same as the X value.

##### Declaration
``````[JsonIgnore]
public double Left { get; set; }``````
Type Description
System.Double

#### Position

Gets or sets the x- and y-axis position of the Rect as a Point.

##### Declaration
``````[JsonIgnore]
public Point Position { get; set; }``````
Type Description
Point

#### Right

Gets or sets the x-axis value of the right of the Rect. This is equal to the sum of the x value and the width.

##### Declaration
``````[JsonIgnore]
public double Right { get; set; }``````
Type Description
System.Double

#### Size

Gets or sets the width and height of the Rect as a Size.

##### Declaration
``````[JsonIgnore]
public Size Size { get; set; }``````
Type Description
Size

#### Top

Gets or sets the topmost value of the Rect. This is the same as the Y value.

##### Declaration
``````[JsonIgnore]
public double Top { get; set; }``````
Type Description
System.Double

#### Width

Gets or sets the width of the Rect. The value must not be negative.

##### Declaration
``public double Width { get; set; }``
Type Description
System.Double

#### X

Gets or sets the top-left x coordinate of the Rect.

##### Declaration
``public double X { readonly get; set; }``
Type Description
System.Double

#### Y

Gets or sets the top-left y coordinate of the Rect.

##### Declaration
``public double Y { readonly get; set; }``
Type Description
System.Double

### Methods

#### Contains(Point)

Indicates whether this Rect contains the given Point.

##### Declaration
``public bool Contains(Point p)``
##### Parameters
Type Name Description
Point p

The Point to check.

##### Returns
Type Description
System.Boolean

True if the Point is contained within this Rect, false otherwise.

#### Contains(Rect)

Indicates whether this Rect contains the given Rect.

##### Declaration
``public bool Contains(Rect r)``
##### Parameters
Type Name Description
Rect r

The Rect to check.

##### Returns
Type Description
System.Boolean

True if the Rect is contained within this Rect, false otherwise.

#### Contains(Double, Double, Double, Double)

Indicates whether this Rect contains the given Point/Rect.

##### Declaration
``public bool Contains(double x, double y, double w = 0, double h = 0)``
##### Parameters
Type Name Description
System.Double x

The X coordinate of the Point or Rect to include in the new bounds.

System.Double y

The Y coordinate of the Point or Rect to include in the new bounds.

System.Double w

The Width of the Rect to include in the new bounds, defaults to zero.

System.Double h

The Height of the Rect to include in the new bounds, defaults to zero.

##### Returns
Type Description
System.Boolean

True if the Point/Rect is contained within this Rect, false otherwise.

#### Contains(Double, Double, Double, Double, Double, Double, Double, Double)

This static function indicates whether a Rect contains the given Point/Rect.

##### Declaration
``public static bool Contains(double rx, double ry, double rw, double rh, double x, double y, double w = 0, double h = 0)``
##### Parameters
Type Name Description
System.Double rx

The X coordinate of a Rect.

System.Double ry

The Y coordinate of a Rect.

System.Double rw

The Width of a Rect.

System.Double rh

The Height of a Rect.

System.Double x

The X coordinate of the Point or Rect that might be in the bounds (RX, RY, RW, RH).

System.Double y

The Y coordinate of the Point or Rect that might be in the bounds (RX, RY, RW, RH).

System.Double w

The Width of the Rect to include in the new bounds, defaults to zero.

System.Double h

The Height of the Rect to include in the new bounds, defaults to zero.

##### Returns
Type Description
System.Boolean

True if the Point/Rect is contained within this Rect, false otherwise.

#### Deflate(Margin)

Returns a new Rect that is this Rect with the given Margin subtracted from each side.

##### Declaration
``public Rect Deflate(Margin m)``
##### Parameters
Type Name Description
Margin m

The Margin to subtract from the Rect.

Type Description
Rect

A new Rect.

#### Deflate(Double, Double)

Returns a new Rect that is this Rect with its width and height changed on all four sides, equally on the left and right sides, and equally on the top and bottom sides. This will not deflate the Rect beyond zero. This method is the same as calling Inflate(Double, Double) with negative parameters.

##### Declaration
``public Rect Deflate(double w, double h)``
##### Parameters
Type Name Description
System.Double w

The subtracted width on each side, left and right.

System.Double h

The subtracted height on each side, top and bottom.

Type Description
Rect

A new Rect.

#### Deflate(Double, Double, Double, Double)

Returns a new Rect that is this Rect with some distance subtracted from each side. This will not deflate the Rect beyond zero. This method is the same as calling Inflate(Double, Double, Double, Double) with negative parameters.

##### Declaration
``public Rect Deflate(double t, double r, double b, double l)``
##### Parameters
Type Name Description
System.Double t

the amount to move the top side downwards.

System.Double r

the amount to move the right side leftwards.

System.Double b

the amount to move the bottom side upwards.

System.Double l

the amount to move the left side rightwards.

Type Description
Rect

A new Rect.

#### Equals(Double, Double, Double, Double)

Indicates whether the given Rect is equal to the current Rect.

##### Declaration
``public bool Equals(double x, double y, double w, double h)``
##### Parameters
Type Name Description
System.Double x
System.Double y
System.Double w

the width.

System.Double h

the height.

##### Returns
Type Description
System.Boolean

True if the Rects are equivalent in x, y, width, and height.

#### Equals(Object)

Indicates whether the given Rect is equal to the current Rect.

##### Declaration
``public override bool Equals(object obj)``
##### Parameters
Type Name Description
System.Object obj

The rectangle to compare to the current rectangle.

##### Returns
Type Description
System.Boolean

True if the Rects are equivalent in x, y, width, and height.

##### Overrides
System.ValueType.Equals(System.Object)

#### EqualsApprox(Rect)

Indicates whether the given Rect is nearly equal to this Rect.

##### Declaration
``public bool EqualsApprox(Rect r)``
##### Parameters
Type Name Description
Rect r

The Rect to compare to the current Rect.

##### Returns
Type Description
System.Boolean

True if the two Rects have respective X, Y, Width, and Height values within 0.5, false otherwise.

#### GetHashCode()

Creates a hash code for the rectangle.

##### Declaration
``public override int GetHashCode()``
##### Returns
Type Description
System.Int32

A hash code for the current rectangle structure.

##### Overrides
System.ValueType.GetHashCode()

#### Inflate(Margin)

Returns a new Rect that is this Rect with the given Margin added to each side.

##### Declaration
``public Rect Inflate(Margin m)``
##### Parameters
Type Name Description
Margin m

The Margin to add to the Rect.

Type Description
Rect

A new Rect.

#### Inflate(Double, Double)

Returns a new Rect that is this Rect with its width and height changed on all four sides, equally on the left and right sides, and equally on the top and bottom sides. When the arguments are negative, this operation deflates this Rect, but not beyond zero.

##### Declaration
``public Rect Inflate(double w, double h)``
##### Parameters
Type Name Description
System.Double w

The additional width on each side, left and right; may be negative.

System.Double h

The additional height on each side, top and bottom; may be negative.

Type Description
Rect

A new Rect.

#### Inflate(Double, Double, Double, Double)

Returns a new Rect that is this Rect with some distance added to each side.

##### Declaration
``public Rect Inflate(double t, double r, double b, double l)``
##### Parameters
Type Name Description
System.Double t

the amount to move the top side upwards; may be negative.

System.Double r

the amount to move the right side rightwards; may be negative.

System.Double b

the amount to move the bottom side downwards; may be negative.

System.Double l

the amount to move the left side leftwards; may be negative.

Type Description
Rect

A new Rect.

#### Intersect(Rect)

Returns a new Rect that is the intersection of this Rect and the given Rect.

##### Declaration
``public Rect Intersect(Rect r)``
##### Parameters
Type Name Description
Rect r

Rect to intersect with.

Type Description
Rect

A new Rect.

#### Intersect(Double, Double, Double, Double)

Returns a new Rect that is the intersection of this Rect and the rectangle defined by x, y, w, h.

##### Declaration
``public Rect Intersect(double x, double y, double w, double h)``
##### Parameters
Type Name Description
System.Double x
System.Double y
System.Double w
System.Double h
Type Description
Rect

A new Rect.

#### Intersects(Rect)

Determine if a given Rect is partly or wholly inside of this Rect.

##### Declaration
``public bool Intersects(Rect r)``
##### Parameters
Type Name Description
Rect r

Rect to test intersection with.

##### Returns
Type Description
System.Boolean

true if there is an intersection.

#### Intersects(Double, Double, Double, Double)

Determine if this Rect partly or wholly overlaps the rectangle defined by x, y, w, h.

##### Declaration
``public bool Intersects(double x, double y, double w, double h)``
##### Parameters
Type Name Description
System.Double x
System.Double y
System.Double w
System.Double h
##### Returns
Type Description
System.Boolean

true if there is any overlap.

#### Intersects(Double, Double, Double, Double, Double, Double, Double, Double)

This static function indicates whether a Rect partly or wholly overlaps the given Rect.

##### Declaration
``public static bool Intersects(double rx, double ry, double rw, double rh, double x, double y, double w, double h)``
##### Parameters
Type Name Description
System.Double rx

The X coordinate of a Rect.

System.Double ry

The Y coordinate of a Rect.

System.Double rw

The Width of a Rect.

System.Double rh

The Height of a Rect.

System.Double x

The X coordinate of the Point or Rect that might overlap the bounds (RX, RY, RW, RH).

System.Double y

The Y coordinate of the Point or Rect that might overlap the bounds (RX, RY, RW, RH).

System.Double w
System.Double h
##### Returns
Type Description
System.Boolean

true if there is any overlap.

#### IntersectsLineSegment(Double, Double, Double, Double, Double, Double, Double, Double)

This static function is true if a rectangular area is intersected by a finite straight line segment.

##### Declaration
``public static bool IntersectsLineSegment(double x, double y, double w, double h, double p1x, double p1y, double p2x, double p2y)``
##### Parameters
Type Name Description
System.Double x

The X coordinate of the rectangle to check for intersection with the line segment.

System.Double y

The Y coordinate of the rectangle to check for intersection with the line segment.

System.Double w

The Width of the rectangle to check for intersection with the line segment.

System.Double h

The Height of the rectangle to check for intersection with the line segment.

System.Double p1x

The X coordinate of one end of the line segment.

System.Double p1y

The Y coordinate of one end of the line segment.

System.Double p2x

The X coordinate of other end of the line segment.

System.Double p2y

The Y coordinate of other end of the line segment.

##### Returns
Type Description
System.Boolean

True if the given finite line segment intersects with the given rectangular area, false otherwise.

#### IsEmpty()

True if this Rect has a Width and Height of zero.

##### Declaration
``public bool IsEmpty()``
Type Description
System.Boolean

#### IsReal()

True if this Rect has X, Y, Width, and Height values that are real numbers and not infinity.

##### Declaration
``public bool IsReal()``
Type Description
System.Boolean

#### Offset(Double, Double)

Returns a new Rect by shifting this Rect's x and y values with the given DX and DY offsets.

##### Declaration
``public Rect Offset(double dx, double dy)``
##### Parameters
Type Name Description
System.Double dx
System.Double dy
Type Description
Rect

A new Rect.

#### Parse(Object)

This is a convenience method for Parse(String) that can be used as a Converter.

##### Declaration
``public static object Parse(object str)``
##### Parameters
Type Name Description
System.Object str
Type Description
System.Object

#### Parse(String)

This static method can be used to read in a Rect from a string that was produced by Stringify(Rect).

##### Declaration
``public static Rect Parse(string str)``
##### Parameters
Type Name Description
System.String str
Type Description
Rect
##### Remarks

`Rect.Parse("1 2 3 4")` produces the Rect `new Rect(1, 2, 3, 4)`.

#### SetSpot(Double, Double, Spot)

Returns a new Rect so that a given Spot in this Rect is at a given (x, y) point. The returned Rect's size will be the same as this Rect.

##### Declaration
``public Rect SetSpot(double x, double y, Spot spot)``
##### Parameters
Type Name Description
System.Double x

the point where the spot should be.

System.Double y

the point where the spot should be.

Spot spot

a Spot; IsSpot() must be true.

Type Description
Rect

A new Rect.

##### Remarks

The result is meaningless if IsNoSpot() is true.

#### Stringify(Rect)

This static method can be used to write out a Rect as a string that can be read by Parse(String).

##### Declaration
``public static string Stringify(Rect val)``
##### Parameters
Type Name Description
Rect val
Type Description
System.String
##### Remarks

`Rect.Stringify(new Rect(1, 2, 3, 4))` produces the string "1 2 3 4".

#### Stringify(Object)

This is a convenience method for Stringify(Rect) that can be used as a BackConverter.

##### Declaration
``public static object Stringify(object rect)``
##### Parameters
Type Name Description
System.Object rect
Type Description
System.Object

#### ToString()

Returns a string representation of the rectangle.

##### Declaration
``public override string ToString()``
##### Returns
Type Description
System.String

A string representation of the current rectangle.

##### Overrides
System.ValueType.ToString()

#### Union(Point)

Returns a new Rect that is exactly big enough to contain this Rect and the given Point.

##### Declaration
``public Rect Union(Point p)``
##### Parameters
Type Name Description
Point p

The Point to include in the new bounds.

Type Description
Rect

A new Rect.

#### Union(Rect)

Returns a new Rect that is exactly big enough to contain this Rect and the given Rect.

##### Declaration
``public Rect Union(Rect r)``
##### Parameters
Type Name Description
Rect r

The Rect to include in the new bounds.

Type Description
Rect

A new Rect.

#### Union(Double, Double, Double, Double)

Returns a new Rect that is exactly big enough to contain this Rect and the given rectangular area.

##### Declaration
``public Rect Union(double x, double y, double w = 0, double h = 0)``
##### Parameters
Type Name Description
System.Double x

The X coordinate of the Rect to include in the new bounds.

System.Double y

The Y coordinate of the Rect to include in the new bounds.

System.Double w

The Width of the Rect to include in the new bounds, defaults to zero.

System.Double h

The Height of the Rect to include in the new bounds, defaults to zero.

Type Description
Rect

A new Rect.

### Operators

#### Equality(Rect, Rect)

##### Declaration
``public static bool operator ==(Rect left, Rect right)``
##### Parameters
Type Name Description
Rect left
Rect right
Type Description
System.Boolean

#### Inequality(Rect, Rect)

##### Declaration
``public static bool operator !=(Rect left, Rect right)``
##### Parameters
Type Name Description
Rect left
Rect right
Type Description
System.Boolean