Get rid of a couple of multiplies

This commit is contained in:
David Rowe 2016-01-05 15:23:25 +13:00
parent a265ffb631
commit 16d4234d55

View file

@ -64,31 +64,31 @@ float interpolate3Points(float y1, float y2, float y3, float u) {
return y2;
}
float slope;
float halfSlope;
if ((y2 >= y1 && y2 >= y3) || (y2 <= y1 && y2 <= y3)) {
// U or inverted-U shape.
// Make the slope at y2 = 0, which means that the control points half way between the value points have the value y2.
slope = 0.0f;
halfSlope = 0.0f;
} else {
// L or inverted and/or mirrored L shape.
// Make the slope at y2 be the slope between y1 and y3, up to a maximum of double the minimum of the slopes between y1
// and y2, and y2 and y3. Use this slope to calculate the control points half way between the value points.
// Note: The maximum ensures that the control points and therefore the interpolated values stay between y1 and y3.
float slope = y3 - y1;
float doubleSlope12 = 2.0f * (y2 - y1);
float doubleSlope23 = 2.0f * (y3 - y2);
if (abs(slope) > abs(doubleSlope12)) {
slope = doubleSlope12;
} else if (abs(slope) > abs(doubleSlope23)) {
slope = doubleSlope23;
halfSlope = (y3 - y1) / 2.0f;
float slope12 = y2 - y1;
float slope23 = y3 - y2;
if (abs(halfSlope) > abs(slope12)) {
halfSlope = slope12;
} else if (abs(halfSlope) > abs(slope23)) {
halfSlope = slope23;
}
}
float stepU = step(0.5f, u); // 0.0 if u < 0.5, 1.0 otherwise.
float slopeSign = 2.0f * stepU - 1.0f; // -1.0 if u < 0.5, 1.0 otherwise
float start = (1.0f - stepU) * y1 + stepU * y2; // y1 if u < 0.5, y2 otherwise
float middle = y2 + slopeSign * slope / 2.0f;
float middle = y2 + slopeSign * halfSlope;
float finish = (1.0f - stepU) * y2 + stepU * y3; // y2 if u < 0.5, y3 otherwise
float v = 2.0f * u - step(0.5f, u); // 0.0-0.5 -> 0.0-1.0 and 0.5-1.0 -> 0.0-1.0
return bezierInterpolate(start, middle, finish, v);