ScottPlot/ScottPlot

SignalXY: Add `GetPointNearestX`

cataclism opened this issue · 6 comments

It would be awesome to bring back GetPointNearestX of ScottPlot 4.x for SignalXY to be able again to extract the Yvalue of the SignalXY given the Xposition.

I tried to look at the old code that looks like this:

private (TX x, TY y, int index) GetPointByIndex(int index)
{
    return (Xs[index], Ys[index], index);
}

/// <summary>
/// Return the X/Y coordinates of the point nearest the X position
/// </summary>
/// <param name="x">X position in plot space</param>
/// <returns></returns>
public (TX x, TY y, int index) GetPointNearestX(TX x)
{
    int index = Array.BinarySearch(Xs, MinRenderIndex, MaxRenderIndex - MinRenderIndex, x);
    if (index < 0)
    {
        index = ~index;
    }
    else // x equal to XS element
    {
        return GetPointByIndex(index);
    }
    if (index <= MinRenderIndex) // x lower then any MinRenderIndex
        return GetPointByIndex(MinRenderIndex);
    if (index > MaxRenderIndex) // x higher then MaxRenderIndex
        return GetPointByIndex(MaxRenderIndex);

    TX distLeft = SubstractExp(x, Xs[index - 1]);
    TX distRight = SubstractExp(Xs[index], x);
    if (LessThanOrEqualExp(distLeft, distRight)) // x closer to XS[index -1]
        return GetPointByIndex(index - 1);
    else // x closer to XS[index]
        return GetPointByIndex(index);
}

Something similar would be awesome. I don't know however how to extract X and Y from a signalXY of it's even possible