// - edit Rect.tt to modify #nullable enable namespace Brutal.Numerics; /// /// Implements a 2D rectangle with float components /// public struct floatRect { /// /// The start of the rectangle. /// public float2 Min; /// /// The end of the rectangle. /// public float2 Max; /// A mutable view over the 4 components: min xy, then max xy. /// /// Deliberately NOT a readonly member, because it hands out a writable view. The consequence is that /// calling it on a readonly field or an parameter gets you a span over a /// defensive COPY, so writes there are silently lost - reach for instead. /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public Span AsSpan() { return MemoryMarshal.CreateSpan(ref Unsafe.As(ref this), 4); } /// A read-only view over the 4 components, safe in the readonly contexts where would view a copy. [MethodImpl(MethodImplOptions.AggressiveInlining)] public readonly ReadOnlySpan AsReadOnlySpan() { return MemoryMarshal.CreateReadOnlySpan(in Unsafe.As(ref Unsafe.AsRef(in this)), 4); } /// /// The center of the rectangle. /// public float2 Center { readonly get => (Min + Max) / 2; set { var half = Extent / 2; Min = value - half; Max = value + half; } } /// /// The size of the rectangle. /// public float2 Extent { readonly get => Max - Min; set => Max = Min + value; } }