Class Mapping

java.lang.Object
cit244.util.Mapping
All Implemented Interfaces:
Curve

public final class Mapping extends Object implements Curve
An immutable mapping from one number range to another, optionally shaped by curves along the way.

apply(double) works in three steps:

  1. Normalize: find how far the input is through the input range (0 at the start, 1 at the end).
  2. Shape: run that progress through the curves added with through(Curve), in the order they were added.
  3. Scale: interpolate the shaped progress across the output range.

A mapping is itself a Curve, so it can go anywhere a curve can, including the end of a Curve.then(Curve) chain.


 // Mouse position to a slider, never leaving the slider:
 Mapping mouseToX = Mapping.from(0, width).to(100, 500).clamped();

 // A two-second slide that stays put once it arrives:
 Mapping slideX = Mapping.from(0, 2).through(Curve.SMOOTHSTEP).to(100, 500);

 // One eased bounce every two seconds, between y = 100 and y = 300:
 Mapping bobY = Mapping.from(0, 2)
         .through(Curve.PINGPONG)
         .through(Curve.SMOOTHSTEP)
         .to(100, 300);
 double y = bobY.apply(seconds);
 

For repeating curves, the input range sets the period: from(0, 2) means one cycle every 2 input units.

Note the difference between through and then: through shapes progress before it is scaled to the output range, while then (inherited from Curve) runs on the final output value. To ease a mapping, use through.

Configuration methods return a new mapping and never change the original.

  • Method Details

    • from

      public static Mapping from(double start, double end)
      Starts a mapping with the given input range and an output range of [0, 1]. The input range may run backward.
      Parameters:
      start - input range start
      end - input range end
      Returns:
      a linear, unclamped mapping
    • to

      public Mapping to(double start, double end)
      Sets the output range. The endpoints may be reversed.
      Parameters:
      start - output range start
      end - output range end
      Returns:
      a mapping with the new output range
    • through

      public Mapping through(Curve next)
      Adds a curve that shapes progress before it is scaled to the output range. Curves run in the order they were added.
      Parameters:
      next - curve to apply after any existing curves
      Returns:
      a mapping with the extra curve
    • clamped

      public Mapping clamped()
      Keeps the result between the two output endpoints.

      Clamping happens once, after all curves have run, so repeating curves still repeat. A plain linear mapping that is clamped behaves like Mathf.mapClamped(double, double, double, double, double).

      Returns:
      a clamped mapping
    • apply

      public double apply(double value)
      Maps a value. A zero-width input range counts as progress 0, just like Mathf.inverseLerp(double, double, double).
      Specified by:
      apply in interface Curve
      Parameters:
      value - value in (or outside) the input range
      Returns:
      mapped value