ag0715
ag0715

Reputation: 1

Recursive fractal square in wrong area

I am writing code for a fractal square. I have the function to draw the square, and this what I get: click here but this is what I am trying to achieve: click here. I think my problem is not placing my coordinates correctly but I might be entirely wrong. Here is my code:

#setting up stuff Code by Angel M. Gonzalez
import turtle
window = turtle.Screen()
square = turtle.Pen()
square.speed(0)
square.penup()
square.goto(0,0)
square.pendown()
square.color("black", "red")

#defining my function to make what i want

def pattern(length, depth, x, y):
    square.goto(0,0)
    if depth > 1:
        square.begin_fill()
        square.forward(length)
        square.left(90)
        square.forward(length)
        square.left(90)
        square.forward(length)
        square.left(90)
        square.forward(length)
        square.end_fill()

        pattern(length/2.2, depth -1, 1,1)
        pattern(length/4.4, depth -1, 1.5,1.5)

#run my function  
pattern(300, 4, 0,0)




#exit
turtle.exitonclick()

Like I said before, I don't know if the coordinates are being placed incorrectly here, or if I am completely missing something. I would say my function for the original square is fine.

Upvotes: 0

Views: 932

Answers (1)

cdlane
cdlane

Reputation: 41872

Your approach to recursion is all wrong. You can't use absolute positioning in your recursion:

square.goto(0,0)
...
pattern(length/4.4, depth -1, 1.5,1.5)

Everything has to be relative to where you are. Also, to make a recursive drawing like this work, your recursive function should leave the turtle in the same state (position and heading) as it was when you entered the recursive function, so the caller knows where it stands.

For this design, you can't simply draw a square and then recurse, you need to be recursing at each corner of the square as you're drawing.

As a simpler, initial exercise, let's just draw the lines of this shape and ignore the fill. This may not lead directly to a solution but will demonstrate the above principals:

from turtle import Turtle, Screen

def pattern(length, depth):

    if depth < 1:
        return

    # assume we're at the center of square, move to corner
    turtle.penup()
    turtle.backward(length / 2)
    turtle.left(90)
    turtle.backward(length / 2)
    turtle.right(90)
    turtle.pendown()

    for _ in range(4):
        pattern(length / 2.2, depth - 1)  # recurse at each corner
        turtle.forward(length)
        turtle.left(90)

    # return to center of square for the benefit of caller
    turtle.penup()
    turtle.forward(length / 2)
    turtle.left(90)
    turtle.forward(length / 2)
    turtle.right(90)
    turtle.pendown()

screen = Screen()
turtle = Turtle()
turtle.speed('fastest')

pattern(100, 4)

turtle.hideturtle()
screen.exitonclick()

OUTPUT

enter image description here

Now comes the problem of both filling these squares and making sure the upper right square in the pattern is always "tucked under".

Upvotes: 1

Related Questions