Int32RectHelper.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. using SkiaSharp;
  2. using System;
  3. using System.Windows;
  4. namespace PixiEditor.Helpers.Extensions
  5. {
  6. public static class Int32RectHelper
  7. {
  8. public static Int32Rect Intersect(this Int32Rect rect, Int32Rect other)
  9. {
  10. int rectX2 = rect.X + rect.Width;
  11. int rectY2 = rect.Y + rect.Height;
  12. int otherX2 = other.X + other.Width;
  13. int otherY2 = other.Y + other.Height;
  14. int maxX1 = Math.Max(rect.X, other.X);
  15. int maxY1 = Math.Max(rect.Y, other.Y);
  16. int minX2 = Math.Min(rectX2, otherX2);
  17. int minY2 = Math.Min(rectY2, otherY2);
  18. int width = minX2 - maxX1;
  19. int height = minY2 - maxY1;
  20. if (width <= 0 || height <= 0)
  21. return Int32Rect.Empty;
  22. return new Int32Rect(maxX1, maxY1, width, height);
  23. }
  24. public static Int32Rect Expand(this Int32Rect rect, Int32Rect other)
  25. {
  26. int rectX2 = rect.X + rect.Width;
  27. int rectY2 = rect.Y + rect.Height;
  28. int otherX2 = other.X + other.Width;
  29. int otherY2 = other.Y + other.Height;
  30. int minX1 = Math.Min(rect.X, other.X);
  31. int minY1 = Math.Min(rect.Y, other.Y);
  32. int maxX2 = Math.Max(rectX2, otherX2);
  33. int maxY2 = Math.Max(rectY2, otherY2);
  34. int width = maxX2 - minX1;
  35. int height = maxY2 - minY1;
  36. if (width <= 0 || height <= 0)
  37. return Int32Rect.Empty;
  38. return new Int32Rect(minX1, minY1, width, height);
  39. }
  40. public static SKRectI ToSKRectI(this Int32Rect rect)
  41. {
  42. return new SKRectI(rect.X, rect.Y, rect.X + rect.Width, rect.Y + rect.Height);
  43. }
  44. }
  45. }