Rectangle.Extensions.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using System;
  2. using Microsoft.Xna.Framework;
  3. namespace MonoGame.Extended;
  4. /// <summary>
  5. /// Provides extension methods for the <see cref="Rectangle"/> structure.
  6. /// </summary>
  7. public static class RectangleExtensions
  8. {
  9. /// <summary>
  10. /// Gets the corners of the rectangle in a clockwise direction starting at the top left.
  11. /// </summary>
  12. /// <param name="rectangle">The rectangle to get the corners of.</param>
  13. /// <returns>An array of <see cref="Point"/> elements representing the corners of the rectangle.</returns>
  14. public static Point[] GetCorners(this Rectangle rectangle)
  15. {
  16. var corners = new Point[4];
  17. corners[0] = new Point(rectangle.Left, rectangle.Top);
  18. corners[1] = new Point(rectangle.Right, rectangle.Top);
  19. corners[2] = new Point(rectangle.Right, rectangle.Bottom);
  20. corners[3] = new Point(rectangle.Left, rectangle.Bottom);
  21. return corners;
  22. }
  23. /// <summary>
  24. /// Converts the specified <see cref="Rectangle"/> to a <see cref="RectangleF"/>.
  25. /// </summary>
  26. /// <param name="rectangle">The rectangle to convert.</param>
  27. /// <returns>The converted <see cref="RectangleF"/>.</returns>
  28. public static RectangleF ToRectangleF(this Rectangle rectangle)
  29. {
  30. return new RectangleF(rectangle.X, rectangle.Y, rectangle.Width, rectangle.Height);
  31. }
  32. /// <summary>
  33. /// Clips the specified rectangle against the specified clipping rectangle.
  34. /// </summary>
  35. /// <param name="rectangle">The rectangle to clip.</param>
  36. /// <param name="clippingRectangle">The rectangle to clip against.</param>
  37. /// <returns>The clipped rectangle, or <see cref="Rectangle.Empty"/> if the rectangles do not intersect.</returns>
  38. public static Rectangle Clip(this Rectangle rectangle, Rectangle clippingRectangle)
  39. {
  40. int left = Math.Max(rectangle.Left, clippingRectangle.Left);
  41. int top = Math.Max(rectangle.Top, clippingRectangle.Top);
  42. int right = Math.Min(rectangle.Right, clippingRectangle.Right);
  43. int bottom = Math.Min(rectangle.Bottom, clippingRectangle.Bottom);
  44. int width = right - left;
  45. int height = bottom - top;
  46. if (width <= 0 || height <= 0)
  47. return Rectangle.Empty;
  48. return new Rectangle(left, top, width, height);
  49. }
  50. /// <summary>
  51. /// Gets a rectangle that is relative to the specified source rectangle, with the specified offsets and dimensions.
  52. /// </summary>
  53. /// <param name="source">The source rectangle.</param>
  54. /// <param name="x">The x-coordinate of the relative rectangle, relative to the source rectangle.</param>
  55. /// <param name="y">The y-coordinate of the relative rectangle, relative to the source rectangle.</param>
  56. /// <param name="width">The width, in pixels, of the relative rectangle.</param>
  57. /// <param name="height">The height, in pixels, of the relative rectangle.</param>
  58. /// <returns>The relative rectangle, clipped to the source rectangle.</returns>
  59. public static Rectangle GetRelativeRectangle(this Rectangle source, int x, int y, int width, int height)
  60. {
  61. int absoluteX = source.X + x;
  62. int absoluteY = source.Y + y;
  63. Rectangle relative;
  64. relative.X = (int)MathHelper.Clamp(absoluteX, source.Left, source.Right);
  65. relative.Y = (int)MathHelper.Clamp(absoluteY, source.Top, source.Bottom);
  66. relative.Width = Math.Max(Math.Min(absoluteX + width, source.Right) - relative.X, 0);
  67. relative.Height = Math.Max(Math.Min(absoluteY + height, source.Bottom) - relative.Y, 0);
  68. return relative;
  69. }
  70. #if FNA
  71. // MomoGame compatibility layer
  72. /// <summary>
  73. /// Deconstruction method for Rectangle.
  74. /// </summary>
  75. public static void Deconstruct(this Rectangle rectangle, out int x, out int y, out int width, out int height)
  76. {
  77. x = rectangle.X;
  78. y = rectangle.Y;
  79. width = rectangle.Width;
  80. height = rectangle.Height;
  81. }
  82. #endif
  83. }