一种快速收缩地图边界方法
一种快速收缩地图边界方法
在制作地图后,有时需要向内收缩地图地图边界,例如左右边界需要向内收缩30cm。这里提供了一种快速收缩地图的方法。先放一张收缩后的效果图。
这里以sin(x)作为地图的右边界,sin(x)+1作为地图右边界,左右边界都需要向内收缩30cm。
方法如下:
- 1、假设右边界连续AB两点,A点向内收缩Dist的距离,为C点。
- 2、计算AB线段的斜率k
- 3、获取AB的倾斜角&
- 4、计算C点的坐标
具体计算公式如下:
A(x0, y0) B(x1, y1)
k = (y1 - y2)/(x1 - x2)
& = atan2(k)
向右缩
x0 + Dist * sin(&)
y0 - Dist * cos(&)
向左缩
x0 - Dist * sin(&)
y0 + Dist * cos(&)
最后,上干货,代码实现:
def move_boundary(x, y, flag, dist):
size = x.shape[0]
result_X = []
result_Y = []
for i in range(size - 1):
x_1 = x[i]
y_1 = y[i]
x_2 = x[i + 1]
y_2 = y[i + 1]
angle = np.arctan2(y_1 - y_2, x_1 - x_2)
# move left boundary
if flag == 0:
x1_ = x_1 - dist * np.sin(angle)
y1_ = y_1 + dist * np.cos(angle)
# move right boundary
elif flag == 1:
x1_ = x_1 + dist * np.sin(angle)
y1_ = y_1 - dist * np.cos(angle)
result_X.append(x1_)
result_Y.append(y1_)
return result_X, result_Y
x = np.linspace(1, 100, 100)
y1= np.sin(x)
y2 = np.sin(x) + 1
plt.plot(x, y1, 'b', label = 'sin(x)')
plt.plot(x, y2, 'g', label = 'sin(x) + 1')
result_X, result_Y = move_boundary(x, y1, 1, 0.2)
plt.plot(result_X, result_Y, 'r', label='move right')
result_X, result_Y = move_boundary(x, y2, 0, 0.2)
plt.plot(result_X, result_Y, 'c', label='move left')
plt.xlim(0, 20)
plt.legend()
plt.show()