topbanner_forum
  *

avatar image

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  • Tuesday October 15, 2024, 12:26 am
  • Proudly celebrating 15+ years online.
  • Donate now to become a lifetime supporting member of the site and get a non-expiring license key for all of our programs.
  • donate

Author Topic: Challenge: algorithm for counting progressively/exponentially  (Read 4483 times)

housetier

  • Charter Honorary Member
  • Joined in 2005
  • ***
  • default avatar
  • Posts: 1,321
    • View Profile
    • Donate to Member
Challenge: algorithm for counting progressively/exponentially
« on: September 16, 2010, 01:56 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.

Codebyte

  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 160
  • "Premature Optimization is the root of all evil."
    • View Profile
    • CodeByter.com
    • Donate to Member
Re: Challenge: algorithm for counting progressively/exponentially
« Reply #1 on: September 16, 2010, 02:02 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.code...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.
CodeByter.com - http://www.codebyter.com
« Last Edit: September 16, 2010, 02:26 PM by Codebyte »

housetier

  • Charter Honorary Member
  • Joined in 2005
  • ***
  • default avatar
  • Posts: 1,321
    • View Profile
    • Donate to Member
Re: Challenge: algorithm for counting progressively/exponentially
« Reply #2 on: September 16, 2010, 02:15 PM »
Excellent, that gives me an idea for the first of the challenge.

Thank you very much!  :-*

Deozaan

  • Charter Member
  • Joined in 2006
  • ***
  • Points: 1
  • Posts: 9,759
    • View Profile
    • Read more about this member.
    • Donate to Member
Re: Challenge: algorithm for counting progressively/exponentially
« Reply #3 on: September 16, 2010, 03:21 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 PM by Deozaan »

CWuestefeld

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 1,009
    • View Profile
    • Donate to Member
Re: Challenge: algorithm for counting progressively/exponentially
« Reply #4 on: September 16, 2010, 04:31 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 PM by CWuestefeld »