Struct RectF
A rectangle in float pixels, y down: the left edge at X, the top at Y. A width or height of 0 or less makes it empty. Pixel (0, 0) covers the square from (0, 0) to (1, 1), so a rectangle with whole-number edges covers whole pixels.
public readonly record struct RectF : IEquatable<RectF>
- Implements
- Inherited Members
Examples
canvas.FillRect(new RectF(10, 10, 80, 40), Rgba8.Red);
canvas.FillRoundRect(new RoundRect(new RectF(100, 10, 80, 40), 12), new Rgba8(0, 128, 0, 255));
// Each corner can have its own radii; radii that would overlap are scaled down together, as CSS does.
CornerRadii tab = new(topLeft: 16, topRight: 16, bottomRight: 0, bottomLeft: 0);
canvas.FillRoundRect(new RoundRect(new RectF(190, 10, 100, 40), tab), Rgba8.Black);
canvas.FillOval(new RectF(10, 60, 120, 60), new Rgba8(255, 200, 0, 255));
canvas.FillCircle(new Vector2(170, 90), 30, new Rgba8(80, 80, 200, 255));
canvas.FillArc(new RectF(210, 60, 60, 60), startDegrees: -90, sweepDegrees: 120, Rgba8.Red);
canvas.FillRing(new RoundRect(new RectF(10, 130, 60, 60), 30), new RoundRect(new RectF(25, 145, 30, 30), 15), Rgba8.Black);
// The same shapes as paths, to build on or to reuse.
Path hexagon = Path.RegularPolygon(new Vector2(120, 160), 30, sides: 6);
Path star = Path.Star(new Vector2(200, 160), outerRadius: 30, innerRadius: 12, points: 5);
canvas.FillPath(hexagon, new Rgba8(0, 150, 150, 255));
canvas.FillPath(star, new Rgba8(220, 160, 0, 255));
Constructors
RectF(float, float, float, float)
A rectangle in float pixels, y down: the left edge at X, the top at Y. A width or height of 0 or less makes it empty. Pixel (0, 0) covers the square from (0, 0) to (1, 1), so a rectangle with whole-number edges covers whole pixels.
public RectF(float X, float Y, float Width, float Height)
Parameters
XfloatThe left edge.
YfloatThe top edge.
WidthfloatThe width; empty when 0 or less.
HeightfloatThe height; empty when 0 or less.
Examples
canvas.FillRect(new RectF(10, 10, 80, 40), Rgba8.Red);
canvas.FillRoundRect(new RoundRect(new RectF(100, 10, 80, 40), 12), new Rgba8(0, 128, 0, 255));
// Each corner can have its own radii; radii that would overlap are scaled down together, as CSS does.
CornerRadii tab = new(topLeft: 16, topRight: 16, bottomRight: 0, bottomLeft: 0);
canvas.FillRoundRect(new RoundRect(new RectF(190, 10, 100, 40), tab), Rgba8.Black);
canvas.FillOval(new RectF(10, 60, 120, 60), new Rgba8(255, 200, 0, 255));
canvas.FillCircle(new Vector2(170, 90), 30, new Rgba8(80, 80, 200, 255));
canvas.FillArc(new RectF(210, 60, 60, 60), startDegrees: -90, sweepDegrees: 120, Rgba8.Red);
canvas.FillRing(new RoundRect(new RectF(10, 130, 60, 60), 30), new RoundRect(new RectF(25, 145, 30, 30), 15), Rgba8.Black);
// The same shapes as paths, to build on or to reuse.
Path hexagon = Path.RegularPolygon(new Vector2(120, 160), 30, sides: 6);
Path star = Path.Star(new Vector2(200, 160), outerRadius: 30, innerRadius: 12, points: 5);
canvas.FillPath(hexagon, new Rgba8(0, 150, 150, 255));
canvas.FillPath(star, new Rgba8(220, 160, 0, 255));
Properties
Bottom
public float Bottom { get; }
Property Value
Center
Gets the center.
public Vector2 Center { get; }
Property Value
Empty
Gets the empty rectangle at the origin.
public static RectF Empty { get; }
Property Value
Height
The height; empty when 0 or less.
public float Height { get; init; }
Property Value
IsEmpty
Gets a value indicating whether the rectangle covers no area: its width or height is 0 or less, or not a number.
public bool IsEmpty { get; }
Property Value
IsFinite
Gets a value indicating whether every coordinate is a finite number.
public bool IsFinite { get; }
Property Value
Left
Gets the left edge, X.
public float Left { get; }
Property Value
Position
Gets the top-left corner.
public Vector2 Position { get; }
Property Value
Right
public float Right { get; }
Property Value
Size
Gets the width and height.
public Vector2 Size { get; }
Property Value
Top
Gets the top edge, Y.
public float Top { get; }
Property Value
Width
The width; empty when 0 or less.
public float Width { get; init; }
Property Value
X
The left edge.
public float X { get; init; }
Property Value
Y
The top edge.
public float Y { get; init; }
Property Value
Methods
Contains(RectF)
Returns whether another rectangle lies wholly inside this one. An empty rectangle is inside none.
public bool Contains(RectF other)
Parameters
otherRectFThe other rectangle.
Returns
Contains(Vector2)
Returns whether a point lies inside: left and top edges included, right and bottom excluded.
public bool Contains(Vector2 point)
Parameters
pointVector2The point.
Returns
FromEdges(float, float, float, float)
Creates a rectangle from its four edges.
public static RectF FromEdges(float left, float top, float right, float bottom)
Parameters
leftfloatThe left edge.
topfloatThe top edge.
rightfloatThe right edge.
bottomfloatThe bottom edge.
Returns
- RectF
The rectangle; empty when
rightis not pastleftorbottomnot belowtop.
FromPoints(ReadOnlySpan<Vector2>)
Creates the smallest rectangle holding every point.
public static RectF FromPoints(ReadOnlySpan<Vector2> points)
Parameters
pointsReadOnlySpan<Vector2>The points.
Returns
FromPosition(Vector2, Vector2)
Creates a rectangle from a corner and a size.
public static RectF FromPosition(Vector2 position, Vector2 size)
Parameters
Returns
- RectF
The rectangle.
Inflate(float, float)
Returns the rectangle grown by dx on the left and right and dy on the top and bottom; negative values shrink it.
public RectF Inflate(float dx, float dy)
Parameters
Returns
- RectF
The grown rectangle.
Intersect(RectF)
Returns the area both rectangles cover.
public RectF Intersect(RectF other)
Parameters
otherRectFThe other rectangle.
Returns
IntersectsWith(RectF)
Returns whether the two rectangles share any area.
public bool IntersectsWith(RectF other)
Parameters
otherRectFThe other rectangle.
Returns
Offset(Vector2)
Returns the rectangle moved by an offset.
public RectF Offset(Vector2 offset)
Parameters
offsetVector2The distance to move.
Returns
- RectF
The moved rectangle.
RoundOut()
Returns the smallest integer rectangle holding this one: edges rounded outwards.
public Rect RoundOut()
Returns
- Rect
The covering pixel rectangle; empty when this one is empty.
Union(RectF)
Returns the smallest rectangle holding both. An empty rectangle adds nothing.
public RectF Union(RectF other)
Parameters
otherRectFThe other rectangle.
Returns
- RectF
The union.
Operators
implicit operator RectF(Rect)
Converts an integer rectangle.
public static implicit operator RectF(Rect rect)
Parameters
rectRectThe rectangle in whole pixels.