Last active
January 4, 2016 07:39
-
-
Save qtip/8590325 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package cubic; | |
public class Cubic { | |
public static final float cubic(float x, float... v) { | |
if (x <= v[0]) { | |
return v[1]; | |
} | |
x = Math.min(Math.max(v[0], x), v[v.length - 2]); | |
int i; | |
for (i = 0; i < v.length && v[i + 6] < x; i += 6); | |
float left = 0.0f; | |
float right = 1.0f; | |
float t = 0.5f; | |
for (int n = 0; n < 15; n++) { | |
float v1 = left*left*left*v[i+6] - (left*left*left - 3.0f*left*left + 3.0f*left - 1)*v[i] + 3.0f*(left*left*left - 2.0f*left*left + left)*v[i+2] - 3.0f*(left*left*left - left*left)*v[i+4]; | |
float v2 = t*t*t*v[i+6] - (t*t*t - 3.0f*t*t + 3.0f*t - 1)*v[i] + 3.0f*(t*t*t - 2.0f*t*t + t)*v[i+2] - 3.0f*(t*t*t - t*t)*v[i+4]; | |
right = v1 < x != v2 < x ? t : right; | |
left = v1 < x != v2 < x ? left : t; | |
t = (right - left) / 2.0f + left; | |
} | |
return t*t*t*v[i + 7] - (t*t*t - 3.0f*t*t + 3.0f*t - 1)*v[i + 1] + 3.0f*(t*t*t - 2.0f*t*t + t)*v[i + 3] - 3.0f*(t*t*t - t*t)*v[i + 5]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment