I'm trying to make a Hilbert Curve that takes as little time as possible to complete. Here is the code so far (adapted from Hilbert curve using turtle graphics and recursion)
from turtle import *
from win32api import GetSystemMetrics
def hilbert_curve(amt, facing, n, start_at_corner=True) -> None:
if start_at_corner:
ht()
up()
goto(x=(- (GetSystemMetrics(0) - 30) / 2), y=(- (GetSystemMetrics(1) / 2 - 50)))
down()
if n < 1:
return
try: # Only here because I find error messages annoying
left(facing * 90)
hilbert_curve(amt, - facing, n - 1, False)
fd(amt)
right(facing * 90)
hilbert_curve(amt, facing, n - 1, False)
fd(amt)
hilbert_curve(amt, facing, n - 1, False)
right(facing * 90)
fd(amt)
hilbert_curve(amt, - facing, n - 1, False)
left(facing * 90)
except Terminator:
from sys import exit
exit()
screen = getscreen()
speed(0)
hilbert_curve(5, 1, 15)
screen.mainloop()
The issue with this is that the turtle makes a lot of unnecessary turns - at the start and at all connections - I understand why this happens but I don't know how to fix it.
If there are any other things I can change in above code to make the turtle faster, suggestions are welcome!