Table of Contents

Struct Rect

Namespace
Northwoods.Go
Assembly
Northwoods.GoDiagram.WinForms.dll

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

public struct Rect
Inherited Members

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.

public Rect(Point a, Point b)

Parameters

a Point

The first Point to be contained.

b Point

The second Point to be contained.

Rect(Point, Size)

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

public Rect(Point pt, Size sz)

Parameters

pt Point

The Point specifying the x-y coordinates.

sz Size

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).

public Rect(double x = 0, double y = 0, double w = 0, double h = 0)

Parameters

x double

The x value.

y double

The y value.

w double

The width.

h double

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.

[JsonIgnore]
public double Bottom { get; set; }

Property Value

double

Center

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

[JsonIgnore]
public Point Center { get; set; }

Property Value

Point

CenterX

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

[JsonIgnore]
public double CenterX { get; set; }

Property Value

double

CenterY

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

[JsonIgnore]
public double CenterY { get; set; }

Property Value

double

Height

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

public double Height { get; set; }

Property Value

double

Left

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

[JsonIgnore]
public double Left { get; set; }

Property Value

double

Position

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

[JsonIgnore]
public Point Position { get; set; }

Property Value

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.

[JsonIgnore]
public double Right { get; set; }

Property Value

double

Size

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

[JsonIgnore]
public Size Size { get; set; }

Property Value

Size

Top

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

[JsonIgnore]
public double Top { get; set; }

Property Value

double

Width

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

public double Width { get; set; }

Property Value

double

X

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

public double X { readonly get; set; }

Property Value

double

Y

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

public double Y { readonly get; set; }

Property Value

double

Methods

Contains(Point)

Indicates whether this Rect contains the given Point.

public bool Contains(Point p)

Parameters

p Point

The Point to check.

Returns

bool

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

See Also

Contains(Rect)

Indicates whether this Rect contains the given Rect.

public bool Contains(Rect r)

Parameters

r Rect

The Rect to check.

Returns

bool

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

See Also

Contains(double, double, double, double)

Indicates whether this Rect contains the given Point/Rect.

public bool Contains(double x, double y, double w = 0, double h = 0)

Parameters

x double

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

y double

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

w double

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

h double

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

Returns

bool

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

public static bool Contains(double rx, double ry, double rw, double rh, double x, double y, double w = 0, double h = 0)

Parameters

rx double

The X coordinate of a Rect.

ry double

The Y coordinate of a Rect.

rw double

The Width of a Rect.

rh double

The Height of a Rect.

x double

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

y double

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

w double

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

h double

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

Returns

bool

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.

public Rect Deflate(Margin m)

Parameters

m Margin

The Margin to subtract from the Rect.

Returns

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.

public Rect Deflate(double w, double h)

Parameters

w double

The subtracted width on each side, left and right.

h double

The subtracted height on each side, top and bottom.

Returns

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.

public Rect Deflate(double t, double r, double b, double l)

Parameters

t double

the amount to move the top side downwards.

r double

the amount to move the right side leftwards.

b double

the amount to move the bottom side upwards.

l double

the amount to move the left side rightwards.

Returns

Rect

A new Rect.

See Also

Equals(double, double, double, double)

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

public bool Equals(double x, double y, double w, double h)

Parameters

x double
y double
w double

the width.

h double

the height.

Returns

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.

public override bool Equals(object obj)

Parameters

obj object

The rectangle to compare to the current rectangle.

Returns

bool

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

See Also

EqualsApprox(Rect)

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

public bool EqualsApprox(Rect r)

Parameters

r Rect

The Rect to compare to the current Rect.

Returns

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.

public override int GetHashCode()

Returns

int

A hash code for the current rectangle structure.

Inflate(Margin)

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

public Rect Inflate(Margin m)

Parameters

m Margin

The Margin to add to the Rect.

Returns

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.

public Rect Inflate(double w, double h)

Parameters

w double

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

h double

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

Returns

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.

public Rect Inflate(double t, double r, double b, double l)

Parameters

t double

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

r double

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

b double

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

l double

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

Returns

Rect

A new Rect.

See Also

Intersect(Rect)

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

public Rect Intersect(Rect r)

Parameters

r Rect

Rect to intersect with.

Returns

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.

public Rect Intersect(double x, double y, double w, double h)

Parameters

x double
y double
w double
h double

Returns

Rect

A new Rect.

See Also

Intersects(Rect)

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

public bool Intersects(Rect r)

Parameters

r Rect

Rect to test intersection with.

Returns

bool

true if there is an intersection.

See Also

Intersects(double, double, double, double)

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

public bool Intersects(double x, double y, double w, double h)

Parameters

x double
y double
w double
h double

Returns

bool

true if there is any overlap.

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.

public static bool Intersects(double rx, double ry, double rw, double rh, double x, double y, double w, double h)

Parameters

rx double

The X coordinate of a Rect.

ry double

The Y coordinate of a Rect.

rw double

The Width of a Rect.

rh double

The Height of a Rect.

x double

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

y double

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

w double
h double

Returns

bool

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.

public static bool IntersectsLineSegment(double x, double y, double w, double h, double p1x, double p1y, double p2x, double p2y)

Parameters

x double

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

y double

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

w double

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

h double

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

p1x double

The X coordinate of one end of the line segment.

p1y double

The Y coordinate of one end of the line segment.

p2x double

The X coordinate of other end of the line segment.

p2y double

The Y coordinate of other end of the line segment.

Returns

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.

public bool IsEmpty()

Returns

bool

IsReal()

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

public bool IsReal()

Returns

bool

Offset(double, double)

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

public Rect Offset(double dx, double dy)

Parameters

dx double
dy double

Returns

Rect

A new Rect.

Parse(object)

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

public static object Parse(object str)

Parameters

str object

Returns

object

Parse(string)

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

public static Rect Parse(string str)

Parameters

str string

Returns

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.

public Rect SetSpot(double x, double y, Spot spot)

Parameters

x double

the point where the spot should be.

y double

the point where the spot should be.

spot Spot

a Spot; IsSpot() must be true.

Returns

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).

public static string Stringify(Rect val)

Parameters

val Rect

Returns

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.

public static object Stringify(object rect)

Parameters

rect object

Returns

object

StringifyFixed(Rect, int)

This static method can be used to write out a Rect as a string with its numbers at a fixed number of digits after the decimal point.

public static string StringifyFixed(Rect val, int digits)

Parameters

val Rect

the Rect

digits int

the number of decimals to output

Returns

string

Remarks

This is useful for limiting the size of JSON output and making it more legible. It might also be useful for regression testing.

StringifyFixed(object)

This is a convenience method for StringifyFixed(Rect, int) that can be used as a BackConverter. This convenience method uses two decimal places.

public static object StringifyFixed(object rect)

Parameters

rect object

Returns

object

ToString()

Returns a string representation of the rectangle.

public override string ToString()

Returns

string

A string representation of the current rectangle.

Union(Point)

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

public Rect Union(Point p)

Parameters

p Point

The Point to include in the new bounds.

Returns

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.

public Rect Union(Rect r)

Parameters

r Rect

The Rect to include in the new bounds.

Returns

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.

public Rect Union(double x, double y, double w = 0, double h = 0)

Parameters

x double

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

y double

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

w double

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

h double

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

Returns

Rect

A new Rect.

See Also

Operators

operator ==(Rect, Rect)

public static bool operator ==(Rect left, Rect right)

Parameters

left Rect
right Rect

Returns

bool

operator !=(Rect, Rect)

public static bool operator !=(Rect left, Rect right)

Parameters

left Rect
right Rect

Returns

bool