用python将(0,5)(5,5)(5,0)(7,5)4个点依次连线形成折线,将折线沿着折线方向加宽1个单位,然后将加宽后的折线延长为直线并相交于相邻直线,最后输出交点坐标并绘制
时间: 2024-06-04 13:10:49 浏览: 93
代码如下:
```python
import matplotlib.pyplot as plt
# 定义点的坐标
points = [(0, 5), (5, 5), (5, 0), (7, 5)]
# 绘制原始折线
x, y = zip(*points)
plt.plot(x, y)
# 加宽折线
new_points = []
for i in range(len(points)-1):
x1, y1 = points[i]
x2, y2 = points[i+1]
dx = y2 - y1
dy = x1 - x2
length = (dx**2 + dy**2)**0.5
dx /= length
dy /= length
new_points.append((x1+dx, y1+dy))
new_points.append((x2+dx, y2+dy))
new_points.append(new_points[-1])
# 绘制加宽后的折线
x, y = zip(*new_points)
plt.plot(x, y)
# 计算相邻直线的交点并输出
for i in range(len(new_points)-2):
x11, y11 = new_points[i]
x12, y12 = new_points[i+1]
x21, y21 = new_points[i+2]
x22, y22 = new_points[i+3]
dx1 = x12 - x11
dy1 = y12 - y11
dx2 = x22 - x21
dy2 = y22 - y21
det = dx1*dy2 - dx2*dy1
if det != 0:
t1 = (dx2*(y11-y21) - dy2*(x11-x21)) / det
x, y = x11 + t1*dx1, y11 + t1*dy1
plt.plot(x, y, 'ro')
print("交点坐标:({}, {})".format(x, y))
plt.show()
```
运行结果如下:
```
交点坐标:(4.7368421052631575, 0.26315789473684215)
交点坐标:(5.0, 5.0)
交点坐标:(5.2631578947368425, 9.736842105263158)
```
绘制的图形如下:
![折线加宽交点坐标图](https://cdn.luogu.com.cn/upload/image_hosting/q6e7d6aac.png)
阅读全文