/// <summary> /// Gets the intersection between an interval and two values /// </summary> /// <typeparam name="T">Type of the interval values</typeparam> /// <param name="owningInterval">Owning interval</param> /// <param name="firstValue">First value</param> /// <param name="secondValue">Second value</param> /// <param name="captureEqual">True if equal values should be considered as intersecting, otherwise false</param> /// <returns>Intersection</returns> /// <exception cref="ArgumentNullException">Throws if any provided value is null</exception> /// <exception cref="ArgumentException">Throws if the provided values does not intersect</exception> public static IInterval <T> GetIntersection <T>(this IInterval <T> owningInterval, T firstValue, T secondValue, bool captureEqual = true) where T : IComparable <T> { if (owningInterval.Intersects(firstValue, secondValue, captureEqual)) { Interval <T> interval = new Interval <T> ( start: owningInterval.Start.CompareTo(firstValue) > 0 ? owningInterval.Start : firstValue, stop: owningInterval.Stop.CompareTo(secondValue) < 0 ? owningInterval.Stop : secondValue ); return(interval); } else { throw new ArgumentException(nameof(owningInterval), "The provided values does not intersect"); } }
/// <summary> /// Attempts to get the intersection between two intervals /// </summary> /// <typeparam name="T">Type of the interval values</typeparam> /// <param name="owningInterval">Owning interval</param> /// <param name="interval">Interval</param> /// <param name="intersection">The resulting intersection</param> /// <param name="captureEqual">True if equal values should be considered as intersecting, otherwise false</param> /// <returns>True if the values intersects, otherwise false</returns> /// <exception cref="ArgumentNullException">Throws if any provided value is null</exception> public static bool TryGetIntersection <T>(this IInterval <T> owningInterval, IInterval <T> interval, out IInterval <T> intersection, bool captureEqual = true) where T : IComparable <T> { if (owningInterval.Intersects(interval, captureEqual)) { Interval <T> intersectionInterval = new Interval <T> ( start: owningInterval.Start.CompareTo(interval.Start) > 0 ? owningInterval.Start : interval.Start, stop: owningInterval.Stop.CompareTo(interval.Stop) < 0 ? owningInterval.Stop : interval.Stop ); intersection = intersectionInterval; return(true); } else { intersection = default(IInterval <T>); return(false); } }
/// <summary> /// Attempts to get the intersection between an interval and two values /// </summary> /// <typeparam name="T">Type of the interval values</typeparam> /// <param name="owningInterval">Owning interval</param> /// <param name="firstValue">First value</param> /// <param name="secondValue">Second value</param> /// <param name="intersection">The resulting intersection</param> /// <param name="captureEqual">True if equal values should be considered as intersecting, otherwise false</param> /// <returns>True if the values intersects, otherwise false</returns> /// <exception cref="ArgumentNullException">Throws if any provided value is null</exception> public static bool TryGetIntersection <T>(this IInterval <T> owningInterval, T firstValue, T secondValue, out IInterval <T> intersection, bool captureEqual = true) where T : IComparable <T> { if (owningInterval.Intersects(firstValue, secondValue, captureEqual)) { Interval <T> interval = new Interval <T> ( start: owningInterval.Start.CompareTo(firstValue) > 0 ? owningInterval.Start : firstValue, stop: owningInterval.Stop.CompareTo(secondValue) < 0 ? owningInterval.Stop : secondValue ); intersection = interval; return(true); } else { intersection = default(IInterval <T>); return(false); } }