| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="NumericInterval.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using System.Numerics; |
| | | 9 | | |
| | | 10 | | namespace MyNet.Primitives.Intervals; |
| | | 11 | | |
| | | 12 | | /// <summary> |
| | | 13 | | /// Represents a numeric interval where the type of the values is a numeric type that implements the <see cref="INumber{ |
| | | 14 | | /// </summary> |
| | 42 | 15 | | public abstract class NumericInterval<T, TSelf>(IntervalBoundary<T>? start, IntervalBoundary<T>? end) : Interval<T, TSel |
| | | 16 | | where T : struct, INumber<T>, IMinMaxValue<T> |
| | | 17 | | where TSelf : NumericInterval<T, TSelf> |
| | | 18 | | { |
| | | 19 | | /// <summary> |
| | | 20 | | /// Gets the length of the interval, calculated as the difference between the end and start values. If either the st |
| | | 21 | | /// </summary> |
| | 33 | 22 | | public T? Length => !HasStart || !HasEnd ? null : End!.Value.Value - Start!.Value.Value; |
| | | 23 | | |
| | | 24 | | /// <summary> |
| | | 25 | | /// Gets a value indicating whether the interval is positive, which is determined by checking if the length of the i |
| | | 26 | | /// </summary> |
| | 6 | 27 | | public bool IsPositive => Length is null || Length > T.Zero; |
| | | 28 | | |
| | | 29 | | /// <summary> |
| | | 30 | | /// Gets a value indicating whether the interval is negative, which is determined by checking if the length of the i |
| | | 31 | | /// </summary> |
| | 6 | 32 | | public bool IsNegative => Length < T.Zero; |
| | | 33 | | |
| | | 34 | | /// <summary> |
| | | 35 | | /// Clamps a value to the interval, ensuring that it falls within the defined boundaries. If the value is less than |
| | | 36 | | /// </summary> |
| | 9 | 37 | | public T Clamp(T value) => HasStart && value.CompareTo(Start!.Value.Value) < 0 ? Start.Value.Value : HasEnd && value |
| | | 38 | | |
| | | 39 | | /// <summary> |
| | | 40 | | /// Normalizes a value within the interval, converting it to a value between 0 and 1 based on its position relative |
| | | 41 | | /// </summary> |
| | 6 | 42 | | public T Normalize(T value) => Length is null ? throw new InvalidOperationException("Cannot normalize unbounded inte |
| | | 43 | | } |
| | | 44 | | |