3

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.

  • "even better a working piece of code to do this" please post a non-working version and we can try to help you to fix it. ah and don't forget to describe where exactly you got stuck, error messages etc... – Mong Zhu Feb 05 '18 at 14:16
  • It'd help if you told us which UI framework you're using, is it WPF? Winforms? Something else? I'm pretty sure there'll be "tweening" NuGet packages for whatever UI framework you're using – Dan Rayson Feb 05 '18 at 14:17
  • I simply vote to close as off-topic, because questions about tutorials and documentational stuff are off-topic on StackOverflow. – MakePeaceGreatAgain Feb 05 '18 at 14:18
  • Piece of not working code: well... I even do not have such a non working code. When I search the web on how to do a smooth quadratic transition between values I do not get things I can try or use. Most of the answers are for Unity using Lerp functions that does not work in VisualStudio. – MadeByVince Feb 05 '18 at 14:49
  • As for the UI, I'm using basic Winform. Thanks – MadeByVince Feb 05 '18 at 14:50
  • @MadeByVince If my answer helped you please accept it, it's not nice to leave future visitors hanging –  Apr 11 '18 at 16:55

1 Answers1

1

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.

  • Thank you very much for the quick answer. I'll look at this and see if I can make it work (mainly because the example values I gave were just example and will be different every single time so I need to figure out how to calculate the equation on the fly). ;) – MadeByVince Feb 05 '18 at 14:42
  • @MadeByVince in that case, generate a bunch of parabolas that intersect the x-axis at 0 and 5, then see what base equation you can multiply your value by to get the equation you need. –  Feb 05 '18 at 15:26
  • @MadeByVince Also, it just occurred to me that an "ease in, ease out" function might work better with a sinusoidal wave instead of a quadratic equation, as that will level out at both ends of the area. –  Feb 05 '18 at 15:27