forked from ddrilling/AsbCloudServer
53 lines
1.8 KiB
C#
53 lines
1.8 KiB
C#
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 x, float? y) lastValue = default;
|
|
|
|
public Interpolation(IOrderedEnumerable<(DateTimeOffset dateMin, float? depthMin, DateTimeOffset dateMax, float? depthMax)> collection)
|
|
{
|
|
enumerator = collection.GetEnumerator();
|
|
enumerator.MoveNext();
|
|
}
|
|
|
|
public float GetDepth(DateTimeOffset date)
|
|
{
|
|
{
|
|
while (date >= enumerator.Current.dateMax)
|
|
{
|
|
if (isLess(date))
|
|
{
|
|
lastValue.x = date;
|
|
lastValue.y = CalcValue(enumerator.Current, date.Second);
|
|
}
|
|
enumerator.MoveNext();
|
|
}
|
|
}
|
|
return (float)lastValue.y;
|
|
}
|
|
|
|
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
|
|
}
|