编程中三色圆该怎么画的

时间:2025-01-28 23:26:57 网络游戏

使用Python的PIL (Pillow) 库

```python

from PIL import Image, ImageDraw

def draw_circle(screen_center, color, radius):

img = Image.new('RGB', (600, 600), 'white')

draw = ImageDraw.Draw(img)

draw.ellipse((screen_center - radius, screen_center - radius, screen_center + radius, screen_center + radius), fill=color)

return img

屏幕中心坐标

center = (300, 300)

颜色列表

colors = ('red', 'green', 'blue')

半径列表

radii = [30, 40, 50]

img = draw_circle(center, colors, radii)

img.save('red_circle.png')

img = draw_circle(center, colors, radii)

img.save('green_circle.png')

img = draw_circle(center, colors, radii)

img.save('blue_circle.png')

```

使用turtle库

```python

import turtle

color = ['red', 'pink', 'green']

ra = [20, 50, 100]

for i in range(3):

turtle.pu()

turtle.goto(0, -ra[i])

turtle.pd()

turtle.pencolor(color[i])

turtle.circle(ra[i])

turtle.done()

```

使用matplotlib库

```python

import matplotlib.pyplot as plt

fig, ax = plt.subplots()

circle1 = plt.Circle((0.2, 0.5), 0.1, color='r')

ax.add_artist(circle1)

circle2 = plt.Circle((0.5, 0.5), 0.2, color='g')

ax.add_artist(circle2)

circle3 = plt.Circle((0.8, 0.5), 0.15, color='b')

ax.add_artist(circle3)

ax.set_xlim([0, 1])

ax.set_ylim([0, 1])

ax.set_aspect('equal')

ax.set_xlabel('X')

ax.set_ylabel('Y')

plt.show()

```

这些示例展示了如何使用Python的PIL库、turtle库和matplotlib库来绘制三个不同颜色的圆。你可以根据自己的需求和编程环境选择合适的方法。