{ "cells": [ { "cell_type": "markdown", "id": "0", "metadata": {}, "source": [ "# Tutorial 4" ] }, { "cell_type": "code", "execution_count": null, "id": "1", "metadata": {}, "outputs": [], "source": [ "%load_ext autoreload\n", "%autoreload 2" ] }, { "cell_type": "code", "execution_count": null, "id": "2", "metadata": {}, "outputs": [], "source": [ "from tikzpics import TikzFigure" ] }, { "cell_type": "code", "execution_count": null, "id": "3", "metadata": {}, "outputs": [], "source": [ "def add_grid(tikz, x0, y0, L, N, options, layer=1):\n", " x1 = x0 + L\n", " y1 = y0 + L\n", " delta = L / N\n", " node_positions = []\n", "\n", " for i in range(N + 1):\n", "\n", " # Horizontal line\n", " y = y0 + i * delta\n", " node_positions = [[x0, y], [x1, y]]\n", " tikz.draw(node_positions, options=options, layer=layer)\n", "\n", " # Vertical line\n", " x = x0 + i * delta\n", " node_positions = [[x, y0], [x, y1]]\n", " tikz.draw(node_positions, options=options, layer=layer)\n", "\n", "\n", "def add_square(tikz, x0, y0, L, options, layer=1):\n", " node_positions = [\n", " [x0, y0],\n", " [x0 + L, y0],\n", " [x0 + L, y0 + L],\n", " [x0, y0 + L],\n", " [x0, y0],\n", " ]\n", " tikz.draw(node_positions, options=options, layer=layer)\n", "\n", "\n", "def draw_grid(tikz):\n", "\n", " # Add backgrounds\n", " add_square(\n", " tikz=tikz,\n", " x0=0,\n", " y0=0,\n", " L=8,\n", " options=[\"draw\", \"line width=0\", \"fill=yellow!10!white\"],\n", " layer=0,\n", " )\n", "\n", " add_square(\n", " tikz=tikz,\n", " x0=4,\n", " y0=2,\n", " L=4,\n", " options=[\"draw\", \"line width=0\", \"fill=blue!10!white\"],\n", " layer=1,\n", " )\n", "\n", " add_square(\n", " tikz=tikz,\n", " x0=5,\n", " y0=3,\n", " L=1,\n", " options=[\"draw\", \"line width=0\", \"fill=red!10!white\"],\n", " layer=2,\n", " )\n", "\n", " # Level A\n", " add_grid(\n", " tikz,\n", " x0=0,\n", " y0=0,\n", " L=8,\n", " N=4,\n", " options=[\"draw\", \"line width=2\", \"color=black\"],\n", " layer=3,\n", " )\n", "\n", " # Level B\n", " add_grid(\n", " tikz,\n", " x0=4,\n", " y0=2,\n", " L=4,\n", " N=4,\n", " options=[\"draw\", \"line width=2\", \"color=blue\"],\n", " layer=4,\n", " )\n", "\n", " # Level C\n", " add_grid(\n", " tikz,\n", " x0=5,\n", " y0=3,\n", " L=1.0,\n", " N=2,\n", " options=[\"draw\", \"line width=2\", \"color=red\"],\n", " layer=5,\n", " )" ] }, { "cell_type": "code", "execution_count": null, "id": "4", "metadata": {}, "outputs": [], "source": [ "tikz = TikzFigure(figure_setup=\">={{Triangle[scale=1]}}\")\n", "\n", "# Draw grid\n", "draw_grid(tikz=tikz)\n", "\n", "z_path = [\n", " [1, 7],\n", " [3, 7],\n", " [1, 5],\n", " [3, 5],\n", " [5, 7],\n", " [7, 7],\n", " [4.5, 5.5],\n", " [5.5, 5.5],\n", " [4.5, 4.5],\n", " [5.5, 4.5],\n", " [6.5, 5.5],\n", " [7.5, 5.5],\n", " [6.5, 4.5],\n", " [7.5, 4.5],\n", " [1, 3],\n", " [3, 3],\n", " [1, 1],\n", " [3, 1],\n", " [4.5, 3.5],\n", " [5.25, 3.75],\n", " [5.75, 3.75],\n", " [5.25, 3.25],\n", " [5.75, 3.25],\n", " [4.5, 2.5],\n", " [5.5, 2.5],\n", " [6.5, 3.5],\n", " [7.5, 3.5],\n", " [6.5, 2.5],\n", " [7.5, 2.5],\n", " [5, 1],\n", " [7, 1],\n", "]\n", "for i in range(len(z_path) - 1):\n", " path = [z_path[i], z_path[i + 1]]\n", " options = [\"->\", \"draw\", \"line width=1\", \"color=black\"]\n", " tikz.draw(path, options=options, layer=5, center=True)\n", "\n", "tikz.show()" ] }, { "cell_type": "code", "execution_count": null, "id": "5", "metadata": {}, "outputs": [], "source": [ "tikz = TikzFigure(figure_setup=\">={{Triangle[scale=1]}}\")\n", "\n", "# Draw grid\n", "draw_grid(tikz=tikz)\n", "\n", "hilbert_path = [\n", " [1, 1],\n", " [3, 1],\n", " [3, 3],\n", " [1, 3],\n", " [1, 7],\n", " [3, 7],\n", " [3, 5],\n", " [4.5, 4.5],\n", " [5.5, 4.5],\n", " [5.5, 5.5],\n", " [4.5, 5.5],\n", " [5, 7],\n", " [7, 7],\n", " [7.5, 5.5],\n", " [6.5, 5.5],\n", " [6.5, 4.5],\n", " [7.5, 4.5],\n", " [7.5, 2.5],\n", " [6.5, 2.5],\n", " [6.5, 3.5],\n", " [5.75, 3.75],\n", " [5.75, 3.25],\n", " [5.25, 3.25],\n", " [5.25, 3.75],\n", " [4.5, 3.5],\n", " [4.5, 2.5],\n", " [5.5, 2.5],\n", " [5, 1],\n", " [7, 1],\n", "]\n", "for i in range(len(hilbert_path) - 1):\n", " path = [hilbert_path[i], hilbert_path[i + 1]]\n", " options = [\"->\", \"draw\", \"line width=1\", \"color=black\"]\n", " tikz.draw(path, options=options, layer=5, center=True)\n", "\n", "tikz.show()" ] } ], "metadata": { "kernelspec": { "display_name": "env_tikzpics", "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.13.3" } }, "nbformat": 4, "nbformat_minor": 5 }