Animated construction of the Dragon curve

The most known method to draw a Dragon curve is by using turtle graphics. Here we implement a method visually illustrated in a video posted by Numberphile: https://www.youtube.com/watch?v=NajQEiKFom4. We are starting with a vertical segment and the successive rotations are counterclockwise.


In [1]:
import numpy as np
from numpy import pi
import plotly.graph_objects as go

In [2]:
def rot_matrix(alpha):
#Define the matrix of rotation about origin with an angle of alpha radians:
    return np.array([[np.cos(alpha), -np.sin(alpha)], 
                     [np.sin(alpha), np.cos(alpha)]])

def rotate_dragon(x, y, alpha=pi/2):
    #x,y  lists or 1D-array containng the (x, y)-coordinates of the turn points on the dragon curve constructed 
    # in a single step
    X, Y = rot_matrix(alpha).dot(np.stack((x, y))) # the lists of coordinates of turn points on the rotated curve
    return X, Y

In [3]:
#the initial step dragon cuvre is represented by a vertical line of length L
L = 0.12
X = np.array([0, 0])
Y = np.array([-L, 0])

fig =  go.Figure(data=[go.Scatter(x=X,y=Y, 
                                  mode='lines', 
                                  line_color='#0000ee',
                                  line_width=1.5,
                                  showlegend=False)
                    ])
title =  "Animated construction of the Dragon curve,<br>through successive rotations" 
fig.update_layout(title_text=title, title_x=0.5,
                  font=dict(family='Balto', size=16),
                  width=700, height=700,
                  xaxis_visible=False, 
                  yaxis_visible=False,
                 
                  xaxis_range=[-11, 6],
                  yaxis_range=[-11, 3],
                  #margin_l=40,
                 );

The frame 0 displays the initial vertical segment, as the dragon cuve defined in step 0 of the iterative process of construction.


In [4]:
alpha = pi/10 # The rotation of 90 degrees is defined as 5 successive rotations of 18 degrees=pi10 radians
n_rot90 = 13 # we have 13 steps
frames = []

for k in range(n_rot90):
    #Record the last point on the dragon, defined in the previous step
    x0, y0 = X[-1], Y[-1]
    x = X-x0  #Translation with origin at (x0, y0)  to be the center of rotation
    y = Y-y0
    for j in range(5): 
        X, Y = rotate_dragon(x, y, alpha=(j+1)*alpha)
        X = np.concatenate((x[:-1], X[::-1]), axis=None) #concatenate to the (k-1)^th step dragon  its rotated version
        Y = np.concatenate((y[:-1], Y[::-1]), axis=None)
        X = X+x0
        Y = Y+y0
        frames.append(go.Frame(data=[go.Scatter(x=X,y=Y)],
                               traces=[0]))

Define a button that triggers the animation:


In [5]:
buttonPlay = {'args': [None, 
                     {'frame': {'duration': 100,
                                'redraw': False}, 
                                'transition': {'duration': 0}, 
                                'fromcurrent': True,
                                'mode': 'immediate'}],
                                'label': 'Play',
                                'method': 'animate'}

In [6]:
fig.update_layout(updatemenus=[{'buttons': [buttonPlay],
                                'showactive': False,
                                'type': 'buttons',
                                'x': 1,
                                'xanchor': 'left',
                                'y': 1,
                                'yanchor': 'top'
                                }])

 

fig.frames=frames

In [7]:
import chart_studio.plotly as py
py.iplot(fig, filename='rot-dragon1')


Out[7]:

A gif file derived from this animation is posted on Wikimedia.


In [ ]: