ATTENTION: You are viewing a page formatted for mobile devices; to view the full web page, click HERE.

Other Software > Developer's Corner

Challenge: algorithm for counting progressively/exponentially


I have tasked myself with writing a function that will count up to a number in a dynamic fashion, that means it'll start with large steps that become smaller and smaller as the counter reaches the target. So while at first it would start counting in the thousands, at the end it would only increment using 1.

However, it seems I have lost all mathematical ability since leaving school. I cannot for the time of my life come up with the algorithm! There is only half-ideas and flashes, but nothing like "start like so, then for every iteration check this and that, calculate the increment by whatever and increment"; I can design and implement complex applications, but I cannot see this one algorithm.

When plotting the counter value it should look similar to this image (taken from a wikipedia article about Sigmoid functionsw), more precisely like the upper right quadrant: it increases steeply at first and then flattens ever more.

So here goes the challenge:

Can anyone tell or show me the algorithm (easy, I think) and can anybody explain this algorithm to me, maybe step by step.

housetier, you should check out "easing". This will help you do alot of things with calculating progress. I use this in my animator library for js. Here is the link for the easing stuff:

and here is how I use it:

calculating easing is a really cool concept and there are a ton of things you can do with it. You can specify your own effects etc.

Excellent, that gives me an idea for the first of the challenge.

Thank you very much!  :-*

This doesn't look like that graph, but basically you just add half the distance (rounded to an integer) between the current value and the target value until the distance is <=1 then you just add the remaining distance.

EDIT: I noticed this is called the Standard Exponential Slide in Robert Penner's chapter on Tweening in Flash.

Since I'm a sci-fi geek, I'm thinking of the velocity of a starship as it travels to its destination under constant (say, 1g) acceleration. The idea is that you reach your highest velocity at the halfway point, then turn around, and start decelerating.


halfway = distance / 2
velocity = 0
counter = 0
accelerationfactor = 1
while (counter < distance)
    if (counter > halfway AND accelerationfactor > 1)
        accelerationfactor = accelerationfactor * -1 // turnaround
    velocity += accelerationfactor
    counter += velocity

You'll need to clean up for overshooting, but I think that should pretty much do what you want.

EDIT: re-reading your question, your linked image doesn't look like what you've described textually. The algorithm I've outlined will give you the linked image. But if you want to start from "high speed" and just decelerate, then either do the geometric algorithm that Deozaan gives, or

velocity = 100 // or whatever
acceleration = 1
counter = 0
while (counter < target)
    velocity -= acceleration
    counter += velocity


[0] Message Index

Go to full version