I
I
Ivan Roganov2012-07-16 03:55:45
Programming
Ivan Roganov, 2012-07-16 03:55:45

How to get a function from a set of values?

The moment is as follows - I'm trying to figure out an algorithm for the following task:
There is a list of players, the players have points. There is a picture, it shows the levels (of which there are currently 6) and the number of points that these levels have.
For example,
level 1 - 601-700
level 2 - 701-800
level 3 - 801-1000
level 4 - 1001-1200
level 5 - 1201-1400
level 6 - 1401-1600
dbb8262faec82bb4266c778f41c73a00.png
The height picture is determined, but not for long. Now the height of the image is 800 pixels. (Width doesn't matter)
For now, I've written a simple function that takes points and returns pixels. This is a system of equations consisting of 2 equations to be exact. They are obviously linear. On a plot of 600-800 pixels, we get a density of 100 points per level. At 800-1600 we have 200 points per level.
The levels are the same.
And so, I suffered with wolframalpha for half an hour, trying to get her to find me a system of equations. It didn't work out. The problem is that in the future, if someone wants to update the picture, he will have to find a system of equations that describes this function and rewrite them in JavaScript. No matter how difficult it is, but I do not hope for followers.
Here is my solution for this particular picture.

function point2pix(pt)

{

if (pt <=800)

    return 800 - ((pt -600)/200)*266

else

    return 533 - ((pt - 800)/800)*533

}

The solution is clumsy, but working and suitable for this picture. And now the question is - the picture will be changed. The levels in this picture will also change. I would like to write an algorithm that would take the following parameters as input.
Height - the height of the image that will be used
. Levels [0,100,200,400,600,800] - an array with level boundaries and, accordingly, the number of these levels.
An array can start with an arbitrary number.
The question is how to dig and where to assemble a javascript function that will map the number of points into pixels?
(Obviously I want to do the reverse transformation too, because I'm drawing on a canvas and I have a point at 1600 = 0px a 600 = 800px, but these are small things).

Answer the question

In order to leave comments, you need to log in

3 answer(s)
C
cjey, 2012-07-16
@Nurked

General idea:
If the height in pixels for each level is fixed, then we count how many pixels one level
occupies PixPerLevel = Height / Levels.Count,
we count whole levels the ScoredLevels player has cycled through the Levels array.
then pix = PixPerLevel * ScoredLevels + tail for the last level (Tail).
The tail for the last level is also easy to calculate
Tail = PixPerLevel * (Score - Levels[ScoredLevels]) / (Levels[ScoredLevels + 1] - Levels[ScoredLevels])

J
JustLuckyGuy, 2012-07-16
@JustLuckyGuy

en.wikipedia.org/wiki/%D0%98%D0%BD%D1%82%D0%B5%D1%80%D0%BF%D0%BE%D0%BB%D1%8F%D1%86%D0 %B8%D1%8F

R
retran, 2012-07-16
@retran

Lagrange or Newton polynomials, perceptron, parametric optimization, and a whole lot more.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question