Python-Dragon 曲线

import matplotlib.pyplot as plt

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

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

# Dragon 曲线

def _dragon_A(direction, order):

    if order == 0:

        return direction

    direction = _dragon_A(direction, order - 1)

    direction += 1

    direction = _dragon_B(direction, order - 1)

    step(direction)

    direction += 1

    return direction

def _dragon_B(direction, order):

    if order == 0:

        return direction

    direction -= 1

    step(direction)

    direction = _dragon_A(direction, order - 1)

    direction -= 1

    direction = _dragon_B(direction, order - 1)

    return direction

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 dragon(order):

    global x, y

    x = [0,]

    y = [0,]

    direction = 0

    step(direction)

    _dragon_A(0, order)

    return (x, y)

x, y = dragon(4)

plt.plot(x, y)

pl.title('Dragon 曲线')

plt.show()


你可能感兴趣的:(Python-Dragon 曲线)