绘制太极八卦图Python
时间: 2024-05-27 11:06:52 浏览: 222
你可以使用Python的turtle库来绘制太极八卦图。具体步骤如下:
1. 导入turtle库
```python
import turtle
```
2. 创建画布和画笔
```python
window = turtle.Screen()
pen = turtle.Turtle()
```
3. 设置画笔的颜色和大小
```python
pen.color('black')
pen.pensize(3)
```
4. 绘制圆形和半圆形
```python
# 绘制黑色半圆
pen.begin_fill()
pen.fillcolor('black')
pen.circle(100, 180)
pen.end_fill()
# 绘制白色半圆
pen.begin_fill()
pen.fillcolor('white')
pen.circle(100, -180)
pen.end_fill()
# 绘制黑色小圆
pen.penup()
pen.goto(0, 50)
pen.pendown()
pen.begin_fill()
pen.fillcolor('black')
pen.circle(25)
pen.end_fill()
# 绘制白色小圆
pen.penup()
pen.goto(0, -50)
pen.pendown()
pen.begin_fill()
pen.fillcolor('white')
pen.circle(25)
pen.end_fill()
```
5. 绘制两个小圆内部的阴阳鱼
```python
# 绘制黑色阴鱼
pen.penup()
pen.goto(0, 25)
pen.pendown()
pen.color('white', 'black')
pen.begin_fill()
pen.circle(-25, 180)
pen.circle(-12.5, -180)
pen.left(90)
pen.forward(50)
pen.end_fill()
# 绘制白色阳鱼
pen.penup()
pen.goto(0, -25)
pen.pendown()
pen.color('black', 'white')
pen.begin_fill()
pen.circle(25, 180)
pen.circle(12.5, -180)
pen.left(90)
pen.forward(-50)
pen.end_fill()
```
6. 隐藏画笔
```python
pen.hideturtle()
```
完整代码如下:
```python
import turtle
window = turtle.Screen()
pen = turtle.Turtle()
pen.color('black')
pen.pensize(3)
# 绘制黑色半圆
pen.begin_fill()
pen.fillcolor('black')
pen.circle(100, 180)
pen.end_fill()
# 绘制白色半圆
pen.begin_fill()
pen.fillcolor('white')
pen.circle(100, -180)
pen.end_fill()
# 绘制黑色小圆
pen.penup()
pen.goto(0, 50)
pen.pendown()
pen.begin_fill()
pen.fillcolor('black')
pen.circle(25)
pen.end_fill()
# 绘制白色小圆
pen.penup()
pen.goto(0, -50)
pen.pendown()
pen.begin_fill()
pen.fillcolor('white')
pen.circle(25)
pen.end_fill()
# 绘制黑色阴鱼
pen.penup()
pen.goto(0, 25)
pen.pendown()
pen.color('white', 'black')
pen.begin_fill()
pen.circle(-25, 180)
pen.circle(-12.5, -180)
pen.left(90)
pen.forward(50)
pen.end_fill()
# 绘制白色阳鱼
pen.penup()
pen.goto(0, -25)
pen.pendown()
pen.color('black', 'white')
pen.begin_fill()
pen.circle(25, 180)
pen.circle(12.5, -180)
pen.left(90)
pen.forward(-50)
pen.end_fill()
# 隐藏画笔
pen.hideturtle()
window.mainloop()
```
阅读全文