多阶Hilbert 曲线

import matplotlib.pyplot as plt

plt.rcParams['font.sans-serif'] = ['SimHei']

plt.rcParams['axes.unicode_minus'] = False

# Hilbert 曲线

def _hilbert(direction, rotation, order):

    if order == 0:

        return

    direction += rotation

    _hilbert(direction, -rotation, order - 1)

    step(direction)

    direction -= rotation

    _hilbert(direction, rotation, order - 1)

    step(direction)

    _hilbert(direction, rotation, order - 1)

    direction -= rotation

    step(direction)

    _hilbert(direction, -rotation, order - 1)

def step(direction):

    next = {0: (1, 0), 1: (0, 1), 2: (-1, 0), 3: (0, -1)}[direction & 0x3]

    global x, y

    x.append(x[-1] + next[0])

    y.append(y[-1] + next[1])

def hilbert(order):

    global x, y

    x = [0,]

    y = [0,]

    _hilbert(0, 1, order)

    return (x, y)

fig, ax = plt.subplots(1, 4) 

for i in range(4):

    sub = ax[i]

    sub.axis("off")

    sub.set_aspect("equal")

    x, y = hilbert(i + 1)

    sub.plot(x, y)

pl.title('多阶Hilbert 曲线',x=-1)

plt.show()


你可能感兴趣的:(多阶Hilbert 曲线)