using System; using System.Diagnostics; namespace Dancer; public static class Extensions { extension(TileMapLayer self) { public Vector2I GlobalToMap(Vector2 globalPosition) => self.LocalToMap(self.ToLocal(globalPosition)); } extension(ReadOnlySpan polygon) { /// /// Calculates the bounding rectangle that encompasses all points in the given polygon. /// /// /// The polygon must contain at least one point. /// public Rect2 CalculateBoundingRect() { var result = new Rect2(polygon[0], Vector2.Zero); foreach (var point in polygon[1..]) result = result.Expand(point); return result; } } extension(Rect2 rect) { public void WritePoints(Span poly) { Debug.Assert(poly.Length >= 4); poly[0] = rect.Position; poly[1] = rect.Position + new Vector2(rect.Size.X, 0); poly[2] = rect.End; poly[3] = rect.Position + new Vector2(0, rect.Size.Y); } } }