使用Python绘制六边形平铺动画

4
现在我有一个名为Hexagon(x,y,n)的函数,它会在Python窗口中以(x,y)为中心、n为边长绘制一个六边形。
我的目标是绘制一个图案动画,从屏幕中心开始逐个绘制六边形,并逐个扩散(就像我附上的图片http://s7.postimage.org/lu6qqq2a3/Tes.jpg所示)。
我正在寻找解决这个问题的算法。作为编程新手,我发现这很难。
谢谢!
1个回答

2

对于一个六边形环,可以定义以下函数:

def HexagonRing(x,y,n,r):
    dc = n*math.sqrt(3) # distance between to neighbouring hexagon centers
    xc,yc = x,y-r*dc # hexagon center of one before first hexagon (=last hexagon)
    dx,dy = -dc*math.sqrt(3)/2,dc/2 # direction vector to next hexagon center
    for i in range(0,6):
        # draw r hexagons in line
        for j in range(0,r):
            xc,yc = xc+dx,yc+dy
            Hexagon(xc,yc,n)
        # rotate direction vector by 60°
        dx,dy = (math.cos(math.pi/3)*dx+math.sin(math.pi/3)*dy,
               -math.sin(math.pi/3)*dx+math.cos(math.pi/3)*dy)

然后可以一个接一个地画出各个环:
Hexagon(0,0,10)
HexagonRing(0,0,10,1)
HexagonRing(0,0,10,2)
HexagonRing(0,0,10,3)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接