4

I'm trying to come up with a flexible decaying score system for a game using quadratic curves. I could probably brute force my way through it but was wondering if anyone can help me come up with something flexible or maybe there are some ready made solutions out there already!

But basically I need the ability to generate the values of a,b & c in:

y = ax^2 + bx + c

from 3 points (which i know fall on a valid quadratic curve, but are dynamic based on configurable settings and maximum times to react to an event) for example: (-1100, 0), (200, 1), (1500, 0).

So I can then plugin in values for x to generate values of Y which will determine the score I give the user.

If I could get away with a fixed quadratic equation I would but the scoring is based on how much time a user has to react to a particular event (X Axis) the y axis points will always be between 0 and 1 with 0 being minimum score and 1 being maximum score!

Let me know if you need more info!

Tristan
  • 3,845
  • 5
  • 35
  • 58

2 Answers2

10

You can use Lagrange polynomial interpolation, the curve is given by

enter image description here

y(x) = y_1 * (x-x_2)*(x-x_3)/((x_1-x_2)*(x_1-x_3))
     + y_2 * (x-x_1)*(x-x_3)/((x_2-x_1)*(x_2-x_3))
     + y_3 * (x-x_1)*(x-x_2)/((x_3-x_1)*(x_3-x_2))

If you collect the coefficients, you obtain

a = y_1/((x_1-x_2)*(x_1-x_3)) + y_2/((x_2-x_1)*(x_2-x_3)) + y_3/((x_3-x_1)*(x_3-x_2))

b = -y_1*(x_2+x_3)/((x_1-x_2)*(x_1-x_3))
    -y_2*(x_1+x_3)/((x_2-x_1)*(x_2-x_3))
    -y_3*(x_1+x_2)/((x_3-x_1)*(x_3-x_2))

c = y_1*x_2*x_3/((x_1-x_2)*(x_1-x_3))
  + y_2*x_1*x_3/((x_2-x_1)*(x_2-x_3))
  + y_3*x_1*x_2/((x_3-x_1)*(x_3-x_2))
henry
  • 4,244
  • 2
  • 26
  • 37
Daniel Fischer
  • 181,706
  • 17
  • 308
  • 431
  • This works beautifully! There was a slight typo that I fixed but this will fit my needs! Cheers – Tristan Jun 03 '13 at 13:17
  • Hi Daniel, looking at your first curve y(x). How do I get x knowing y? – fmalina Aug 27 '13 at 18:49
  • 1
    @linebreak Do you mean finding an `x` so that `y = ax^2 + bx + c` with known coefficients `a,b,c` and a known `y`? – Daniel Fischer Aug 27 '13 at 18:52
  • Yes, what form would `x(y) = ...` take knowing `(x_1, y_1)`, `(x_2, y_2)` and `(x_3, y_3)`? – fmalina Aug 27 '13 at 18:59
  • 1
    If `a != 0`, you get `(y-c)/a + b^2/(2a)^2 = (x+b/(2a))^2`, and from that `x = (-b ± sqrt(4a(y-c)+b^2))/(2a)`. If `a == 0` and `b != 0`, you get `x = (y-c)/b`, and if `a == b == 0`, you get either nothing (if `y != c`) or `x` arbitrary (if `y == c`). – Daniel Fischer Aug 27 '13 at 19:08
  • @linebreak I didn't use any vectors. The `x_i` and `y_i` are the coordinates of the points used for the interpolation. Can you state more precisely which problem you try to solve? – Daniel Fischer Aug 27 '13 at 19:19
  • My case is `a == 0` and `b != 0`. How do I write `x = (y-c)/b` using coordinates like you wrote in your function `y(x)` above? – fmalina Aug 27 '13 at 19:24
  • Sorry, @linebreak, I don't understand. What data do you start from, and what do you want to achieve? – Daniel Fischer Aug 27 '13 at 19:27
  • `x_1, y_1 = 0, 0`; `x_2, y_2 = 8, 10`; `x_3, y_3 = 30, 300`; Knowing `y`, for example `y = 150`, how do I work out `x`? – fmalina Aug 27 '13 at 19:28
  • @linebreak And `y` is a function of `x`, a quadratic polynomial, and you want to find the `x` value(s) for a given `y`? – Daniel Fischer Aug 27 '13 at 19:32
  • `y` is a function of `x` and I want to find the `x` value(s) for a given `y`. – fmalina Aug 27 '13 at 19:34
  • @linebreak Okay, so compute `a,b,c` by the above formulae, getting `a = 35/88`, `b = -85/44`, `c = 0`. Solving the quadratic for `x` then gives `x = (17 ± sqrt(616y/5 + 289))/7`. For `y = 150`, the values are `x = (17 ± sqrt(18769))/7 = (17 ± 137)/7`, `x = 22` or `x = -120/7`. – Daniel Fischer Aug 27 '13 at 19:45
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/36358/discussion-between-line-break-and-daniel-fischer) – fmalina Aug 27 '13 at 19:47
0

you can formulate it in a matrix form: aX=b

    1 x1 x1^2
a=  1 x2 x2^2
    1 x3 x3^2

   y1
b= y2
   y3

then solve by inverting the matrix a (can be done via gauss method pretty straight forward) http://en.wikipedia.org/wiki/Gaussian_elimination

X = a^-1*b

and X in this case are the [c b a] coefficients your are looking for.