1 package aurelienribon.tweenengine.paths; 2 3 import aurelienribon.tweenengine.TweenPath; 4 5 /** 6 * @author Aurelien Ribon | http://www.aurelienribon.com/ 7 */ 8 public class CatmullRom implements TweenPath { 9 @Override compute(float t, float[] points, int pointsCnt)10 public float compute(float t, float[] points, int pointsCnt) { 11 int segment = (int) Math.floor((pointsCnt-1) * t); 12 segment = Math.max(segment, 0); 13 segment = Math.min(segment, pointsCnt-2); 14 15 t = t * (pointsCnt-1) - segment; 16 17 if (segment == 0) { 18 return catmullRomSpline(points[0], points[0], points[1], points[2], t); 19 } 20 21 if (segment == pointsCnt-2) { 22 return catmullRomSpline(points[pointsCnt-3], points[pointsCnt-2], points[pointsCnt-1], points[pointsCnt-1], t); 23 } 24 25 return catmullRomSpline(points[segment-1], points[segment], points[segment+1], points[segment+2], t); 26 } 27 catmullRomSpline(float a, float b, float c, float d, float t)28 private float catmullRomSpline(float a, float b, float c, float d, float t) { 29 float t1 = (c - a) * 0.5f; 30 float t2 = (d - b) * 0.5f; 31 32 float h1 = +2 * t * t * t - 3 * t * t + 1; 33 float h2 = -2 * t * t * t + 3 * t * t; 34 float h3 = t * t * t - 2 * t * t + t; 35 float h4 = t * t * t - t * t; 36 37 return b * h1 + c * h2 + t1 * h3 + t2 * h4; 38 } 39 } 40