Class Mapping
java.lang.Object
cit244.util.Mapping
- All Implemented Interfaces:
Curve
An immutable mapping from one number range to another, optionally
shaped by curves along the way.
apply(double) works in three steps:
- Normalize: find how far the input is through the input range (0 at the start, 1 at the end).
- Shape: run that progress through the curves added with
through(Curve), in the order they were added. - 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.
-
Field Summary
Fields inherited from interface cit244.util.Curve
EASE_IN_CUBIC, EASE_IN_QUAD, LINEAR, PINGPONG, REPEAT, SINE, SMOOTHSTEP -
Method Summary
Modifier and TypeMethodDescriptiondoubleapply(double value) Maps a value.clamped()Keeps the result between the two output endpoints.static Mappingfrom(double start, double end) Starts a mapping with the given input range and an output range of [0, 1].Adds a curve that shapes progress before it is scaled to the output range.to(double start, double end) Sets the output range.
-
Method Details
-
from
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 startend- input range end- Returns:
- a linear, unclamped mapping
-
to
Sets the output range. The endpoints may be reversed.- Parameters:
start- output range startend- output range end- Returns:
- a mapping with the new output range
-
through
-
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 likeMathf.inverseLerp(double, double, double).
-