同一窗口用turtle库同时绘制等边三角形、叠加等边三角形、无角正方形、六角形。
时间: 2023-05-22 22:04:47 浏览: 125
Python的turtle库还能绘制这些有趣图形?
可以使用以下代码绘制:
```python
import turtle
# 绘制等边三角形
def draw_equilateral_triangle(size):
for i in range(3):
turtle.forward(size)
turtle.left(120)
# 绘制叠加等边三角形
def draw_stacked_equilateral_triangles(size):
turtle.penup()
turtle.forward(size/2)
turtle.left(60)
turtle.pendown()
for i in range(3):
turtle.forward(size/2)
turtle.right(120)
turtle.penup()
turtle.backward(size/2)
turtle.right(60)
turtle.pendown()
for i in range(3):
turtle.forward(size/2)
turtle.left(120)
# 绘制无角正方形
def draw_square(size):
for i in range(4):
turtle.forward(size)
turtle.left(90)
# 绘制六边形
def draw_hexagon(size):
for i in range(6):
turtle.forward(size)
turtle.left(60)
# 画布设置
turtle.setup(width=800,height=600)
turtle.speed(0)
turtle.pensize(3)
# 绘制图形
draw_equilateral_triangle(200)
draw_stacked_equilateral_triangles(150)
draw_square(100)
draw_hexagon(100)
# 关闭画布
turtle.done()
```
不要忘记执行上述代码后打开 Turtle 窗口才能看到绘制结果。
阅读全文