На странице Википедии, которую вы цитируете, разъясняется, как это сделать, используя графику черепахи и L-систему. Просто следуя этим инструкциям, я получил:
from turtle import Screen, Turtle
AXIOM = 'LFL+F+LFL'
RULES = {
'L': '-RF+LFL+FR-',
'R': '+LF-RFR-FL+',
}
DISTANCE = 300
CYCLES = 4
def draw(string, distance):
for character in string:
if character == 'F':
turtle.forward(distance)
elif character == '+':
turtle.right(90)
elif character == '-':
turtle.left(90)
else:
pass # ignore other characters
def produce(string):
production = ''
for character in string:
if character in RULES:
production += RULES[character]
else:
production += character # just copy other characters
return production
screen = Screen()
screen.tracer(False)
turtle = Turtle()
turtle.hideturtle()
turtle.setheading(90)
string = AXIOM
for _ in range(1, CYCLES):
string = produce(string)
distance = DISTANCE / CYCLES ** 2 # crude estimate, fix this
draw(string, distance)
screen.tracer(True)
screen.exitonclick()
And the fun part is, having implemented our L-system, by just changing the data, not the code, we can also make the Hilbert curve:
from turtle import Screen, Turtle
AXIOM = 'A'
RULES = {
'A': '-BF+AFA+FB-',
'B': '+AF-BFB-FA+',
}
DISTANCE = 300
CYCLES = 6
# ...
введите описание изображения здесь
Конечно, вычисление переменной distance
требует некоторой работы ...