python
import matplotlib.pyplot as plt
# 绘制边界框的函数
def draw_boundary(boundary):
plt.plot([boundary[0][0], boundary[0][0]], [boundary[0][1], boundary[1][1]], 'k-', linewidth=2)
plt.plot([boundary[1][0], boundary[1][0]], [boundary[0][1], boundary[1][1]], 'k-', linewidth=2)
plt.plot([boundary[0][0], boundary[1][0]], [boundary[0][1], boundary[0][1]], 'k-', linewidth=2)
plt.plot([boundary[0][0], boundary[1][0]], [boundary[1][1], boundary[1][1]], 'k-', linewidth=2)
# 绘制网格的函数
def draw_grid(boundary, step=1):
for x in range(boundary[0][0], boundary[1][0]+1, step):
plt.plot([x, x], [boundary[0][1], boundary[1][1]], 'k-', linewidth=0.5)
for y in range(boundary[0][1], boundary[1][1]+1, step):
plt.plot([boundary[0][0], boundary[1][0]], [y, y], 'k-', linewidth=0.5)
# 绘制连线的函数
def draw_lines(coords, boundary, step=1):
for i in range(len(coords)-1):
if (coords[i][0] < boundary[0][0] or coords[i][0] > boundary[1][0] or
coords[i][1] < boundary[0][1] or coords[i][1] > boundary[1][1] or
coords[i+1][0] < boundary[0][0] or coords[i+1][0] > boundary[1][0] or
coords[i+1][1] < boundary[0][1] or coords[i+1][1] > boundary[1][1]):
continue
if abs(coords[i][0]-coords[i+1][0]) <= 1 and abs(coords[i][1]-coords[i+1][1]) <= 1:
plt.arrow(coords[i][0], coords[i][1], coords[i+1][0]-coords[i][0], coords[i+1][1]-coords[i][1],
length_includes_head=True, head_width=0.4, head_length=0.8, fc='b', ec='b')
plt.text((coords[i][0]+coords[i+1][0])/2, (coords[i][1]+coords[i+1][1])/2, str(i+1), fontsize=10, color='b')
# 添加首尾坐标的圆点标记
plt.plot([coords[0][0]], [coords[0][1]], 'ro')
plt.plot([coords[-1][0]], [coords[-1][1]], 'ro')
# 定义坐标和边界框
coords = [(2,2), (10,10), (18,4), (28,16), (25,19), (14,15), (5,17)]
boundary = [(0,