Wednesday, 28 August 2013

In an array, how do I find the closest key given a float value?

In an array, how do I find the closest key given a float value?

I'm making an "acceleration" array like this:
acc["0.1"] = 1;
acc["0.3"] = 2;
acc["0.6"] = 4;
acc["0.9"] = 8;
acc["2.0"] = 16;
acc["5.0"] = 32;
And, when the user presses a key, I start a timer: this._startTick = (new
Date()).getTime();
Now I have a timer that checks if the key is still pressed. If so, then I
do something like:
this._delay = (new Date()).getTime() - this._startTick;
And now, based on this._delay, I'd like to find one of the previous values
(1, 2, 4 or 8). How would you do that?
NOTA: my goal is, given an elapsed time, find out which value is the best.
I started the way I've just explained, but if you have another solution,
I'll take it!

No comments:

Post a Comment