{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## Animated construction of the Dragon curve" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The most known method to draw a Dragon curve is by using turtle graphics. Here we implement a method visually illustrated \n", " in a video posted by [Numberphile](https://en.wikipedia.org/wiki/Numberphile): \n", " [https://www.youtube.com/watch?v=NajQEiKFom4](https://www.youtube.com/watch?v=NajQEiKFom4).\n", "We are starting with a vertical segment and the successive rotations are counterclockwise. " ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "from numpy import pi\n", "import plotly.graph_objects as go" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "def rot_matrix(alpha):\n", "#Define the matrix of rotation about origin with an angle of alpha radians:\n", " return np.array([[np.cos(alpha), -np.sin(alpha)], \n", " [np.sin(alpha), np.cos(alpha)]])\n", "\n", "def rotate_dragon(x, y, alpha=pi/2):\n", " #x,y lists or 1D-array containng the (x, y)-coordinates of the turn points on the dragon curve constructed \n", " # in a single step\n", " X, Y = rot_matrix(alpha).dot(np.stack((x, y))) # the lists of coordinates of turn points on the rotated curve\n", " return X, Y\n" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "#the initial step dragon cuvre is represented by a vertical line of length L\n", "L = 0.12\n", "X = np.array([0, 0])\n", "Y = np.array([-L, 0])\n", "\n", "fig = go.Figure(data=[go.Scatter(x=X,y=Y, \n", " mode='lines', \n", " line_color='#0000ee',\n", " line_width=1.5,\n", " showlegend=False)\n", " ])\n", "title = \"Animated construction of the Dragon curve,
through successive rotations\" \n", "fig.update_layout(title_text=title, title_x=0.5,\n", " font=dict(family='Balto', size=16),\n", " width=700, height=700,\n", " xaxis_visible=False, \n", " yaxis_visible=False,\n", " \n", " xaxis_range=[-11, 6],\n", " yaxis_range=[-11, 3],\n", " #margin_l=40,\n", " );" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The frame 0 displays the initial vertical segment, as the dragon cuve defined in step 0 of the iterative \n", "process of construction." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "alpha = pi/10 # The rotation of 90 degrees is defined as 5 successive rotations of 18 degrees=pi10 radians\n", "n_rot90 = 13 # we have 13 steps\n", "frames = []\n", "\n", "for k in range(n_rot90):\n", " #Record the last point on the dragon, defined in the previous step\n", " x0, y0 = X[-1], Y[-1]\n", " x = X-x0 #Translation with origin at (x0, y0) to be the center of rotation\n", " y = Y-y0\n", " for j in range(5): \n", " X, Y = rotate_dragon(x, y, alpha=(j+1)*alpha)\n", " X = np.concatenate((x[:-1], X[::-1]), axis=None) #concatenate to the (k-1)^th step dragon its rotated version\n", " Y = np.concatenate((y[:-1], Y[::-1]), axis=None)\n", " X = X+x0\n", " Y = Y+y0\n", " frames.append(go.Frame(data=[go.Scatter(x=X,y=Y)],\n", " traces=[0]))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Define a button that triggers the animation:" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "buttonPlay = {'args': [None, \n", " {'frame': {'duration': 100,\n", " 'redraw': False}, \n", " 'transition': {'duration': 0}, \n", " 'fromcurrent': True,\n", " 'mode': 'immediate'}],\n", " 'label': 'Play',\n", " 'method': 'animate'}" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "fig.update_layout(updatemenus=[{'buttons': [buttonPlay],\n", " 'showactive': False,\n", " 'type': 'buttons',\n", " 'x': 1,\n", " 'xanchor': 'left',\n", " 'y': 1,\n", " 'yanchor': 'top'\n", " }])\n", "\n", " \n", "\n", "fig.frames=frames" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import chart_studio.plotly as py\n", "py.iplot(fig, filename='rot-dragon1')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A gif file derived from this animation is posted on [Wikimedia](https://commons.wikimedia.org/wiki/File:Animated_Dragon_construction.gif#/media/File:Animated_Dragon_construction.gif)." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.3" } }, "nbformat": 4, "nbformat_minor": 2 }