InterpolatingMap2D
A 2D map that supports flexible interpolation between Double values.
InterpolatingMap2D is a wrapper around a 2D grid of TreeMap<Double, TreeMap<Double, Double>> that interpolates values between stored keys using a custom interpolation strategy. Exact matches return stored values; intermediate keys are interpolated using the configured strategy.
Interpolation Strategies
Custom interpolation functions accept four corner values and blend parameters and return an interpolated result: (q00: Double, q10: Double, q01: Double, q11: Double, tx: Double, ty: Double) -> Double
where:
\(q_{00}, q_{10}, q_{01}, q_{11}\) are the values at corners \((x_0, y_0), (x_1, y_0), (x_0, y_1), (x_1, y_1)\)
\(t_x = \frac{x - x_0}{x_1 - x_0}\) and \(t_y = \frac{y - y_0}{y_1 - y_0}\) are blend parameters in \(0, 1\)
Bilinear Interpolation (Default)
The default interpolation uses bilinear blending:
\(q(x, y) = (1 - t_x)(1 - t_y) \cdot q_{00} + t_x(1 - t_y) \cdot q_{10} + (1 - t_x) t_y \cdot q_{01} + t_x t_y \cdot q_{11}\)
Usage Examples
Basic 2D bilinear interpolation:
val map = InterpolatingMap2D.bilinear()
map[0.0, 0.0] = 0.0
map[1.0, 0.0] = 1.0
map[0.0, 1.0] = 2.0
map[1.0, 1.0] = 3.0
println(map[0.5, 0.5]) // 1.5 (bilinearly interpolated)With lookup table:
val xKeys = listOf(0.0, 1.0, 2.0)
val yKeys = listOf(0.0, 1.0, 2.0)
val values = listOf(
listOf(0.0, 1.0, 2.0),
listOf(1.0, 2.0, 3.0),
listOf(2.0, 3.0, 4.0)
)
val map = InterpolatingMap2D.bilinear(xKeys, yKeys, values)
println(map[0.5, 0.5]) // Interpolated valueThrows
if yKeys.size != values.size or if any row has inconsistent column count