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
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
PointThe Point specifying the x-y coordinates.
sz
SizeThe 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
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
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
CenterX
Gets or sets the horizontal center X coordinate of the Rect.
[JsonIgnore]
public double CenterX { get; set; }
Property Value
CenterY
Gets or sets the vertical center Y coordinate of the Rect.
[JsonIgnore]
public double CenterY { get; set; }
Property Value
Height
Gets or sets the height of the Rect. The value must not be negative.
public double Height { get; set; }
Property Value
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
Position
Gets or sets the x- and y-axis position of the Rect as a Point.
[JsonIgnore]
public Point Position { get; set; }
Property Value
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
Size
Gets or sets the width and height of the Rect as a Size.
[JsonIgnore]
public Size Size { get; set; }
Property Value
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
Width
Gets or sets the width of the Rect. The value must not be negative.
public double Width { get; set; }
Property Value
X
Gets or sets the top-left x coordinate of the Rect.
public double X { readonly get; set; }
Property Value
Y
Gets or sets the top-left y coordinate of the Rect.
public double Y { readonly get; set; }
Property Value
Methods
Contains(Point)
Indicates whether this Rect contains the given Point.
public bool Contains(Point p)
Parameters
p
PointThe 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
RectThe 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
doubleThe X coordinate of the Point or Rect to include in the new bounds.
y
doubleThe Y coordinate of the Point or Rect to include in the new bounds.
w
doubleThe Width of the Rect to include in the new bounds, defaults to zero.
h
doubleThe 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
doubleThe X coordinate of a Rect.
ry
doubleThe Y coordinate of a Rect.
rw
doubleThe Width of a Rect.
rh
doubleThe Height of a Rect.
x
doubleThe X coordinate of the Point or Rect that might be in the bounds (RX, RY, RW, RH).
y
doubleThe Y coordinate of the Point or Rect that might be in the bounds (RX, RY, RW, RH).
w
doubleThe Width of the Rect to include in the new bounds, defaults to zero.
h
doubleThe 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
MarginThe 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
doubleThe subtracted width on each side, left and right.
h
doubleThe 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
doublethe amount to move the top side downwards.
r
doublethe amount to move the right side leftwards.
b
doublethe amount to move the bottom side upwards.
l
doublethe 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
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
objectThe 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
RectThe 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
MarginThe 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
doubleThe additional width on each side, left and right; may be negative.
h
doubleThe 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
doublethe amount to move the top side upwards; may be negative.
r
doublethe amount to move the right side rightwards; may be negative.
b
doublethe amount to move the bottom side downwards; may be negative.
l
doublethe 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
RectRect 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
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
RectRect 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
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
doubleThe X coordinate of a Rect.
ry
doubleThe Y coordinate of a Rect.
rw
doubleThe Width of a Rect.
rh
doubleThe Height of a Rect.
x
doubleThe X coordinate of the Point or Rect that might overlap the bounds (RX, RY, RW, RH).
y
doubleThe Y coordinate of the Point or Rect that might overlap the bounds (RX, RY, RW, RH).
w
doubleh
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
doubleThe X coordinate of the rectangle to check for intersection with the line segment.
y
doubleThe Y coordinate of the rectangle to check for intersection with the line segment.
w
doubleThe Width of the rectangle to check for intersection with the line segment.
h
doubleThe Height of the rectangle to check for intersection with the line segment.
p1x
doubleThe X coordinate of one end of the line segment.
p1y
doubleThe Y coordinate of one end of the line segment.
p2x
doubleThe X coordinate of other end of the line segment.
p2y
doubleThe 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
IsReal()
True if this Rect has X, Y, Width, and Height values that are real numbers and not infinity.
public bool IsReal()
Returns
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
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
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
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
doublethe point where the spot should be.
y
doublethe point where the spot should be.
spot
Spota 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
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
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
Returns
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
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
PointThe 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
RectThe 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
doubleThe X coordinate of the Rect to include in the new bounds.
y
doubleThe Y coordinate of the Rect to include in the new bounds.
w
doubleThe Width of the Rect to include in the new bounds, defaults to zero.
h
doubleThe 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
Returns
operator !=(Rect, Rect)
public static bool operator !=(Rect left, Rect right)