algorithm - 用Python画一个六边形镶嵌动画

标签 algorithm animation python-2.7 hexagonal-tiles

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

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

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

谢谢!

最佳答案

对于一圈六边形,可以定义如下函数:

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)

关于algorithm - 用Python画一个六边形镶嵌动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14916941/

相关文章:

c# - 所有案例都涵盖了 Bresenham 的线算法

c# - 使用 C# 创建 3D 矩形

algorithm - 在查找二叉树是否平衡时难以理解递归

animation - SwiftUI - 按钮在 View 的左上角不起作用

jquery - 带有 CSS 和 jQuery 的动画信封

html - 延迟特定的变换属性 CSS

algorithm - 找到连续的子序列,使得数字总和的立方体最多为 y

python-2.7 - Flask wtform forms.validate_on_submit() 总是假的

python - Django 分组依据变量

python - 在 double_scalars 中遇到除以零进行导数计算