用Point+Graphics画虚线
时间: 2023-07-04 22:09:46 浏览: 55
使用Point+Graphics库可以通过以下步骤绘制虚线:
1. 导入Point和Graphics库:
```python
from graphics import *
```
2. 创建窗口并设置背景颜色:
```python
win = GraphWin("Dashed Line", 500, 500)
win.setBackground("white")
```
3. 设置虚线的起点和终点:
```python
start = Point(100, 100)
end = Point(400, 400)
```
4. 计算虚线的长度和间隔:
```python
line_length = ((end.getX() - start.getX()) ** 2 + (end.getY() - start.getY()) ** 2) ** 0.5
dash_length = 10
gap_length = 5
```
5. 根据虚线的长度和间隔,计算出需要绘制的虚线段数:
```python
num_dashes = int(line_length / (dash_length + gap_length))
```
6. 循环绘制虚线:
```python
for i in range(num_dashes):
p1 = Point(start.getX() + (dash_length + gap_length) * i, start.getY())
p2 = Point(p1.getX() + dash_length, p1.getY())
line = Line(p1, p2)
line.setOutline("black")
line.setDash([dash_length, gap_length])
line.draw(win)
```
7. 显示绘制结果:
```python
win.getMouse()
win.close()
```
完整代码如下:
```python
from graphics import *
win = GraphWin("Dashed Line", 500, 500)
win.setBackground("white")
start = Point(100, 100)
end = Point(400, 400)
line_length = ((end.getX() - start.getX()) ** 2 + (end.getY() - start.getY()) ** 2) ** 0.5
dash_length = 10
gap_length = 5
num_dashes = int(line_length / (dash_length + gap_length))
for i in range(num_dashes):
p1 = Point(start.getX() + (dash_length + gap_length) * i, start.getY())
p2 = Point(p1.getX() + dash_length, p1.getY())
line = Line(p1, p2)
line.setOutline("black")
line.setDash([dash_length, gap_length])
line.draw(win)
win.getMouse()
win.close()
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)