# 割圆术动画_需要最新sprites版本

### 割圆术动画_需要最新sprites版本

```import time
import math
import random
import turtle
from sprites import *

def rndcolor():
r = random.randint(0,255)
g = random.randint(0,255)
b = random.randint(0,255)
return r,g,b

def draw_polygon(turtle,cors):
d = math.dist(cors[0],cors[1])
turtle.penup()
turtle.goto(cors[0])
turtle.pendown()
for x,y in cors[1:]:
turtle.goto(x,y)
turtle.goto(cors[0])
turtle.penup()
周长 = d * len(cors)
return 周长

turtle.colormode(255)

screen = Screen()
screen.setup(500,500)
screen.bgcolor('black')
screen.tracer(0,0)

A = (-200,0)
B =(200,0)
pen = Sprite('blank')
pen.width(2)
pen.color('white')
pen.draw_circle(A,B)

ft = ('黑体',16,'normal')
p = Sprite('blank')        # 写圆周率的
p.sety(220)
p.pencolor('yellow')

frames = []
t = Sprite('blank')
for n in range(3,101):
t.home()
t.fd(200)
t.left(90)
points = []
for i in range(n):
t.circle(200,360/n)
points.append(t.position())
t.randomcolor()
周长 = draw_polygon(t,points)
pi = 周长/400
p.clear()
p.write(f'n={n},π={pi}',align='center',font=ft)
screen.update()
frames.append(screen.save())
t.wait(2/n)
#t.clear()
for index,frame in enumerate(frames):
frame.save(f'gframes/{index}.png')

screen.mainloop()

```