Struct Rect

GoDiagram®
v10.0.11
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).

Namespace: Northwoods.Go
Assembly: Northwoods.GoDiagram.Avalonia.dll
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
double x

The x value.

double y

The y value.

double w

The width.

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; }
Property Value
Type Description
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; }
Property Value
Type Description
Point

CenterX

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

Declaration
[JsonIgnore]
public double CenterX { get; set; }
Property Value
Type Description
double

CenterY

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

Declaration
[JsonIgnore]
public double CenterY { get; set; }
Property Value
Type Description
double

Height

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

Declaration
public double Height { get; set; }
Property Value
Type Description
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; }
Property Value
Type Description
double

Position

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

Declaration
[JsonIgnore]
public Point Position { get; set; }
Property Value
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; }
Property Value
Type Description
double

Size

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

Declaration
[JsonIgnore]
public Size Size { get; set; }
Property Value
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; }
Property Value
Type Description
double

Width

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

Declaration
public double Width { get; set; }
Property Value
Type Description
double

X

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

Declaration
public double X { readonly get; set; }
Property Value
Type Description
double

Y

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

Declaration
public double Y { readonly get; set; }
Property Value
Type Description
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
bool

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

See Also

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
bool

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

See Also

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
double rx

The X coordinate of a Rect.

double ry

The Y coordinate of a Rect.

double rw

The Width of a Rect.

double rh

The Height of a Rect.

double x

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

double y

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

double w

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

double h

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

Returns
Type Description
bool

True if the Point/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
double x

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

double y

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

double w

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

double h

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

Returns
Type Description
bool

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

See Also

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.

Returns
Type Description
Rect

A new Rect.

See Also

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
double t

the amount to move the top side downwards.

double r

the amount to move the right side leftwards.

double b

the amount to move the bottom side upwards.

double l

the amount to move the left side rightwards.

Returns
Type Description
Rect

A new Rect.

See Also

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
double w

The subtracted width on each side, left and right.

double h

The subtracted height on each side, top and bottom.

Returns
Type Description
Rect

A new Rect.

See Also

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
double x
double y
double w

the width.

double h

the height.

Returns
Type Description
bool

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

See Also

Equals(object)

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

Declaration
public override bool Equals(object obj)
Parameters
Type Name Description
object obj

The rectangle to compare to the current rectangle.

Returns
Type Description
bool

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

Overrides
See Also

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
bool

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
int

A hash code for the current rectangle structure.

Overrides

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.

Returns
Type Description
Rect

A new Rect.

See Also

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
double t

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

double r

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

double b

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

double l

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

Returns
Type Description
Rect

A new Rect.

See Also

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
double w

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

double h

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

Returns
Type Description
Rect

A new Rect.

See Also

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.

Returns
Type Description
Rect

A new Rect.

See Also

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
double x
double y
double w
double h
Returns
Type Description
Rect

A new Rect.

See Also

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
bool

true if there is an intersection.

See Also

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
double rx

The X coordinate of a Rect.

double ry

The Y coordinate of a Rect.

double rw

The Width of a Rect.

double rh

The Height of a Rect.

double x

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

double y

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

double w
double h
Returns
Type Description
bool

true if there is any overlap.

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
double x
double y
double w
double h
Returns
Type Description
bool

true if there is any overlap.

See Also

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
double x

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

double y

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

double w

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

double h

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

double p1x

The X coordinate of one end of the line segment.

double p1y

The Y coordinate of one end of the line segment.

double p2x

The X coordinate of other end of the line segment.

double p2y

The Y coordinate of other end of the line segment.

Returns
Type Description
bool

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()
Returns
Type Description
bool

IsReal()

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

Declaration
public bool IsReal()
Returns
Type Description
bool

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
double dx
double dy
Returns
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
object str
Returns
Type Description
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
string str
Returns
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
double x

the point where the spot should be.

double y

the point where the spot should be.

Spot spot

a Spot; IsSpot() must be true.

Returns
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
Returns
Type Description
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
object rect
Returns
Type Description
object

ToString()

Returns a string representation of the rectangle.

Declaration
public override string ToString()
Returns
Type Description
string

A string representation of the current rectangle.

Overrides

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.

Returns
Type Description
Rect

A new Rect.

See Also

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.

Returns
Type Description
Rect

A new Rect.

See Also

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
double x

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

double y

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

double w

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

double h

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

Returns
Type Description
Rect

A new Rect.

See Also

Operators

operator ==(Rect, Rect)

Declaration
public static bool operator ==(Rect left, Rect right)
Parameters
Type Name Description
Rect left
Rect right
Returns
Type Description
bool

operator !=(Rect, Rect)

Declaration
public static bool operator !=(Rect left, Rect right)
Parameters
Type Name Description
Rect left
Rect right
Returns
Type Description
bool