- 新增 jiyi.py 文件,实现字母翻牌记忆游戏功能 - 添加 youxijiemian.py 文件,创建游戏开始界面 - 使用 turtle 和 tkinter 模块分别实现游戏和界面 - 支持选择不同难度的游戏模式
54 lines
1.5 KiB
Python
54 lines
1.5 KiB
Python
import numpy as np
|
||
import matplotlib.pyplot as plt
|
||
import matplotlib.animation as animation
|
||
|
||
# 圆的半径
|
||
radius = 1.0
|
||
init_n = 6
|
||
max_frame = 10 # 最多动画帧数
|
||
|
||
# 创建图形
|
||
fig, ax = plt.subplots(figsize=(7, 7))
|
||
ax.set_aspect('equal')
|
||
ax.set_xlim(-1.3, 1.3)
|
||
ax.set_ylim(-1.3, 1.3)
|
||
ax.set_title("割圆法动画演示:多边形逼近圆周率 π", fontsize=14)
|
||
|
||
# 添加单位圆
|
||
circle = plt.Circle((0, 0), radius, fill=False, linestyle='--', color='gray')
|
||
ax.add_patch(circle)
|
||
|
||
# 多边形对象和文本对象
|
||
polygon_line, = ax.plot([], [], 'b-', linewidth=2)
|
||
info_text = ax.text(-1.25, 1.18, '', fontsize=11, color='darkred', fontfamily='monospace')
|
||
formula_text = ax.text(-1.25, -1.2, '', fontsize=10, color='black', fontfamily='monospace')
|
||
|
||
def generate_polygon(n):
|
||
"""生成正n边形的顶点"""
|
||
angles = np.linspace(0, 2 * np.pi, n + 1)
|
||
x = radius * np.cos(angles)
|
||
y = radius * np.sin(angles)
|
||
return x, y
|
||
|
||
def update(frame):
|
||
n = init_n * (2 ** frame)
|
||
x, y = generate_polygon(n)
|
||
polygon_line.set_data(x, y)
|
||
|
||
# π近似计算:π ≈ n × sin(π / n)
|
||
pi_approx = n * np.sin(np.pi / n)
|
||
|
||
info_text.set_text(f"边数: {n:5d} π ≈ {pi_approx:.10f}")
|
||
|
||
# 显示公式推导
|
||
formula_text.set_text(
|
||
"π ≈ n × sin(π / n) (r = 1)\n"
|
||
"推导:L = n × s = 2n × sin(π / n) → π ≈ L / 2\n"
|
||
)
|
||
|
||
return polygon_line, info_text, formula_text
|
||
|
||
# 动画制作
|
||
ani = animation.FuncAnimation(fig, update, frames=max_frame, interval=1000, blit=True)
|
||
plt.show()
|