Search code examples
c#formulanonlinear-functions

Formula for non-linear graph. The further you slide, the less it moves


There's a name for a graph that does this, and I am trying to figure out an algorithm that calculates a result based on a DOUBLE input.

Like this: https://www.montereyinstitute.org/courses/Algebra1/COURSE_TEXT_RESOURCE/U03_L2_T5_text_final_files/image008.gif

I am trying to create a method that accomplishes what you see in apps that basically slows down velocity the further you pull. So for instance, if you slide your finger, a box appears easily, but then the further you pull, the slower it moves.

The full requirement is to have a "free pull" amount, i.e.: it's a 1:1 relationship where the amount you slide your finger, the output value is that much as well. And have a theoretical "max result", which I assume is theoretical, because the more your finger moves, the smaller the amount changes by.

I have a feeling there is a formula for this. So any Maths guys, please help :)


Solution

  • You will have to try and see which formula works best for you.

    Let's say the length of a finger pull is D and the distance of how far the box moves is D'. You can start with something very simple, like:

    D' = D / 2
    

    Then if you need some "free pull" distance of F, you would probably include it like this:

    D' = if D < F
         then D
         else F + (D - F) / 2
    

    To see which behavior of D' works best, you'll need to try different formulas. For example, a square root:

    D' = if D < F
         then D
         else F + sqrt(D - F)
    

    Edit: Here's a version with an upper boundary of F + M. It works because arctan's upper asymptote is Pi/2.

    D' = if D < F
         then D
         else F + arctan((D - F) / M) * M * (2 / Pi)
    

    Sample graph for F = 5, M = 3.
    D' will never reach 8 in this example.