38 lines
768 B
Plaintext
38 lines
768 B
Plaintext
|
|
module Interpolator;
|
|
|
|
import std.io.*;
|
|
|
|
typedef function double (double) interpolator_t;
|
|
|
|
int main(args : String[])
|
|
{
|
|
interpolator_t i1 = getInterpolator(1, 5, 4);
|
|
interpolator_t i2 = getInterpolator(10, 2, 5);
|
|
|
|
assert( i1(0) =~ 1 );
|
|
assert( i1(4) =~ 5 );
|
|
assert( i1(3) =~ 4.0 );
|
|
|
|
assert( i2(0) =~ 10 );
|
|
assert( i2(5) =~ 2 );
|
|
assert( i2(1.0) =~ 8.4 );
|
|
assert( i2(2.0) =~ 6.8 );
|
|
|
|
return 0;
|
|
}
|
|
|
|
/*
|
|
* Create and return a linear interpolator functor which interpolates
|
|
* between (0.0, x1) and (sep, x2), returning the output y-value
|
|
*/
|
|
interpolator_t getInterpolator(x1 : double, x2 : double, sep : double)
|
|
{
|
|
assert (sep > 0.0);
|
|
|
|
return function double (x : double)
|
|
{
|
|
return x * (x2 - x1) / sep + x1;
|
|
}
|
|
}
|