在Python中,你可以使用诸如matplotlib
和turtle
的库来绘制多边形图案
方法1:使用matplotlib库
pip install matplotlib
import matplotlib.pyplot as plt
import numpy as np
def draw_polygon(n, radius=1, rotation=0, center=(0,0)):
angles = np.linspace(0 + rotation, 2 * np.pi + rotation, n + 1)
x = center[0] + radius * np.cos(angles)
y = center[1] + radius * np.sin(angles)
plt.plot(x, y, 'o-')
plt.gca().set_aspect('equal', adjustable='box')
plt.show()
n = 7 # 边的数量
draw_polygon(n)
这段代码将绘制一个7边形。
方法2:使用turtle库
import turtle
def draw_polygon(n, side_length=100):
angle = 360 / n
for i in range(n):
turtle.forward(side_length)
turtle.left(angle)
turtle.done()
n = 7 # 边的数量
draw_polygon(n)
这段代码将使用turtle模块绘制一个7边形。你可以根据需要调整n
和side_length
的值。
使用上述方法之一,你可以轻松地在Python中绘制多边形图案。
领取专属 10元无门槛券
手把手带您无忧上云