def barycentric_trapezoidial_interpolation(Nx,Ny,p,hexagonalOffset=0.5):
'''
define our function to calculate the position of points from Nx columns and Ny rows.
The vertices are defined by p which is a size(4,2) array.
each row of p are the coordinates or the vertices of our trapezoid
the vertices have to be given in a specific order:
[[1 1]
[1 2]
[2 1]
[2 2]]
an example plot using the barycentric interpolation to regrid data
define number of rows and number of columns and the vertices, then make some plots
Example:
Nx = 20
Ny = 15
coords = [[0,0],[0,1],[1,0],[1,1]] #these are the [x,y] coords of your 4 draggable corners
coords = np.asarray(coords)
f, ax = plt.subplots(2, 2) # sharey=True, sharex=True)
for i,a in enumerate(ax.flatten()):
newCoords = coords[:]
if i > 0:
newCoords = newCoords + np.random.rand(4,2) / 5
xi,yi = barycentric_trapezoidial_interpolation(Nx,Ny,newCoords)
a.plot(xi,yi,'.',markersize=12)
plt.show()
'''
x_basis = np.linspace(0,1,Nx)
y_basis = np.linspace(0,1,Ny)
px = [[p[0,0], p[2,0]],[p[1,0], p[3,0]]] #these are the [2,2] x-coordinates
py = [[p[0,1], p[2,1]],[p[1,1], p[3,1]]] #these are the [2,2] y-coordinates
fx = interpolate.interp2d([0,1], [0,1], px, kind='linear')
xi = fx(x_basis[:],y_basis[:]).flatten()
fy = interpolate.interp2d([0,1], [0,1], py, kind='linear')
yi = fy(x_basis[:],y_basis[:]).flatten()
d1 = (p[2,0] - p[0,0]) / Nx / 2.0
d2 = (p[3,0] - p[1,0]) / Nx / 2.0
offset = (d1 + d2) * hexagonalOffset
#every other row will be shifted in diff(x) * hexagonalOffset
for i in range(0,len(xi)-Nx,Nx*2):
for j in range(Nx):
xi[i+j+Nx] += offset
return xi,yi
评论列表
文章目录