
Я пытался сделать это, изменив некоторый код существования для самоизбегания обходов по сетке:
Код: Выделить всё
from random import choice
from matplotlib import pyplot as plt
plt.style.use('seaborn-v0_8-darkgrid')
print(plt.style.available)
def tree(n):
x, y = 0, 0
path = [(x, y)]
for i in range(n):
thx,thy = choice(path[j] for j in range(len(path)))
# pick the closest point but it must complete without crossing itself
x, y = choice([(thx+1, thy), (thx-1, thy), (thx, thy+1), (thx, thy-1)])
if (x, y) in path:
return path
path.append((x, y))
return path
# show plot
def show_path(path):
plt.figure(figsize=(10, 10))
# draw points
plt.scatter(*zip(*path), s=5, c='k')
# draw lines in red
plt.plot(*zip(*path), c='r')
plt.show()
path = tree(100000)
show_path(path)
Подробнее здесь: https://stackoverflow.com/questions/789 ... -a-2d-grid