Search code examples
c#transitionsmoothingeasingquadratic

Smooth transition between values (ease in/out)


I'll probably get negative reputation for asking this question but I'm stuck and need help. I need to do a nice and smooth easing between two values, the result being displayed in a textbox. For example, my textbox is showing "125" and have to grow smoothly to "25000" in 2.5 seconds with a nice quadratic acceleration and deceleration curve. I'm not good at C# and I'm used to "hack" pieces of code I can find on the web and build them together to do what I need but, for this, I can't find anything I've been able to use so far. Is there anyone who can be kind enough to give me instructions, links or, even better a working piece of code to do this? Thank you very much Vince.


Solution

  • I've found that the simplest way to do this is with quadratic formulas (as you have already figured out). There are online calculators that can give you a parabolic equation using three points, and when taking (0, 125), (2.5, 25000), and (5, 125) we get an equation of y = -3980x^2 + 19900x + 125. Translated into C#, this gives us:

    var foo = -3980 * Math.Power(bar, 2) + 19900 * bar + 125;
    

    After that, we can use a for loop and a small sleep time to transition smoothly from bar = 0 to bar = 2.5.