1
0
mirror of https://github.com/ppy/osu synced 2025-01-14 01:51:04 +00:00

Guard against empty range in PositionRange

This commit is contained in:
ekrctb 2021-07-22 13:28:40 +09:00
parent 7b6981c632
commit 19657cd00e

View File

@ -8,14 +8,15 @@ using System;
namespace osu.Game.Rulesets.Catch.Edit
{
/// <summary>
/// Represents a closed interval of horizontal positions in the playfield.
/// Represents either the empty range or a closed interval of horizontal positions in the playfield.
/// A <see cref="PositionRange"/> represents a closed interval if it is <see cref="Min"/> &lt;= <see cref="Max"/>, and represents the empty range otherwise.
/// </summary>
public readonly struct PositionRange
{
public readonly float Min;
public readonly float Max;
public float Length => Max - Min;
public float Length => Math.Max(0, Max - Min);
public PositionRange(float value)
: this(value, value)
@ -30,7 +31,11 @@ namespace osu.Game.Rulesets.Catch.Edit
public static PositionRange Union(PositionRange a, PositionRange b) => new PositionRange(Math.Min(a.Min, b.Min), Math.Max(a.Max, b.Max));
public float GetFlippedPosition(float x) => Max - (x - Min);
/// <summary>
/// Get the given position flipped (mirrored) for the axis at the center of this range.
/// Returns the given position unchanged if the range was empty.
/// </summary>
public float GetFlippedPosition(float x) => Min <= Max ? Max - (x - Min) : x;
public static readonly PositionRange EMPTY = new PositionRange(float.PositiveInfinity, float.NegativeInfinity);
}