Reputation: 3205
I have a class with three arbitrary float
values stored in it. Let's call them A B and C. The class has a method that will take an input float in the range of -1 to +1 and then return a value based on the input.
calling the method with value -1 will result in the value of A, calling it with 0 will result in B, calling it with 1 will result in C. The question now is, how can i interpolate an output for everything in between?
I was thinking of an imaginary curve/spline based on three points defined as -1xA 0xB and 1xC. With a function that can plot this curve, providing output for any float in the range can be calculated.
NB:
Normally I would post some code here to illustrate what i have so far, but as I don't really know how to attack this problem I do not see any point in posting just an empty function definition.
PS:
If someone is curious why, I am using input from a controller to define the angle of a joint, so the output value will never be outside the range of -180 to +180
Upvotes: 1
Views: 858
Reputation:
Piecewise linear interpolation respects the constraints but is only C0 continuous.
You can achieve C1 continuity with piecewise parabolic interpolation (y = A x(x-1)/2 + B (x+1)(1-x) + C x(x+1)/2
), but you must ensure that the two pieces are monotonous (no maximum, except possibly at the endpoints).
First try parabolic interpolation. The derivative is linear (y' = A (2x-1)/2 + B 2x + C (2x+1)/2)
). If the signs of the derivative at -1 and +1 are the same, you are done. Otherwise, use the solution of 3. @sds.
Upvotes: 1
Reputation: 60004
The simplest answer is a piece-wise linear function as in the other answer.
However, a comment says "Any kind of smooth curve that doesn't result in pointy mountaintops or narrow pits".
The second simplest answer is to use the unique parabola which goes through the 3 points (quadratic interpolation) but that violates the requirement that all the values must be between min(A,B,C)
and max(A,B,C)
.
So we are stuck with the 3rd simplest answer: use a piece-wise quadratic function with 0 derivative at 0:
f(x) = B + (C-B) * x^2 when x is in [0,1]
B + (A-B) * x^2 when x is in [-1,0]
Upvotes: 2
Reputation: 16677
how about just finding the midpoint between the appropriate two points?
given i as input between -1 and 1:
if i < 0 then point.x = (a.x+b.x)/2 and point.y = (a.y+b.y)/2
if i > 0 then point.x = (b.x+c.x)/2 and point.y = (b.y+c.y)/2
Upvotes: 0
Reputation: 2123
Let's define a piecewise linear function using the three points (-1, A), (0, B), (1, C)
f(x) = (B-A)(x+1)+A if x in [-1, 0)
= (C-B)x+B if x in [0, 1]
(and if someone knows TeX and want to make that pretty by all means)...
there are some pretty fancy interpolation methods for smooth functions, by making f(x) differentiable at x = B, but it's been a while since my numerical analysis class...
Upvotes: 1