A response curve is an easy-to-define piece-wise linear curve. They are described by Bob Alexander in AI Programming Wisdom in the article The Beauty of Response Curves. Below is an example:
The curve above can be defined as follows:
var inputs = new List{0, 20, 40, 60, 100}; var outputs = new List {10, 30, 110, 140, 100}; var curve = new ResponseCurveFloat(inputs, output);
You can then sample this curve at any point:
float value = curve[30f]; //value is 70
Note that input samples need not be evenly spaced, and that you can sample the curve at any floating point value. The curve is clamped at the ends, so in the example above, curve[150f]
would give the value 100.
The Extensions library provides response curves for several types:
ResponseCurveFloat
ResponseCurveVector2
ResponseCurveVector3
ResponseCurveVector4
ResponseCurveColor
(Our Colors library also contains a ResponseCurveHSLColor
).
Response curves have many applications. They can be used for:
- lookup tables for function approximations
- approximating smooth curves
- defining paths in 2D or 3D space
- defining color gradients
- implementing arbitrary probability distributions
It is also trivial to implement Response Curves for your own types, by following these steps:
- Extend your class
ResponseCurveMyType
fromResponseCurveBase
. - Implement the abstract method
Lerp
to provide a linear interpolation ofMyType
.
That is it!