Draw hexagon tessellation animation in Python

Now I have a Hexagon (x, y, n) function that will draw a hexagon centered at (x, y) and with a side length n in a python window.

My goal is to draw a tessellation animation that will pull the hexagon one by one from the center of the screen and unfold one by one (as shown in the picture below http://s7.postimage.org/lu6qqq2a3/Tes.jpg ).

I am looking for an algorithm to solve this problem. New to programming and I found it difficult to do it.

Thank!

+3


source to share


1 answer


For a ring of hexagons, you can define the following function:

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)

      



Then you can swipe one ring after another:

Hexagon(0,0,10)
HexagonRing(0,0,10,1)
HexagonRing(0,0,10,2)
HexagonRing(0,0,10,3)

      

+2


source







All Articles