StorageHelpers.cs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. namespace Content.Shared.Storage;
  2. public static class StorageHelper
  3. {
  4. public static Box2i GetBoundingBox(this IReadOnlyList<Box2i> boxes)
  5. {
  6. if (boxes.Count == 0)
  7. return new Box2i();
  8. var firstBox = boxes[0];
  9. if (boxes.Count == 1)
  10. return firstBox;
  11. var bottom = firstBox.Bottom;
  12. var left = firstBox.Left;
  13. var top = firstBox.Top;
  14. var right = firstBox.Right;
  15. for (var i = 1; i < boxes.Count; i++)
  16. {
  17. var box = boxes[i];
  18. if (bottom > box.Bottom)
  19. bottom = box.Bottom;
  20. if (left > box.Left)
  21. left = box.Left;
  22. if (top < box.Top)
  23. top = box.Top;
  24. if (right < box.Right)
  25. right = box.Right;
  26. }
  27. return new Box2i(left, bottom, right, top);
  28. }
  29. public static int GetArea(this IReadOnlyList<Box2i> boxes)
  30. {
  31. var area = 0;
  32. var bounding = boxes.GetBoundingBox();
  33. for (var y = bounding.Bottom; y <= bounding.Top; y++)
  34. {
  35. for (var x = bounding.Left; x <= bounding.Right; x++)
  36. {
  37. if (boxes.Contains(x, y))
  38. area++;
  39. }
  40. }
  41. return area;
  42. }
  43. public static bool Contains(this IReadOnlyList<Box2i> boxes, int x, int y)
  44. {
  45. foreach (var box in boxes)
  46. {
  47. if (box.Contains(x, y))
  48. return true;
  49. }
  50. return false;
  51. }
  52. public static bool Contains(this IReadOnlyList<Box2i> boxes, Vector2i point)
  53. {
  54. foreach (var box in boxes)
  55. {
  56. if (box.Contains(point))
  57. return true;
  58. }
  59. return false;
  60. }
  61. }