This time only a quicky, but it will be used in a later post. A small class to calculate a Bezier curve interpolation in 3D or 2D. Nothing special about it.
public class Bezier { public static Vector3 Interpolate(Vector3 start, Vector3 end, Vector3 controlPointA, Vector3 controlPointB, float t) { float tInv = 1 - t; float tSqr = t * t; float tInvSrq = tInv * tInv; return tInv * tInvSrq * start + 3 * t * tInvSrq * controlPointA + 3 * tInv * tSqr * controlPointB + t * tSqr * end; } public static Vector2 Interpolate(Vector2 start, Vector2 end, Vector2 controlPointA, Vector2 controlPointB, float t) { float tInv = 1 - t; float tSqr = t * t; float tInvSqr = tInv * tInv; return tInv * tInvSqr * start + 3 * t * tInvSqr * controlPointA + 3 * tInv * tSqr * controlPointB + t * tSqr * end; } public static float Length(Vector3 start, Vector3 end, Vector3 controlPointA, Vector3 controlPointB, int steps) { float length = 0; Vector3 fst = start; for (int i = 1; i <= steps; ++i) { Vector3 snd = Interpolate(start, end, controlPointA, controlPointB, (float) i / steps); length += (snd - fst).magnitude; fst = snd; } return length; } public static float Length(Vector2 start, Vector2 end, Vector2 controlPointA, Vector2 controlPointB, int steps) { float length = 0; Vector2 fst = start; for (int i = 1; i <= steps; ++i) { Vector2 snd = Interpolate(start, end, controlPointA, controlPointB, (float) i / steps); length += (snd - fst).magnitude; fst = snd; } return length; } }
Comments
Trackbacks