Tutorial 4ΒΆ

[1]:
%load_ext autoreload
%autoreload 2
[2]:
from tikzpics import TikzFigure
[3]:
def add_grid(tikz, x0, y0, L, N, options, layer=1):
    x1 = x0 + L
    y1 = y0 + L
    delta = L / N
    node_positions = []

    for i in range(N + 1):

        # Horizontal line
        y = y0 + i * delta
        node_positions = [[x0, y], [x1, y]]
        tikz.draw(node_positions, options=options, layer=layer)

        # Vertical line
        x = x0 + i * delta
        node_positions = [[x, y0], [x, y1]]
        tikz.draw(node_positions, options=options, layer=layer)


def add_square(tikz, x0, y0, L, options, layer=1):
    node_positions = [
        [x0, y0],
        [x0 + L, y0],
        [x0 + L, y0 + L],
        [x0, y0 + L],
        [x0, y0],
    ]
    tikz.draw(node_positions, options=options, layer=layer)


def draw_grid(tikz):

    # Add backgrounds
    add_square(
        tikz=tikz,
        x0=0,
        y0=0,
        L=8,
        options=["draw", "line width=0", "fill=yellow!10!white"],
        layer=0,
    )

    add_square(
        tikz=tikz,
        x0=4,
        y0=2,
        L=4,
        options=["draw", "line width=0", "fill=blue!10!white"],
        layer=1,
    )

    add_square(
        tikz=tikz,
        x0=5,
        y0=3,
        L=1,
        options=["draw", "line width=0", "fill=red!10!white"],
        layer=2,
    )

    # Level A
    add_grid(
        tikz,
        x0=0,
        y0=0,
        L=8,
        N=4,
        options=["draw", "line width=2", "color=black"],
        layer=3,
    )

    # Level B
    add_grid(
        tikz,
        x0=4,
        y0=2,
        L=4,
        N=4,
        options=["draw", "line width=2", "color=blue"],
        layer=4,
    )

    # Level C
    add_grid(
        tikz,
        x0=5,
        y0=3,
        L=1.0,
        N=2,
        options=["draw", "line width=2", "color=red"],
        layer=5,
    )
[4]:
tikz = TikzFigure(figure_setup=">={{Triangle[scale=1]}}")

# Draw grid
draw_grid(tikz=tikz)

z_path = [
    [1, 7],
    [3, 7],
    [1, 5],
    [3, 5],
    [5, 7],
    [7, 7],
    [4.5, 5.5],
    [5.5, 5.5],
    [4.5, 4.5],
    [5.5, 4.5],
    [6.5, 5.5],
    [7.5, 5.5],
    [6.5, 4.5],
    [7.5, 4.5],
    [1, 3],
    [3, 3],
    [1, 1],
    [3, 1],
    [4.5, 3.5],
    [5.25, 3.75],
    [5.75, 3.75],
    [5.25, 3.25],
    [5.75, 3.25],
    [4.5, 2.5],
    [5.5, 2.5],
    [6.5, 3.5],
    [7.5, 3.5],
    [6.5, 2.5],
    [7.5, 2.5],
    [5, 1],
    [7, 1],
]
for i in range(len(z_path) - 1):
    path = [z_path[i], z_path[i + 1]]
    options = ["->", "draw", "line width=1", "color=black"]
    tikz.draw(path, options=options, layer=5, center=True)

tikz.show()
../_images/tutorials_tutorial_04_4_0.png
[5]:
tikz = TikzFigure(figure_setup=">={{Triangle[scale=1]}}")

# Draw grid
draw_grid(tikz=tikz)

hilbert_path = [
    [1, 1],
    [3, 1],
    [3, 3],
    [1, 3],
    [1, 7],
    [3, 7],
    [3, 5],
    [4.5, 4.5],
    [5.5, 4.5],
    [5.5, 5.5],
    [4.5, 5.5],
    [5, 7],
    [7, 7],
    [7.5, 5.5],
    [6.5, 5.5],
    [6.5, 4.5],
    [7.5, 4.5],
    [7.5, 2.5],
    [6.5, 2.5],
    [6.5, 3.5],
    [5.75, 3.75],
    [5.75, 3.25],
    [5.25, 3.25],
    [5.25, 3.75],
    [4.5, 3.5],
    [4.5, 2.5],
    [5.5, 2.5],
    [5, 1],
    [7, 1],
]
for i in range(len(hilbert_path) - 1):
    path = [hilbert_path[i], hilbert_path[i + 1]]
    options = ["->", "draw", "line width=1", "color=black"]
    tikz.draw(path, options=options, layer=5, center=True)

tikz.show()
../_images/tutorials_tutorial_04_5_0.png