3
\$\begingroup\$

I'm trying to implement a score meter whereby I specify a maximum value (say 15,000) and I can render values on it in a logarithmic manner ie:

+------+---+--+-++        +------+---+--+-++
|==              |        |======          |
+------+---+--+-++        +------+---+--+-++
 200 pts                   1,000 pts

+------+---+--+-++        +------+---+--+-++
|=============   |        |================|
+------+---+--+-++        +------+---+--+-++
 5,000 pts                 15,000 pts +

The upper bound needs to be variable, and need to be able to convert a score to a percentage, using the above mockup as an example:

score2pct(15000, 200)   = 0.2
score2pct(15000, 1000)  = 0.4
score2pct(15000, 5000)  = 0.8
score2pct(15000, 15000) = 1

Does anyone have any pointers for me?

\$\endgroup\$
  • 1
    \$\begingroup\$ Be more geek-like: Buy an old slide rule and scan-and-scale it in as your meter! Like this! \$\endgroup\$ – Pieter Geerkens Nov 12 '13 at 23:21
8
\$\begingroup\$

I added a simpler curve to Nathan's answer,

enter image description here

The curve is

1 - ( b - x ) / (c*x + b)

It doesn't use log, and you can alter the steepness of the curve using the c parameter. When c=0, the curve becomes linear.

\$\endgroup\$
  • \$\begingroup\$ Good curve skills @bobobobo, I love that the c parameter is variable, I think I can utilise that to good effect as my end value changes :-) \$\endgroup\$ – oodavid Nov 13 '13 at 11:10
3
\$\begingroup\$

Sure, you can use a logarithmic function for this, for example:

scale = log(score / 160.0 + 1.0) / log(maxScore / 160.0 + 1.0)

Here, the value 160 is a constant that controls how steep or shallow the curve is. I also added 1.0 to make the curve start from zero (otherwise, the log function goes to negative infinity at zero).

I used the Desmos online graphing calculator app to plot your suggested points and play around with the curve. It's a quite handy tool for this sort of problem.

\$\endgroup\$
  • \$\begingroup\$ This is a great tool. I had been fiddling around in Grapher recently, of all things. \$\endgroup\$ – bobobobo Nov 12 '13 at 23:22
  • \$\begingroup\$ Thanks Nathan, that's a great answer and loving Desmos too! \$\endgroup\$ – oodavid Nov 13 '13 at 11:08
1
\$\begingroup\$

So your max score is 15000, and you want to do everything on the log scale (I'm assuming base 10, but it doesn't matter as long as you're consistent. Some languages use e as a basis, some use 10).

First you need to find the max score on your log scale that corresponds to 15000 (your chosen max). Then you'd just log transform your score and compute the percentage from those. It's pretty simple.

max = 15000
logMax = log(max + 1)
score = 200
logScore = log(score + 1)

percent = log(score)/log(max)

I hope that helps.

\$\endgroup\$
  • \$\begingroup\$ Thanks, I think I failed to see the obvious; I just needed to calculate the value at 100% and then create my percentage based on that! \$\endgroup\$ – oodavid Nov 13 '13 at 11:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.