namespace Urho { internal static class SplineMath { // CatmullRom Spline formula: /// /// See http://en.wikipedia.org/wiki/Cubic_Hermite_spline#Cardinal_spline /// /// Control point 1 /// Control point 2 /// Control point 3 /// Control point 4 /// The parameter c is a tension parameter that must be in the interval (0,1). In some sense, this can be interpreted as the "length" of the tangent. c=1 will yield all zero tangents, and c=0 yields a Catmull–Rom spline. /// Time along the spline /// The point along the spline for the given time (t) internal static Vector2 CardinalSplineAt(Vector2 p0, Vector2 p1, Vector2 p2, Vector2 p3, float tension, float t) { if (tension < 0f) { tension = 0f; } if (tension > 1f) { tension = 1f; } float t2 = t * t; float t3 = t2 * t; /* * Formula: s(-ttt + 2tt - t)P1 + s(-ttt + tt)P2 + (2ttt - 3tt + 1)P2 + s(ttt - 2tt + t)P3 + (-2ttt + 3tt)P3 + s(ttt - tt)P4 */ float s = (1 - tension) / 2; float b1 = s * ((-t3 + (2 * t2)) - t); // s(-t3 + 2 t2 - t)P1 float b2 = s * (-t3 + t2) + (2 * t3 - 3 * t2 + 1); // s(-t3 + t2)P2 + (2 t3 - 3 t2 + 1)P2 float b3 = s * (t3 - 2 * t2 + t) + (-2 * t3 + 3 * t2); // s(t3 - 2 t2 + t)P3 + (-2 t3 + 3 t2)P3 float b4 = s * (t3 - t2); // s(t3 - t2)P4 float x = (p0.X * b1 + p1.X * b2 + p2.X * b3 + p3.X * b4); float y = (p0.Y * b1 + p1.Y * b2 + p2.Y * b3 + p3.Y * b4); return new Vector2(x, y); } // Bezier cubic formula: // ((1 - t) + t)3 = 1 // Expands to // (1 - t)3 + 3t(1-t)2 + 3t2(1 - t) + t3 = 1 internal static float CubicBezier(float a, float b, float c, float d, float t) { float t1 = 1f - t; return ((t1 * t1 * t1) * a + 3f * t * (t1 * t1) * b + 3f * (t * t) * (t1) * c + (t * t * t) * d); } internal static float QuadBezier(float a, float b, float c, float t) { float t1 = 1f - t; return (t1 * t1) * a + 2.0f * (t1) * t * b + (t * t) * c; } } }