4

现在我有一个名为 Hexagon(x,y,n) 的函数,它将在 python 窗口中绘制一个以 (x,y) 为中心且边长为 n 的六边形。

我的目标是绘制一个曲面细分动画,它将从屏幕中心一个接一个地绘制六边形并一个一个展开(作为我附在此处的图片http://s7.postimage.org/lu6qqq2a3/Tes.jpg)。

我正在寻找解决这个问题的算法。刚接触编程,我发现很难做到这一点。

谢谢!

4

1 回答 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)
于 2013-02-17T19:43:54.580 回答