Sohaib Aljey
Sohaib Aljey

Reputation: 35

Scaling a turtle drawing python

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 **

Upvotes: 0

Views: 1374

Answers (1)

cdlane
cdlane

Reputation: 41905

my algorithm work fine it draws the the curve and rescale the size when changing the window size.

No it doesn't. Your drawing is never scaled, it remains the same size. And your main() function which sets up the scaling code is never called as it follows the mainloop() call which turns control over to the tkinter event handler:

my_win.mainloop()

main()

Using an event timer is the wrong way to go about this problem. However, since turtle doesn't expose the underlying tkinter window resize event, let's play along with this model, rather than dropping down to the tkinter layer. I would do it this way instead:

from turtle import Turtle, Screen

def hilbert_curve(turtle, A, parity, n):

    '''
    Draw the hilbert curve using recursion.

    Arguments:
        turtle is for the Turtle,
        A is the length of the lines,
        parity is for inverting the direction,
        and n is for the order
    '''

    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():
    order = 4
    parity = 1
    length = 100 / (4 * order - 1)

    def onResize():
        # Rescale drawing when changing window size (the hard way)
        turtle.reset()
        screen.setworldcoordinates(0, 0, 100, 100)
        hilbert_curve(turtle, length, parity, order)
        screen.update()
        screen.ontimer(onResize, 1000)

    screen = Screen()
    screen.tracer(False)

    turtle = Turtle()

    onResize()

    screen.mainloop()

main()

I.e. maintain constant virtual coordinates regardless of window size and redraw the curve to fit the current window size. BTW, didn't I write this Hilbert curve code? Make sure to upvote where you got it from!

Upvotes: 1

Related Questions