Post by

**zezo** | 2017-10-17 | 08:10:43

I don't have the exact code for the general case.

But the idea is following

Given tws, twa, arrays with twa and tws indexes and 2d polar array:

the indexes look like

"tws": [0, 2, 4, 5, 8, 10, 12, 14, 16, 18, 20, 22, 24,

"twa": [0, 20, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75,

You have to find the 4 bounding points that form square in the plane where the tws and twa are in the square

1) Find the tws index where the i-th speed is below the twa and i+1 is above

2) Same fort the twa

3) Do linear interpolation in one direction and then the other

Example: given TWA 43 and TWS 3

tws index is 1, twa index is 4 we have to interpolate between

polars[4][1], polars[4][2]

polars[5][1], polars[5][2]

The linear interpolation itself is like (for two data points, ws1 at tws[1] and ws2 at tws[2])

bs = bs1 + (bs2-bs1) * (tws - tws1)/*(tws2-tws1)

You calculate that two times for the two TWA cases and apply the same logic for the angle.

If in the example above numbers are

5 6

6 7

we get bs = 5.5 for twa 40 and 6.5 for twa 45 and then bs is 5.5 + 3/5

You can also solve the three interpolations analytically and get a single formula for the bi-linear interpolation. It looks like this

#define bilinear(x,y,A) ((A[1]+A[2]-A[0]-A[3])*x*y + (A[3]-A[2])*x + (A[0]-A[2]) * y + A[2])

where A holds the four corners and x and y are the fractional positions within the square, i.e. (tws - tws1)/*(tws2-tws1), (twa - twa1)/*(twa2-twa1)

There is additional twist above the maximum tws. Not sure if you get the last point or keep the slope defined by the last two points.