python绘制一个八卦图
时间: 2023-07-04 12:19:40 浏览: 210
用python绘制一个八卦图
好的,以下是Python绘制八卦图的代码:
```python
import turtle
# 创建画布和画笔
canvas = turtle.Screen()
pen = turtle.Turtle()
# 绘制圆形
def draw_circle(color, radius, x, y):
pen.penup()
pen.fillcolor(color)
pen.goto(x, y)
pen.pendown()
pen.begin_fill()
pen.circle(radius)
pen.end_fill()
# 绘制半圆形
def draw_half_circle(color, radius, x, y):
pen.penup()
pen.goto(x, y)
pen.pendown()
pen.color(color)
pen.circle(radius, 180)
# 绘制八卦图
def draw_ba_gua():
draw_circle("black", 100, 0, 0)
draw_half_circle("white", 50, 0, 100)
draw_half_circle("black", 50, 0, -100)
draw_circle("white", 10, 0, 50)
draw_circle("black", 10, 0, -50)
draw_half_circle("white", 50, 0, 0)
draw_half_circle("black", 50, 0, 0)
# 调用绘制函数
draw_ba_gua()
# 隐藏画笔
pen.hideturtle()
# 显示画布
canvas.mainloop()
```
运行后可以看到绘制出来的八卦图。
阅读全文