用python写一个,根据用户输入的长宽在画布上画出一个纵向的长方形,并在右侧水平位置画出相同大小的纵向的长方形,根据用户输入的数值确定两个长方形的左右距离,注:两个长方形的长宽分别让用户输入,定义:“右边的长方形的左下角为A点,右下角为A0点,左上角为B点,右上角为B0点;左边长方形的右上角为C点,左上角为C0点,右下角为D点,左下角为D0点,并在图上标出A、A0、B、B0、C、C0、D、D0等点,在图上标出两个长方形的宽和高的数值
时间: 2023-05-11 21:02:40 浏览: 58
以下是用 Python 实现的代码:
```python
import turtle
# 获取用户输入的长和宽
width = int(input("请输入长方形的宽度:"))
height = int(input("请输入长方形的高度:"))
# 获取用户输入的左右距离
distance = int(input("请输入两个长方形的左右距离:"))
# 初始化画布
canvas = turtle.Screen()
canvas.setup(width=800, height=600)
# 初始化画笔
pen = turtle.Turtle()
pen.speed(0)
pen.hideturtle()
# 画出左边的长方形
pen.penup()
pen.goto(-distance - width, height)
pen.pendown()
pen.goto(-distance - width, 0)
pen.goto(-distance, 0)
pen.goto(-distance, height)
pen.goto(-distance - width, height)
# 画出右边的长方形
pen.penup()
pen.goto(distance, height)
pen.pendown()
pen.goto(distance, 0)
pen.goto(distance + width, 0)
pen.goto(distance + width, height)
pen.goto(distance, height)
# 标出各个点的位置
pen.penup()
pen.goto(-distance - width, height)
pen.dot()
pen.write("A", align="right")
pen.goto(-distance - width, 0)
pen.dot()
pen.write("D0", align="right")
pen.goto(-distance, 0)
pen.dot()
pen.write("C0", align="left")
pen.goto(-distance, height)
pen.dot()
pen.write("B", align="left")
pen.goto(distance, height)
pen.dot()
pen.write("B0", align="right")
pen.goto(distance, 0)
pen.dot()
pen.write("C", align="left")
pen.goto(distance + width, 0)
pen.dot()
pen.write("D", align="left")
pen.goto(distance + width, height)
pen.dot()
pen.write("A0", align="left")
# 标出长和宽的数值
pen.penup()
pen.goto(-distance - width / 2, height / 2)
pen.write("宽:" + str(width), align="center")
pen.goto(-distance - width, height / 2 - 20)
pen.write("高:" + str(height), align="right")
pen.goto(distance + width / 2, height / 2)
pen.write("宽:" + str(width), align="center")
pen.goto(distance + width, height / 2 - 20)
pen.write("高:" + str(height), align="left")
# 显示画布
canvas.mainloop()
```
当用户输入长为 100、宽为 50、左右距离为 200 时,画出的图形如下:
```
A0 B0
●----●
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
●----● A
D0 C0 |
●----● |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
●----● D
C B0
```
其中,左边的长方形的左上角为点 B,右下角为点 D0,右边的长方形的左下角为点 A,右上角为点 B0。左边长方形的宽为 100,高为 50,右边长方形的宽和高也都为 100。
阅读全文