初级
实现函数 `regular_polygon(n)`, 返回一个规则 $n$ 边形的各顶点的笛卡尔坐标
未完成
初级参考
完整示例代码供参考,建议自己理解后重新输入
def solve():
import matplotlib.pyplot as plt
import numpy as np
from math import sin, cos, pi
def regular_polygon(n):
return [(cos(2*pi*i/n), sin(2*pi*i/n)) for i in range(n)]
fig, axes = plt.subplots(2, 3, figsize=(12, 8))
for idx, n in enumerate([3, 4, 5, 6, 8, 10]):
ax = axes[idx // 3, idx % 3]
vertices = regular_polygon(n)
vertices.append(vertices[0])
x = [v[0] for v in vertices]
y = [v[1] for v in vertices]
ax.plot(x, y, 'b-', linewidth=2)
ax.scatter(x[:-1], y[:-1], color='red', s=50)
ax.set_title(f'正{n}边形')
ax.set_xlim(-1.5, 1.5)
ax.set_ylim(-1.5, 1.5)
ax.set_aspect('equal')
ax.grid(alpha=0.3)
plt.tight_layout()
plt.show()
👑
升级 VIP
解锁全部题目,畅通无阻地学习
- ✓ 解锁全部训练包所有题目
- ✓ 查看完整参考代码和提示
- ✓ 浏览器内直接运行 Python 代码
- ✓ 自动批改 + 进度追踪
30天
¥18
1年
¥99
2年
¥158
3年
¥199