Skip to content

Instantly share code, notes, and snippets.

@mirrornerror
Last active February 18, 2022 12:28
Show Gist options
  • Save mirrornerror/7c9f7d6cb3026dc4557eb7f600c07ad5 to your computer and use it in GitHub Desktop.
Save mirrornerror/7c9f7d6cb3026dc4557eb7f600c07ad5 to your computer and use it in GitHub Desktop.
Hilbert_curve.ipynb
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"metadata": {},
"cell_type": "markdown",
"source": "# Hilbert Curve Drawing \n#### Order = 1, 2, 3 ... (4 dots, 16 dots, 64 dots ...)  \n\n[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](http://colab.research.google.com/gist/mirrornerror/7c9f7d6cb3026dc4557eb7f600c07ad5/Hilbert_curve.ipynb) "
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2022-02-18T12:27:59.239566Z",
"end_time": "2022-02-18T12:27:59.999513Z"
},
"trusted": true
},
"cell_type": "code",
"source": "from numpy import array, arange\nimport matplotlib.pyplot as plt\nfrom matplotlib import animation\nfrom IPython.display import HTML, display",
"execution_count": 1,
"outputs": []
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2022-02-18T12:28:01.181454Z",
"end_time": "2022-02-18T12:28:01.225781Z"
},
"trusted": true
},
"cell_type": "code",
"source": "# Plot function\ndef Plot(X, Y, outline=False, num=False, dot=False):\n N = int((len(X))**0.5)\n fig = plt.figure(figsize=(8,8))\n ax = fig.add_subplot(111)\n ax.axis([0, N, 0, N])\n \n if outline == False:\n ax.axis('off')\n ax.plot(X, Y, color='tab:blue')\n if num:\n for i in range(len(X)):\n ax.text(X[i]+0.02, Y[i]+0.02, s=str(i))\n if dot:\n ax.scatter(X, Y, color='tab:blue')\n ax.grid()\n plt.xticks(arange(0, N+1, max(1, N//4)))\n plt.yticks(arange(0, N+1, max(1, N//4)))\n plt.show()\n plt.close()\n\n# Animation function\ndef Anim(X, Y, outline=False, save=False):\n N = int((len(X))**0.5)\n fig = plt.figure(figsize=(8,8))\n ax = fig.add_subplot(111)\n ax.axis([0, N, 0, N])\n ax.grid()\n plt.xticks(arange(0, N+1, max(1, N//4)))\n plt.yticks(arange(0, N+1, max(1, N//4)))\n if outline == False:\n ax.axis('off') \n ims = []\n for i in range(len(X) + 1): \n A = ax.plot(X[:i], Y[:i], color='tab:blue')\n ims.append(A)\n ani = animation.ArtistAnimation(fig,ims,interval=100)\n if save:\n ani.save('anim.gif', writer = 'pillow') # writer='Imagemagick', 'ffmpeg', Windows for 'pillow'\n #HTML(ani.to_html5_video()) \n display(HTML(ani.to_jshtml()))\n plt.close()",
"execution_count": 2,
"outputs": []
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2022-02-18T12:28:03.882607Z",
"end_time": "2022-02-18T12:28:03.902515Z"
},
"trusted": true
},
"cell_type": "code",
"source": "# Hilbert curve function\ndef Hilbert(ORDER): # ORDER 1: 4 dots, ORDER 2: 16 dots, ORDER 3: 64 dots ...\n N = 2**ORDER # the number of row or columns of the dots\n X,Y = [], [] # empty lists for X and Y coodinates outputs\n for i in range(N * N): # iteration: the total number of the dots\n U = [[0, 0], [0, 1], [1, 1], [1, 0]] # initial four dots coodinates: origin, up, right, down\n px, py = 0.5, 0.5 # starting coodinates (initial offsets)\n for j in reversed(range(ORDER)): # reversed iteration: j=2,1,0 when ORDER=3\n ID = i // 4**j % 4 # index number depending on ORDER\n L = 2**j # unit length under ORDER: L=4, 2, 1 when ORDER=3\n if ID == 0:\n U[1], U[3] = U[3], U[1] # swap 1st with 3rd dots when ID is 0\n elif ID == 3:\n U[0], U[2] = U[2], U[0] # swap 0th with 2nd dots when ID is 3\n px += U[ID][0] * L # add each length depending on ORDER\n py += U[ID][1] * L\n X.append(px) # preserve (x, y) coodinates of the dots\n Y.append(py)\n return X, Y",
"execution_count": 3,
"outputs": []
},
{
"metadata": {},
"cell_type": "markdown",
"source": "## Draw Hilvert Curve"
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2022-02-18T12:28:19.265054Z",
"end_time": "2022-02-18T12:28:19.335951Z"
},
"trusted": true
},
"cell_type": "code",
"source": "# Draw Hilbert curve\nX, Y = Hilbert(4) # argument: Order from 1 to n\nPlot(X, Y) # arguments: X, Y, outline=False, num=False, dot=False\n#Anim(X, Y) # arguments: X, Y, outline=False, save=False",
"execution_count": 5,
"outputs": [
{
"output_type": "display_data",
"data": {
"text/plain": "<Figure size 576x576 with 1 Axes>",
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAcwAAAHBCAYAAADkRYtYAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjQuMywgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/MnkTPAAAACXBIWXMAAAsTAAALEwEAmpwYAAANhklEQVR4nO3ZvXUjSZqG0a/20BCYECZQhwcwgPKIMAHiyjQAHkCnCWkCPMEIBHdP9/RUv5iJ/FDMulehFoH4ST7Igx+3260AgJ/7n2d/AAD4DgQTAAKCCQABwQSAgGACQEAwASAgmAAQEEwACAgmAAQEEwACggkAAcEEgIBgAkBAMAEgIJgAEBBMAAgIJgAEBBMAAoIJAIGX2QPujpe3qjrMHvdvnK+n/XvHRNa3ii2vb8trq7K+aaxvFVPXt8Yb5qGqxgrj/jujeg/B+uYatd31jdru2qqsbzbrm2vU5PVNf8O8W66n/etKY//B7nj56JjnT6xvki2vb8trq7K+lVjfJGusz2+YABAQTAAICCYABAQTAAKCCQABwQSAgGACQEAwASAgmAAQEEwACAgmAAQEEwACggkAAcEEgIBgAkBAMAEgIJgAEBBMAAgIJgAEBBMAAoIJAAHBBICAYAJAQDABIPDy7A8wydgdLx9dc1XV0jTX78L5fV/O7ntzfg/YQjDPzfMtT5hzy5zf9+Xsvjfn96BvH8zraf9eVe/P/hz8Z5zf9+Xsvjfn9zi/YQJAQDABICCYABAQTAAICCYABAQTAAKCCQABwQSAgGACQEAwASAgmAAQEEwACAgmAAQEEwACggkAAcEEgIBgAkBAMAEgIJgAEBBMAAgIJgAEBBMAAoIJAAHBBIDAy0rjjt3x8rHS2H/lf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BBICAYAJAQDABICCYABAQTAAICCYABAQTAAKCCQCBl5XGHbvj5WOlsf/K+Xrav3dMtDte3qrq0DHX3aiqpWuyJ6yvqvH87jrvp7s5ibu5CvfzAWu8YZ6r8SGqz03pPITDfc4uS33uaZfu9Y3qPb/O+znK3ZzJ3ZxrlPv5kOlvmPdvK23fyJq/jX1Zrqf96xPm7dK2vu7z67yf7uYq3M1J3M/H+Q0TAAKCCQABwQSAgGACQEAwASAgmAAQEEwACAgmAAQEEwACggkAAcEEgIBgAkBAMAEgIJgAEBBMAAgIJgAEBBMAAoIJAAHBBICAYAJAQDABICCYABAQTAAICCYABF5mD7g7Xv63qsbscX9iVNXSOF9V1dgdLx/Nc3YZZT9nGWUvZxplP2catf39XK6n/T9mDTY9mE+wVNW5cb7OuZ5hKfs5y1L2cqal7OdMS9nPh/y43W7P/gwA8MvzGyYABAQTAAKCCQABwQSAgGACQEAwASAgmAAQEEwACAgmAAQEEwACggkAAcEEgIBgAkBAMAEgIJgAEBBMAAgIJgAEBBMAAoIJAIF/AiOTy8uAdI0aAAAAAElFTkSuQmCC\n"
},
"metadata": {
"needs_background": "light"
}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "",
"execution_count": null,
"outputs": []
}
],
"metadata": {
"_draft": {
"nbviewer_url": "https://gist.github.com/7c9f7d6cb3026dc4557eb7f600c07ad5"
},
"gist": {
"id": "7c9f7d6cb3026dc4557eb7f600c07ad5",
"data": {
"description": "Hilbert_curve.ipynb",
"public": true
}
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3",
"language": "python"
},
"language_info": {
"name": "python",
"version": "3.8.5",
"mimetype": "text/x-python",
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"pygments_lexer": "ipython3",
"nbconvert_exporter": "python",
"file_extension": ".py"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment