41 lines
1 KiB
C#
41 lines
1 KiB
C#
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<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);
|
|
}
|
|
}
|
|
}
|