用python写一个,根据用户输入的长宽在画布上画出一个纵向的长方形,并在右侧水平位置画出相同大小的纵向的长方形,根据用户输入的数值确定两个长方形的左右距离,注:两个长方形的长宽分别让用户输入,定义:“右边的长方形的左下角为A点,右下角为A0点,左上角为B点,右上角为B0点;左边长方形的右上角为C点,左上角为C0点,右下角为D点,左下角为D0点,并在图上标出A、A0、B、B0、C、C0、D、D0等点和两个长方形的宽和高的数值
时间: 2023-05-11 07:02:33 浏览: 85
以下是用 Python 实现的代码:
```python
import turtle
# 获取用户输入的长和宽
width = int(input("请输入长方形的宽度:"))
height = int(input("请输入长方形的高度:"))
# 获取用户输入的左右距离
distance = int(input("请输入两个长方形的左右距离:"))
# 创建画布和画笔
canvas = turtle.Screen()
pen = turtle.Turtle()
# 画右边的长方形
pen.penup()
pen.goto(distance + width, 0)
pen.pendown()
pen.setheading(90)
pen.forward(height)
pen.right(90)
pen.forward(width)
pen.right(90)
pen.forward(height)
pen.right(90)
pen.forward(width)
# 画左边的长方形
pen.penup()
pen.goto(-distance - width, 0)
pen.pendown()
pen.setheading(90)
pen.forward(height)
pen.left(90)
pen.forward(width)
pen.left(90)
pen.forward(height)
pen.left(90)
pen.forward(width)
# 标出各个点和长宽数值
pen.penup()
pen.goto(distance + width, height)
pen.pendown()
pen.write("A", align="center")
pen.goto(distance + width, 0)
pen.write("A0", align="center")
pen.goto(distance + 2*width, height)
pen.write("B", align="center")
pen.goto(distance + 2*width, 0)
pen.write("B0", align="center")
pen.goto(-distance - width, height)
pen.write("C", align="center")
pen.goto(-distance - width, 0)
pen.write("C0", align="center")
pen.goto(-distance - 2*width, height)
pen.write("D", align="center")
pen.goto(-distance - 2*width, 0)
pen.write("D0", align="center")
pen.goto(distance + width/2, height + 20)
pen.write("宽度:" + str(width), align="center")
pen.goto(-distance - width/2, height + 20)
pen.write("宽度:" + str(width), align="center")
pen.goto(distance + 2*width + 20, height/2)
pen.write("高度:" + str(height), align="center")
pen.goto(-distance - 2*width - 20, height/2)
pen.write("高度:" + str(height), align="center")
# 隐藏画笔
pen.hideturtle()
# 显示画布
canvas.mainloop()
```
当用户输入长宽分别为 100 和 200,左右距离为 50 时,程序会画出以下图形:
![长方形图形](https://img-blog.csdnimg.cn/20220105151431257.png)
图中标出了各个点和长宽数值。
阅读全文