title: Oscillate or "ping pong" between two values?
body: <p>I have a path that is evaluate at time 't' and returns an orientation and position based on the path type.</p>
  
  <p>The value for time is affected by the path type:</p>
  
  <pre><code>switch (type)
  {
      case PathType.Closed:
          time = ToolBox.Wrap(time, StartTime, EndTime);
          break; // Wrap time around the path time to loop
  
      case PathType.Open:
          time = ToolBox.Min(time, EndTime);
          break; // Clamp the time value to the max path time range
  
      case PathType.Oscillating:
          break;
  }
  </code></pre>
  
  <p>The missing link is oscillating.</p>
  
  <p>My question is what is a good, efficient way for oscillating between two values?  </p>
  
  <p>For example (2, 7).  If time reaches 7 it reverses and decrements towards to 2 and once it reaches 2 it reverses and increases towards 7.</p>
  
  <p>The algorithm should know whether to increase/decrease the value based on the original value so if the value is 9 it knows the answer is 7 - (Abs(7 - 9).  If the value is 14 the value has wrapped around so it will result in an increase of 1.  </p>
  
  <p>Higher values will also increase or decrease the value depending on the number of times it wraps around the original range.</p>
  
  <p>I hope that makes sense as I'm finding it difficult to explain.</p>
  
  <p>EDIT:</p>
  
  <p>Doesn't oscillate with floating point values:</p>
  
  <pre><code>        for (float i = 0; i &lt; 100; i += 0.1f)
          {
              Console.WriteLine("{0} {1}", i, Oscillate(2.5f, 7.5f, i));
          }
  
      private float Oscillate(float min, float max, float value)
      {
          float range = max - min;
  
          float multiple = value / range;
  
          bool ascending = multiple % 2 == 0;
          float modulus = value % range;
  
          return ascending ? modulus + min : max - modulus;
      }
  </code></pre>