根据你的描述,我假设
field_x = field[:,:,0] * np.sin(field[:,:,1])
field_y = field[:,:,1] * np.cos(field[:,:,1])
-
如果您想绕原点旋转而不进行平移,只需将旋转角度添加到field[:,:,1]。
-
当您想对仿射形式的所有点应用相同的变换时,旋转矩阵很有用。 (你有一个极地表示)。
-
我不清楚你究竟想如何进行转换,所以我会给你一个函数,它围绕给定的枢轴旋转点然后平移。
要围绕给定枢轴平移,您必须首先将旋转中心平移到原点,应用旋转,然后将旋转中心平移回原始位置。
def translate(x, y, tx, ty):
return (x + tx, y + ty)
def rotate(x, y, angle):
c = np.cos(angle)
s = np.sin(angle)
return x * c - y * s, x * s + y * c
def polar2cartesian(field):
c = np.sin(field[:,:,1])
s = np.cos(field[:,:,1])
r = field[:,:,0]
return r*c, r*s
def cartesian2polar(x, y):
result = np.empty(x.shape + (2,))
result[:,:,0] = np.sqrt(x**2 + y**2)
result[:,:,1] = np.arctan2(y, x);
return result;
def transform(field1, pivot, angle, translation):
x,y = polar2cartesian(field1)
px,py = polar2cartesian(pivot)
tx,ty = polar2cartesian(translation)
# move the center of rotation to the origin
x, y = translate(x, y, -px, -py);
x, y = rotate(x, y, angle)
x, y = translate(x, y, tx, ty)
return cartesian2polar(x, y)
为了说明使用,我将使用一些 1000 x 1 的场,具有恒定的半径和恒定的旋转。
r = np.linspace(0, 2*np.pi, 1000)
field1 = np.zeros((1000, 1, 2))
field2 = np.zeros((1000, 1, 2))
field3 = np.zeros((1000, 1, 2))
plt.figure(figsize=(5, 5))
R = 10;
field1[:,0,0] = 1
field1[:,0,1] = 0
field2[:,0,0] = R+1
field2[:,0,1] = r
field3[:,0,0] = 0
field3[:,0,1] = R*r;
field4[:,0,0] = R;
field4[:,0,1] = r
plt.plot(*polar2cartesian(transform(field1, field3, field3[:,:,1], field4)))
field4[:,0,0] = R+2
field3[:,0,1] = -(R+2)*r + np.pi
plt.plot(*polar2cartesian(transform(field1, field3, field3[:,:,1], field4)))
plt.plot(*polar2cartesian(field2))
解释 第一个图是一个半径为R+1的圆,第二个是半径为一个的圆内一个点在半径为R+1的圆内滚动的轨迹,第三个图是半径为一的圆内点在半径圆外滚动的轨迹R+1