Your task is to take an array of numbers and a real number and return the value at that point in the array. Arrays start at \$\pi\$ and are counted in \$\pi\$ intervals. Thing is, we're actually going to interpolate between elements given the "index". As an example:
Index: 1π 2π 3π 4π 5π 6π
Array: [ 1.1, 1.3, 6.9, 4.2, 1.3, 3.7 ]
Because it's \$\pi\$, we have to do the obligatory trigonometry, so we'll be using cosine interpolation using the following formula:
\${\cos(i \mod \pi) + 1 \over 2} * (\alpha - \beta) + \beta\$
where:
- \$i\$ is the input "index"
- \$\alpha\$ is the value of the element immediately before the "index"
- \$\beta\$ is the value of the element immediately after the "index"
- \$\cos\$ takes its angle in radians
Example
Given [1.3, 3.7, 6.9], 5.3:
Index 5.3 is between \$1\pi\$ and \$2\pi\$, so 1.3 will be used for before
and 3.7 will be used for after
. Putting it into the formula, we get:
\${\cos(5.3 \mod \pi) + 1 \over 2} * (1.3 - 3.7) + 3.7\$
Which comes out to 3.165
Notes
- Input and output may be in any convenient format
- You may assume the input number is greater than \$\pi\$ and less than
array length
* \$\pi\$ - You may assume the input array will be at least 2 elements long.
- Your result must have at least two decimal points of precision, be accurate to within 0.05, and support numbers up to 100 for this precision/accuracy. (single-precision floats are more than sufficient to meet this requirement)
Happy Golfing!