I am trying to to write a python script that take n and draw a Hilbert curve based on that order. my algorithm work fine it draws the the curve and rescale the size when changing the window size. however, my drawing is not centred and can go out of bound. I would like to scale the curve with the screen without having much empty space or have it going out of bound
here is my code:
import sys
import turtle
from turtle import Turtle, Screen
#Drawing the hilbert curve using recursion.
#Var: turtle if for the Turtle, A is the length of the lines, parity is for inverting the direction, and n is for the order
def hilbert_curve(turtle, A, parity, n):
if n < 1:
return
turtle.left(parity * 90)
hilbert_curve(turtle, A, - parity, n - 1)
turtle.forward(A)
turtle.right(parity * 90)
hilbert_curve(turtle, A, parity, n - 1)
turtle.forward(A)
hilbert_curve(turtle, A, parity, n - 1)
turtle.right(parity * 90)
turtle.forward(A)
hilbert_curve(turtle, A, - parity, n - 1)
turtle.left(parity * 90)
def main():
#Rescale the drawing when changing the window size
def onResize(x=0, y=0):
width = my_win.window_width()
hight = my_win.window_height()
my_win.setworldcoordinates(-width-1, -hight-1, width-1, hight-1)
my_win.ontimer(onResize,100)
#initilize the turtle.
turtle = Turtle()
#initilize the screen.
my_win = Screen()
w = my_win.window_width()
h = my_win.window_height()
A = 20
onResize()
rule = 1
my_win.tracer(False)
if len(sys.argv) < 2:
print("Please declare the order after calling the program name")
return
n = int(sys.argv[1])
hilbert_curve(turtle,A,rule,n)
my_win.update()
my_win.mainloop()
main()
**I would appreciate it if someone can fix my problem thank you **