Search code examples
algorithmanimationpython-2.7hexagonal-tiles

Draw a hexagon tessellation animation in Python


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

My goal is to draw a tessellation animation which will draw the hexagon one after another from the center of the screen and spread out one by one (As the picture I attached here http://s7.postimage.org/lu6qqq2a3/Tes.jpg).

I am looking for the algorithm solving this problem. New to programing and I found it hard to do that.

Thanks!


Solution

  • For a ring of hexagons one can define a function like this:

    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 one can draw one ring after the other:

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