/// <summary> /// Converts an unbounded time to a time within the curve's interval using the /// endpoint behavior. /// </summary> /// <param name="time">Time to convert.</param> /// <param name="intervalBegin">Beginning of the curve's interval.</param> /// <param name="intervalEnd">End of the curve's interval.</param> /// <param name="preLoop">Looping behavior of the curve before the first endpoint's time.</param> /// <param name="postLoop">Looping behavior of the curve after the last endpoint's time.</param> /// <returns>Time within the curve's interval.</returns> public static double ModifyTime(double time, double intervalBegin, double intervalEnd, CurveEndpointBehavior preLoop, CurveEndpointBehavior postLoop) { if (time < intervalBegin) { switch (preLoop) { case CurveEndpointBehavior.Wrap: double modifiedTime = time - intervalBegin; double intervalLength = intervalEnd - intervalBegin; modifiedTime %= intervalLength; return(intervalEnd + modifiedTime); case CurveEndpointBehavior.Clamp: return(Math.Max(intervalBegin, time)); case CurveEndpointBehavior.Mirror: modifiedTime = time - intervalBegin; intervalLength = intervalEnd - intervalBegin; int numFlips = (int)(modifiedTime / intervalLength); if (numFlips % 2 == 0) { return(intervalBegin - modifiedTime % intervalLength); } return(intervalEnd + modifiedTime % intervalLength); } } else if (time >= intervalEnd) { switch (postLoop) { case CurveEndpointBehavior.Wrap: double modifiedTime = time - intervalEnd; double intervalLength = intervalEnd - intervalBegin; modifiedTime %= intervalLength; return(intervalBegin + modifiedTime); case CurveEndpointBehavior.Clamp: return(Math.Min(intervalEnd, time)); case CurveEndpointBehavior.Mirror: modifiedTime = time - intervalEnd; intervalLength = intervalEnd - intervalBegin; int numFlips = (int)(modifiedTime / intervalLength); if (numFlips % 2 == 0) { return(intervalEnd - modifiedTime % intervalLength); } return(intervalBegin + modifiedTime % intervalLength); } } return(time); }
/// <summary> /// Converts an unbounded time to a time within the curve's interval using the /// endpoint behavior. /// </summary> /// <param name="time">Time to convert.</param> /// <param name="intervalBegin">Beginning of the curve's interval.</param> /// <param name="intervalEnd">End of the curve's interval.</param> /// <param name="preLoop">Looping behavior of the curve before the first endpoint's time.</param> /// <param name="postLoop">Looping behavior of the curve after the last endpoint's time.</param> /// <returns>Time within the curve's interval.</returns> public static Fix64 ModifyTime(Fix64 time, Fix64 intervalBegin, Fix64 intervalEnd, CurveEndpointBehavior preLoop, CurveEndpointBehavior postLoop) { if (time < intervalBegin) { switch (preLoop) { case CurveEndpointBehavior.Wrap: Fix64 modifiedTime = time - intervalBegin; Fix64 intervalLength = intervalEnd - intervalBegin; modifiedTime %= intervalLength; return(intervalEnd + modifiedTime); case CurveEndpointBehavior.Clamp: return(MathHelper.Max(intervalBegin, time)); case CurveEndpointBehavior.Mirror: modifiedTime = time - intervalBegin; intervalLength = intervalEnd - intervalBegin; var numFlips = (int)(modifiedTime / intervalLength); if (numFlips % 2 == 0) { return(intervalBegin - modifiedTime % intervalLength); } return(intervalEnd + modifiedTime % intervalLength); } } else if (time >= intervalEnd) { switch (postLoop) { case CurveEndpointBehavior.Wrap: Fix64 modifiedTime = time - intervalEnd; Fix64 intervalLength = intervalEnd - intervalBegin; modifiedTime %= intervalLength; return(intervalBegin + modifiedTime); case CurveEndpointBehavior.Clamp: return(MathHelper.Min(intervalEnd, time)); case CurveEndpointBehavior.Mirror: modifiedTime = time - intervalEnd; intervalLength = intervalEnd - intervalBegin; var numFlips = (int)(modifiedTime / intervalLength); if (numFlips % 2 == 0) { return(intervalEnd - modifiedTime % intervalLength); } return(intervalBegin + modifiedTime % intervalLength); } } return(time); }