Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@nbeck-SMT
Copy link
Contributor

@nbeck-SMT nbeck-SMT commented Oct 16, 2025

This PR fixes #5108.

The problem is caused by the fact that the pixel column position is cast to the type of the displayed data. In the case of integers, a data point will be displayed when the current pixel column position rounds down to the value of the data point and the next column position rounds up. This will only happen at half integer positions.

One solution would be to round up the column position for integer types. But this means checking for all integral types, which would break if new types are introduced.

private (int SearchedPosition, int LimitedIndex) SearchIndex(double x, IndexRange indexRange)
{
    var valueToSearch = (x - XOffset) / XScale;
    if (typeof(TX) == typeof(long) || typeof(TX) == typeof(ulong)
        || typeof(TX) == typeof(int) || typeof(TX) == typeof(uint)
        || ... )
        valueToSearch = Math.Ceiling(valueToSearch);
    NumericConversion.DoubleToGeneric(valueToSearch, out TX x2);
    int index = Array.BinarySearch(Xs, indexRange.Min, indexRange.Length, x2);
    ....

Another solution is to use a custom comparer for the binary search that compares the data points with the double pixel position. But this would rely on the fact that the implementation of binary search always uses the left slot of the comparer for array values.

private (int SearchedPosition, int LimitedIndex) SearchIndex(double x, IndexRange indexRange)
{
    TX dummyValue = NumericConversion.DoubleToGeneric<TX>(0);
    var comparer = Comparer<TX>.Create((v, _) => NumericConversion.GenericToDouble(ref v).CompareTo((x - XOffset) / XScale));
    int index = Array.BinarySearch(Xs, indexRange.Min, indexRange.Length, dummyValue, comparer);
    ...

I believe the cleanest solution is to implement a binary search that looks for the index to insert a double into the given array.

Let me know what you think.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SignalXY with long[] shifts points by +0.5 compared to double[]

1 participant