← 返回题库
初级

实现函数 `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()
Python 代码 🔒 登录后使用
🔒

登录后即可练习

注册免费账号,在浏览器中直接运行 Python 代码