using System; using System.Collections.Generic; using System.Linq; namespace AsbCloudInfrastructure.Services.Subsystems.Utils { #nullable enable internal class Interpolation { private IEnumerator<(DateTimeOffset dateMin, float? depthMin, DateTimeOffset dateMax, float? depthMax)> enumerator; (DateTimeOffset dateMin, float? depthMin, DateTimeOffset dateMax, float? depthMax) lastValue = default; public Interpolation(IOrderedEnumerable<(DateTimeOffset dateMin, float? depthMin, DateTimeOffset dateMax, float? depthMax)> collection) { enumerator = collection.GetEnumerator(); enumerator.MoveNext(); } public float GetDepth(DateTimeOffset date) { if (lastValue.depthMin is not null && lastValue.depthMax is not null) { while (date > enumerator.Current.dateMax) { if (isLess(date)) CalcValue(enumerator.Current, date.Second); enumerator.MoveNext(); } } return (float)lastValue.depthMin; } private bool isLess(DateTimeOffset date) { if (date <= enumerator.Current.dateMax && date >= enumerator.Current.dateMin) return true; return false; } private float CalcValue((DateTimeOffset dateMin, float? depthMin, DateTimeOffset dateMax, float? depthMax) item, int x) { var a = (item.depthMax - item.depthMin) / (item.dateMax.Second - item.dateMin.Second); var b = item.depthMin * (item.dateMax.Second - (item.depthMax - item.depthMin) / (item.dateMax.Second - item.dateMin.Second)); var resultDepth = a * x + b; return (float)resultDepth; } } #nullable disable }