BoundingBox.cs 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. using QuestPDF.Infrastructure;
  2. namespace QuestPDF.LayoutTests.TestEngine;
  3. internal class BoundingBox
  4. {
  5. public double MinX { get; init; }
  6. public double MinY { get; init; }
  7. public double MaxX { get; init; }
  8. public double MaxY { get; init; }
  9. public double Width => MaxX - MinX;
  10. public double Height => MaxY - MinY;
  11. public override string ToString() => $"BBox(Min: {MinX:N0}x{MinY:N0}, Max: {MaxX:N0}x{MaxY:N0}, W: {Width:N0}, H: {Height:N0})";
  12. public static BoundingBox From(Position position, Size size)
  13. {
  14. return new BoundingBox
  15. {
  16. MinX = position.X,
  17. MinY = position.Y,
  18. MaxX = position.X + size.Width,
  19. MaxY = position.Y + size.Height
  20. };
  21. }
  22. }
  23. internal static class BoundingBoxExtensions
  24. {
  25. public static BoundingBox? Intersection(BoundingBox first, BoundingBox second)
  26. {
  27. var common = new BoundingBox
  28. {
  29. MinX = Math.Max(first.MinX, second.MinX),
  30. MinY = Math.Max(first.MinY, second.MinY),
  31. MaxX = Math.Min(first.MaxX, second.MaxX),
  32. MaxY = Math.Min(first.MaxY, second.MaxY),
  33. };
  34. if (common.Width < 0 || common.Height < 0)
  35. return null;
  36. return common;
  37. }
  38. }