unlinq'd & refactored

This commit is contained in:
Lilith Schier 2026-08-30 16:02:24 +02:00
parent a5093e62e4
commit 6bdc9617b2
4 changed files with 38 additions and 24 deletions

View file

@ -1,12 +1,41 @@
using System.Runtime.InteropServices.Swift;
using System;
using System.Diagnostics;
namespace Dancer;
public static class TileMapLayerExtensions
public static class Extensions
{
extension(TileMapLayer self)
{
public Vector2I GlobalToMap(Vector2 globalPosition) =>
self.LocalToMap(self.ToLocal(globalPosition));
}
extension(ReadOnlySpan<Vector2> polygon)
{
/// <summary>
/// Calculates the bounding rectangle that encompasses all points in the given polygon.
/// </summary>
/// <remarks>
/// The polygon must contain at least one point.
/// </remarks>
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<Vector2> 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);
}
}
}