Challenge: algorithm for counting progressively/exponentially - DonationCoder.com
HOME | Blog | Software | Reviews and Features | Forum | Help | Donate
Welcome Guest. April 26, 2015, 02:26:59 PM

Or did you miss your validation email?

Why not become a lifetime supporting member of the site with a one-time donation of any amount? Your donation entitles you to a ton of additional benefits, including access to exclusive discounts and downloads, the ability to enter monthly free software drawings, and a single non-expiring license key for all of our programs.

 You must sign up here before you can post and access some areas of the site. Registration is totally free and confidential.
 Learn about the DonationCoder.com microdonation system (DonationCredits). Entire Forum This board This topic Members Help Desk Entire Site
Pages: [1]   Go Down
 Reply  |  New Topic  |  Print
 Author Topic: Challenge: algorithm for counting progressively/exponentially  (Read 1683 times)
housetier
Charter Honorary Member

Posts: 1,321

 « on: September 16, 2010, 01:56:52 PM »

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.
 Logged
Codebyte
Supporting Member

Posts: 160

"Premature Optimization is the root of all evil."

 « Reply #1 on: September 16, 2010, 02:02:30 PM »

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:

http://robertpenner.com/easing/

and here is how I use it:

http://concepts.codebyter.com/js/cb_animator2/

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.
 « Last Edit: September 16, 2010, 02:26:35 PM by Codebyte » Logged

CodeByter.com - http://www.codebyter.com
housetier
Charter Honorary Member

Posts: 1,321

 « Reply #2 on: September 16, 2010, 02:15:21 PM »

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

Thank you very much!
 Logged
Deozaan
Charter Member

Posts: 6,751

 « Reply #3 on: September 16, 2010, 03:21:47 PM »

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.
 « Last Edit: September 16, 2010, 05:35:38 PM by Deozaan » Logged

CWuestefeld
Supporting Member

Posts: 965

 « Reply #4 on: September 16, 2010, 04:31:05 PM »

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.

So:

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

 « Last Edit: September 16, 2010, 04:37:13 PM by CWuestefeld » Logged

Pages: [1]   Go Up
 Reply  |  New Topic  |  Print