| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779 |
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Runtime.Serialization;
- using Microsoft.Xna.Framework;
- namespace MonoGame.Extended;
- /// <summary>
- /// An axis-aligned rectangular volume defined by position, width, and height.
- /// </summary>
- [DataContract]
- [DebuggerDisplay("{DebugDisplayString,nq}")]
- public struct RectangleF : IEquatable<RectangleF>
- {
- #region Fields
- private static readonly RectangleF s_empty = new RectangleF(0.0f, 0.0f, 0.0f, 0.0f);
- /// <summary>
- /// The x-coordinate of the rectangle's top-left corner.
- /// </summary>
- [DataMember]
- public float X;
- /// <summary>
- /// The y-coordinate of the rectangle's top-left corner.
- /// </summary>
- [DataMember]
- public float Y;
- /// <summary>
- /// The horizontal extent of the rectangle.
- /// </summary>
- [DataMember]
- public float Width;
- /// <summary>
- /// The vertical extent of the rectangle.
- /// </summary>
- [DataMember]
- public float Height;
- #endregion
- #region Properties
- /// <summary>
- /// A rectangle with all components set to zero.
- /// </summary>
- public static RectangleF Empty => s_empty;
- /// <summary>
- /// Whether this rectangle has zero position, width, and height.
- /// </summary>
- public readonly bool IsEmpty => Width.Equals(0) && Height.Equals(0) && X.Equals(0) && Y.Equals(0);
- /// <summary>
- /// The horizontal position of the rectangle's left edge.
- /// </summary>
- public readonly float Left => X;
- /// <summary>
- /// The horizontal position of the rectangle's right edge.
- /// </summary>
- public readonly float Right => X + Width;
- /// <summary>
- /// The vertical position of the rectangle's top edge.
- /// </summary>
- public readonly float Top => Y;
- /// <summary>
- /// The vertical position of the rectangle's bottom edge.
- /// </summary>
- public readonly float Bottom => Y + Height;
- /// <summary>
- /// The position of the rectangle's top-left corner.
- /// </summary>
- public Vector2 Location
- {
- readonly get
- {
- return new Vector2(X, Y);
- }
- set
- {
- X = value.X;
- Y = value.Y;
- }
- }
- /// <summary>
- /// The dimensions of the rectangle.
- /// </summary>
- public SizeF Size
- {
- readonly get
- {
- return new SizeF(Width, Height);
- }
- set
- {
- Width = value.Width;
- Height = value.Height;
- }
- }
- /// <summary>
- /// The position of the rectangle's geometric center.
- /// </summary>
- public readonly Vector2 Center => new Vector2(X + Width * 0.5f, Y + Height * 0.5f);
- /// <summary>
- /// The position of the rectangle's top-left corner.
- /// </summary>
- public readonly Vector2 TopLeft => new Vector2(X, Y);
- /// <summary>
- /// The position of the rectangle's top-right corner.
- /// </summary>
- public readonly Vector2 TopRight => new Vector2(X + Width, Y);
- /// <summary>
- /// The position of the rectangle's bottom-left corner.
- /// </summary>
- public readonly Vector2 BottomLeft => new Vector2(X, Y + Height);
- /// <summary>
- /// The position of the rectangle's bottom-right corner.
- /// </summary>
- public readonly Vector2 BottomRight => new Vector2(X + Width, Y + Height);
- #endregion
- #region Constructors
- /// <summary>
- /// Initializes a rectangle from position and dimensions.
- /// </summary>
- /// <param name="x">The x-coordinate of the rectangle's top-left corner.</param>
- /// <param name="y">The y-coordinate of the rectangle's top-left corner.</param>
- /// <param name="width">The horizontal extent of the rectangle.</param>
- /// <param name="height">The vertical extent of the rectangle.</param>
- public RectangleF(float x, float y, float width, float height)
- {
- X = x;
- Y = y;
- Width = width;
- Height = height;
- }
- /// <summary>
- /// Initializes a rectangle from position and size components.
- /// </summary>
- /// <param name="position">The position of the rectangle's top-left corner.</param>
- /// <param name="size">The dimensions of the rectangle.</param>
- public RectangleF(Vector2 position, SizeF size)
- {
- X = position.X;
- Y = position.Y;
- Width = size.Width;
- Height = size.Height;
- }
- #endregion
- #region Create From Methods
- /// <summary>
- /// Creates a rectangle from two corner points.
- /// </summary>
- /// <param name="minimum">The minimum corner coordinates (typically top-left).</param>
- /// <param name="maximum">The maximum corner coordinates (typically bottom-right).</param>
- /// <returns>A rectangle spanning from the minimum to maximum points.</returns>
- public static RectangleF CreateFrom(Vector2 minimum, Vector2 maximum)
- {
- return new RectangleF(
- minimum.X,
- minimum.Y,
- maximum.X - minimum.X,
- maximum.Y - minimum.Y
- );
- }
- /// <summary>
- /// Creates the smallest rectangle that contains all specified points.
- /// </summary>
- /// <param name="points">The collection of points to enclose.</param>
- /// <returns>
- /// The axis-aligned bounding rectangle containing all points, or <see cref="Empty"/> if no points are provided.
- /// </returns>
- /// <remarks>
- /// Computes the axis-aligned bounding box by finding the minimum and maximum x and y coordinates
- /// among all provided points. The resulting rectangle will have the smallest area that contains
- /// all input points.
- /// </remarks>
- public static RectangleF CreateFrom(IReadOnlyList<Vector2> points)
- {
- if (points == null || points.Count == 0)
- {
- return Empty;
- }
- float minX = points[0].X;
- float minY = points[0].Y;
- float maxX = minX;
- float maxY = minY;
- for (int i = 1; i < points.Count; i++)
- {
- Vector2 point = points[i];
- if (point.X < minX) minX = point.X;
- if (point.Y < minY) minY = point.Y;
- if (point.X > maxX) maxX = point.X;
- if (point.Y > maxY) maxY = point.Y;
- }
- return new RectangleF(minX, minY, maxX - minX, maxY - minY);
- }
- #endregion
- #region Union Methods
- /// <summary>
- /// Expands this rectangle to contain the specified rectangle.
- /// </summary>
- /// <param name="rectangle">The rectangle to include.</param>
- public void Union(RectangleF rectangle)
- {
- X = Math.Min(X, rectangle.X);
- Y = Math.Min(Y, rectangle.Y);
- Width = Math.Max(Right, rectangle.Right) - X;
- Height = Math.Max(Bottom, rectangle.Bottom) - Y;
- }
- /// <summary>
- /// Computes the <see cref="RectangleF" /> that contains the two specified
- /// <see cref="RectangleF" /> structures.
- /// </summary>
- /// <param name="first">The first rectangle.</param>
- /// <param name="second">The second rectangle.</param>
- /// <returns>
- /// An <see cref="RectangleF" /> that contains both the <paramref name="first" /> and the
- /// <paramref name="second" />.
- /// </returns>
- public static RectangleF Union(ref RectangleF first, ref RectangleF second)
- {
- float x = Math.Min(first.X, second.X);
- float y = Math.Min(first.Y, second.Y);
- float width = Math.Max(first.Right, second.Right) - x;
- float height = Math.Max(first.Bottom, second.Bottom) - y;
- return new RectangleF(x, y, width, height);
- }
- #endregion
- #region Normalize Methods
- /// <summary>
- /// Normalizes this <see cref="RectangleF"/> so that the <see cref="Width"/> and <see cref="Height"/> are
- /// positive without changing the location of the rectangle.
- /// </summary>
- public void Normalize()
- {
- if (Width < 0)
- {
- X += Width;
- Width = -Width;
- }
- if (Height < 0)
- {
- Y += Height;
- Height = -Height;
- }
- }
- /// <summary>
- /// Normalizes the specified <see cref="RectangleF"/> so that the <see cref="Width"/> and <see cref="Height"/>
- /// are positive without changing the location of the rectangle.
- /// </summary>
- /// <param name="rectangle">The <see cref="RectangleF"/> to normalize.</param>
- /// <returns>A <see cref="RectangleF"/> with positive width and height.</returns>
- public static RectangleF Normalize(RectangleF rectangle)
- {
- if (rectangle.Width < 0)
- {
- rectangle.X += rectangle.Width;
- rectangle.Width = -rectangle.Width;
- }
- if (rectangle.Height < 0)
- {
- rectangle.Y += rectangle.Height;
- rectangle.Height = -rectangle.Height;
- }
- return rectangle;
- }
- #endregion
- #region Containment Methods
- /// <summary>
- /// Determines whether this <see cref="RectangleF" /> contains the specified
- /// <see cref="Vector2" />.
- /// </summary>
- /// <param name="point">The point.</param>
- /// <returns>
- /// <c>true</c> if the this <see cref="RectangleF"/> contains the <paramref name="point" />; otherwise,
- /// <c>false</c>.
- /// </returns>
- public bool Contains(Vector2 point)
- {
- return Left <= point.X
- && point.X < Right
- && Top <= point.Y
- && point.Y < Bottom;
- }
- /// <summary>
- /// Determines whether the specified <see cref="RectangleF" /> contains the specified
- /// <see cref="Vector2" />.
- /// </summary>
- /// <param name="rectangle">The rectangle.</param>
- /// <param name="point">The point.</param>
- /// <returns>
- /// <c>true</c> if the <paramref name="rectangle" /> contains the <paramref name="point" />; otherwise,
- /// <c>false</c>.
- /// </returns>
- public static bool Contains(RectangleF rectangle, Vector2 point)
- {
- return rectangle.Left <= point.X
- && point.X < rectangle.Right
- && rectangle.Top <= point.Y
- && point.Y < rectangle.Bottom;
- }
- #endregion
- #region Intersection Methods
- /// <summary>
- /// Modifies this rectangle to represent the intersection with the specified rectangle.
- /// </summary>
- /// <param name="rectangle">The rectangle to intersect with.</param>
- public void Intersect(RectangleF rectangle)
- {
- float left = Math.Max(Left, rectangle.Left);
- float top = Math.Max(Top, rectangle.Top);
- float right = Math.Min(Right, rectangle.Right);
- float bottom = Math.Min(Bottom, rectangle.Bottom);
- if (right < left || bottom < top)
- {
- this = Empty;
- return;
- }
- X = left;
- Y = top;
- Width = right - left;
- Height = bottom - top;
- }
- /// <summary>
- /// Computes the <see cref="RectangleF"/> that represents the intersection of two <see cref="RectangleF"/>
- /// structures.
- /// </summary>
- /// <param name="value1">The first rectangle to intersect.</param>
- /// <param name="value2">The second rectangle to intersect.</param>
- /// <returns>
- /// A <see cref="RectangleF"/> that represents the intersection of <paramref name="value1"/> and
- /// <paramref name="value2"/>, if there is an intersection; otherwise, <see cref="RectangleF.Empty"/>.
- /// </returns>
- public static RectangleF Intersect(RectangleF value1, RectangleF value2)
- {
- float left = Math.Max(value1.Left, value2.Left);
- float top = Math.Max(value1.Top, value2.Top);
- float right = Math.Min(value1.Right, value2.Right);
- float bottom = Math.Min(value1.Bottom, value2.Bottom);
- if (right < left || bottom < top)
- {
- return Empty;
- }
- return new RectangleF(left, top, right - left, bottom - top);
- }
- /// <summary>
- /// Determines whether this rectangle intersects with another rectangle.
- /// </summary>
- /// <param name="other">The other rectangle to test for intersection.</param>
- /// <returns>
- /// <see langword="true"/> if this rectangle intersects the other; otherwise, <see langword="false"/>.
- /// </returns>
- public readonly bool Intersects(RectangleF other) => IntersectionTests.RectangleFRectangleF(in this, in other);
- /// <summary>
- /// Determines whether this rectangle intersects with a circle.
- /// </summary>
- /// <param name="circle">The circle to test for intersection.</param>
- /// <returns>
- /// <see langword="true"/> if this rectangle intersects the circle; otherwise, <see langword="false"/>.
- /// </returns>
- public readonly bool Intersects(Circle circle) => IntersectionTests.CircleRectangleF(in circle, in this);
- /// <summary>
- /// Determines whether this rectangle intersects with an ellipse.
- /// </summary>
- /// <param name="ellipse">The ellipse to test for intersection.</param>
- /// <returns>
- /// <see langword="true"/> if this rectangle intersects the ellipse; otherwise, <see langword="false"/>.
- /// </returns>
- public readonly bool Intersects(Ellipse ellipse) => IntersectionTests.EllipseRectangleF(in ellipse, in this);
- /// <summary>
- /// Determines whether this rectangle intersects with a ray.
- /// </summary>
- /// <param name="ray">The ray to test for intersection.</param>
- /// <returns>
- /// <see langword="true"/> if this rectangle intersects the ray; otherwise, <see langword="false"/>.
- /// </returns>
- public readonly bool Intersects(Ray ray) => IntersectionTests.RectangleFRay(in this, in ray);
- /// <summary>
- /// Determines whether this rectangle intersects with a line segment.
- /// </summary>
- /// <param name="lineSegment">The line segment to test for intersection.</param>
- /// <returns>
- /// <see langword="true"/> if this rectangle intersects the line segment; otherwise, <see langword="false"/>.
- /// </returns>
- public readonly bool Intersects(LineSegment lineSegment) => IntersectionTests.RectangleFLineSegment(in this, in lineSegment);
- #endregion
- #region Closest Point To Methods
- /// <summary>
- /// Computes the closest <see cref="Vector2" /> on this <see cref="RectangleF" /> to a specified
- /// <see cref="Vector2" />.
- /// </summary>
- /// <param name="point">The point.</param>
- /// <returns>The closest <see cref="Vector2" /> on this <see cref="RectangleF" /> to the <paramref name="point" />.</returns>
- public readonly Vector2 ClosestPointTo(Vector2 point)
- {
- Vector2 result = point;
- if (result.X < Left)
- {
- result.X = Left;
- }
- else if (result.X > Right)
- {
- result.X = Right;
- }
- if (result.Y < Top)
- {
- result.Y = Top;
- }
- else if (result.Y > Bottom)
- {
- result.Y = Bottom;
- }
- return result;
- }
- #endregion
- #region Distance Methods
- /// <summary>
- /// Calculates the distance from this rectangle to the specified point.
- /// </summary>
- /// <param name="point">The point to measure distance to.</param>
- /// <returns>The distance to the point, or zero if the point is inside the rectangle.</returns>
- public readonly float DistanceTo(Vector2 point)
- {
- return MathF.Sqrt(DistanceToSquared(point));
- }
- /// <summary>
- /// Calculates the squared distance from this rectangle to the specified point.
- /// </summary>
- /// <param name="point">The point to measure distance to.</param>
- /// <returns>The squared distance to the point, or zero if the point is inside the rectangle.</returns>
- public readonly float DistanceToSquared(Vector2 point)
- {
- float squaredDistance = 0.0f;
- if (point.X < Left)
- {
- float distance = Left - point.X;
- squaredDistance += distance * distance;
- }
- else if (point.X > Right)
- {
- float distance = point.X - Right;
- squaredDistance += distance * distance;
- }
- if (point.Y < Top)
- {
- float distance = Top - point.Y;
- squaredDistance += distance * distance;
- }
- else if (point.Y > Bottom)
- {
- float distance = point.Y - Bottom;
- squaredDistance += distance * distance;
- }
- return squaredDistance;
- }
- #endregion
- #region Offset Methods
- /// <summary>
- /// Moves this rectangle by the specified offset.
- /// </summary>
- /// <param name="amount">The amount to move horizontally and vertically.</param>
- public void Offset(Vector2 amount)
- {
- X += amount.X;
- Y += amount.Y;
- }
- /// <summary>
- /// Returns a rectangle moved by the specified offset.
- /// </summary>
- /// <param name="rectangle">The rectangle to move.</param>
- /// <param name="amount">The amount to move horizontally and vertically.</param>
- /// <returns>The moved rectangle.</returns>
- public static RectangleF Offset(RectangleF rectangle, Vector2 amount)
- {
- return rectangle with { X = rectangle.X + amount.X, Y = rectangle.Y + amount.Y };
- }
- #endregion
- #region Inflate Methods
- /// <summary>
- /// Inflates this rectangle by the specified amount in both directions.
- /// </summary>
- /// <param name="amount">The amount to inflate horizontally and vertically.</param>
- public void Inflate(Vector2 amount)
- {
- X -= amount.X;
- Y -= amount.Y;
- Width += amount.X * 2.0f;
- Height += amount.Y * 2.0f;
- }
- /// <summary>
- /// Returns an inflated rectangle from the specified rectangle and amount.
- /// </summary>
- /// <param name="rectangle">The rectangle to inflate.</param>
- /// <param name="amount">The amount to inflate horizontally and vertically.</param>
- /// <returns>The inflated rectangle.</returns>
- public static RectangleF Inflate(RectangleF rectangle, Vector2 amount)
- {
- return new RectangleF(
- rectangle.X - amount.X,
- rectangle.Y - amount.Y,
- rectangle.Width + amount.X * 2.0f,
- rectangle.Height + amount.Y * 2.0f
- );
- }
- #endregion
- #region Transform Methods
- /// <summary>
- /// Transforms this rectangle using the specified matrix.
- /// </summary>
- /// <param name="matrix">The transformation matrix.</param>
- public void Transform(Matrix3x2 matrix)
- {
- Transform(ref matrix);
- }
- /// <summary>
- /// Transforms this rectangle using the specified matrix.
- /// </summary>
- /// <param name="matrix">The transformation matrix.</param>
- public void Transform(ref Matrix3x2 matrix)
- {
- Vector2 center = Center;
- Vector2 halfExtents = Size * 0.5f;
- PrimitivesHelper.TransformRectangle(ref center, ref halfExtents, ref matrix);
- X = center.X + halfExtents.X;
- Y = center.Y + halfExtents.Y;
- Width = halfExtents.X * 2.0f;
- Height = halfExtents.Y * 2.0f;
- }
- /// <summary>
- /// Transforms the specified rectangle using the given matrix.
- /// </summary>
- /// <param name="rectangle">The rectangle to transform.</param>
- /// <param name="transformMatrix">The transformation matrix.</param>
- /// <returns>The transformed rectangle.</returns>
- /// <remarks>
- /// <para>
- /// If a previously transformed rectangle is used, the resulting rectangle will have compounded transformations.
- /// </para>
- /// </remarks>
- public static RectangleF Transform(RectangleF rectangle, Matrix3x2 transformMatrix)
- {
- return Transform(rectangle, ref transformMatrix);
- }
- /// <summary>
- /// Transforms the specified rectangle using the given matrix.
- /// </summary>
- /// <param name="rectangle">The rectangle to transform.</param>
- /// <param name="transformMatrix">The transformation matrix.</param>
- /// <returns>The transformed rectangle.</returns>
- public static RectangleF Transform(RectangleF rectangle, ref Matrix3x2 transformMatrix)
- {
- Vector2 center = rectangle.Center;
- Vector2 halfExtents = (Vector2)rectangle.Size * 0.5f;
- PrimitivesHelper.TransformRectangle(ref center, ref halfExtents, ref transformMatrix);
- float x = center.X - halfExtents.X;
- float y = center.Y - halfExtents.Y;
- float width = halfExtents.X * 2;
- float height = halfExtents.Y * 2;
- return new RectangleF(x, y, width, height);
- }
- #endregion
- #region Equality Methods
- /// <summary>
- /// Returns a value indicating whether this <see cref="RectangleF" /> is equal to a specified object.
- /// </summary>
- /// <param name="obj">The object to make the comparison with.</param>
- /// <returns>
- /// <c>true</c> if this <see cref="RectangleF" /> is equal to <paramref name="obj" />; otherwise, <c>false</c>.
- /// </returns>
- public readonly override bool Equals(object obj)
- {
- return obj is RectangleF other && Equals(other);
- }
- /// <summary>
- /// Indicates whether this <see cref="RectangleF" /> is equal to another <see cref="RectangleF" />.
- /// </summary>
- /// <param name="rectangle">The rectangle.</param>
- /// <returns>
- /// <c>true</c> if this <see cref="RectangleF" /> is equal to the <paramref name="rectangle" />; otherwise, <c>false</c>.
- /// </returns>
- public readonly bool Equals(RectangleF rectangle)
- {
- return X == rectangle.X
- && Y == rectangle.Y
- && Width == rectangle.Width
- && Height == rectangle.Height;
- }
- #endregion
- /// <summary>
- /// Returns a hash code of this <see cref="RectangleF" /> suitable for use in hashing algorithms and data
- /// structures like a hash table.
- /// </summary>
- /// <returns>
- /// A hash code of this <see cref="RectangleF" />.
- /// </returns>
- public override int GetHashCode()
- {
- return HashCode.Combine(X, Y, Width, Height);
- }
- /// <summary>
- /// Returns a <see cref="string" /> that represents this <see cref="RectangleF" />.
- /// </summary>
- /// <returns>
- /// A <see cref="string" /> that represents this <see cref="RectangleF" />.
- /// </returns>
- public override string ToString()
- {
- return $"X: {X}, Y: {Y}, Width: {Width}, Height: {Height}";
- }
- internal string DebugDisplayString => string.Concat(X, " ", Y, " ", Width, " ", Height);
- #region Operators
- /// <summary>
- /// Compares two <see cref="RectangleF" /> structures. The result specifies whether the values of the
- /// <see cref="X" />, <see cref="Y"/>, <see cref="Width"/> and <see cref="Height" /> fields of the two <see cref="RectangleF" /> structures
- /// are equal.
- /// </summary>
- /// <param name="left">The first rectangle.</param>
- /// <param name="right">The second rectangle.</param>
- /// <returns>
- /// <c>true</c> if the values of the
- /// <see cref="X" />, <see cref="Y"/>, <see cref="Width"/> and <see cref="Height" /> fields of the two <see cref="RectangleF" /> structures
- /// are equal; otherwise, <c>false</c>.
- /// </returns>
- public static bool operator ==(RectangleF left, RectangleF right)
- {
- return left.Equals(right);
- }
- /// <summary>
- /// Compares two <see cref="RectangleF" /> structures. The result specifies whether the values of the
- /// <see cref="X" />, <see cref="Y"/>, <see cref="Width"/> and <see cref="Height" /> fields of the two <see cref="RectangleF" /> structures
- /// are unequal.
- /// </summary>
- /// <param name="left">The first rectangle.</param>
- /// <param name="right">The second rectangle.</param>
- /// <returns>
- /// <c>true</c> if the values of the
- /// <see cref="X" />, <see cref="Y"/>, <see cref="Width"/> and <see cref="Height" /> fields of the two <see cref="RectangleF" /> structures
- /// are unequal; otherwise, <c>false</c>.
- /// </returns>
- public static bool operator !=(RectangleF left, RectangleF right)
- {
- return !left.Equals(right);
- }
- /// <summary>
- /// Performs an implicit conversion from a <see cref="Rectangle" /> to a <see cref="RectangleF" />.
- /// </summary>
- /// <param name="rectangle">The rectangle.</param>
- /// <returns>
- /// The resulting <see cref="RectangleF" />.
- /// </returns>
- public static implicit operator RectangleF(Rectangle rectangle)
- {
- return new RectangleF(
- rectangle.X,
- rectangle.Y,
- rectangle.Width,
- rectangle.Height
- );
- }
- /// <summary>
- /// Performs an explicit conversion from a <see cref="Rectangle" /> to a <see cref="RectangleF" />.
- /// </summary>
- /// <param name="rectangle">The rectangle.</param>
- /// <returns>
- /// The resulting <see cref="RectangleF" />.
- /// </returns>
- /// <remarks>
- /// <para>A loss of precision may occur due to the truncation from <see cref="float" /> to <see cref="int" />.</para>
- /// </remarks>
- public static explicit operator Rectangle(RectangleF rectangle)
- {
- return new Rectangle(
- (int)rectangle.X,
- (int)rectangle.Y,
- (int)rectangle.Width,
- (int)rectangle.Height
- );
- }
- #endregion
- }
|