← 返回题库
初级

绘制100个不重叠的恐龙图像

未完成
初级参考 完整示例代码供参考,建议自己理解后重新输入
def solve():
    import matplotlib.pyplot as plt
    
    dino_vectors = [(6,4), (3,1), (1,2), (-1,5), (-2,5), (-3,4), (-4,4),
                    (-5,3), (-5,2), (-2,2), (-5,1), (-4,0), (-2,1), (-1,0), (0,-3),
                    (-1,-4), (1,-4), (2,-3), (1,-2), (3,-1), (5,1)]
    
    def add(v1, v2):
        return (v1[0] + v2[0], v1[1] + v2[1])
    
    def translate(translation, vectors):
        return [add(translation, v) for v in vectors]
    
    plt.figure(figsize=(12, 10))
    
    for i, x in enumerate(range(-5, 5)):
        for j, y in enumerate(range(-5, 5)):
            translation = (12*x, 10*y)
            translated = translate(translation, dino_vectors)
            tx = [v[0] for v in translated] + [translated[0][0]]
            ty = [v[1] for v in translated] + [translated[0][1]]
            plt.plot(tx, ty, 'b-', linewidth=0.5)
    
    plt.axhline(y=0, color='k', linewidth=0.5)
    plt.axvline(x=0, color='k', linewidth=0.5)
    plt.grid(alpha=0.3)
    plt.title('100个恐龙图像')
    plt.xlabel('x')
    plt.ylabel('y')
    plt.axis('equal')
    plt.show()
Python 代码 🔒 登录后使用
🔒

登录后即可练习

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