Python Forum
Manipulating code to draw a tree - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Manipulating code to draw a tree (/thread-21660.html)



Manipulating code to draw a tree - Py_thon - Oct-09-2019

Hi!

I need to manipulate the following code to draw a tree, it currently draws a spiral. I know that the following line needs to be added:

draw_spiral(x2,y2,angle-dangle,dangle,length/dlength,dlength,steps-1,canvas)inside draw spiral. At the end after the first recursive call to draw_spiral.

I just dont see where to add this code, when i add it after the line : draw_spiral(x2,y2,angle+dangle, dangle,length/dlength,dlength,steps-1,canvas) i get the error message unable to alloc 240 bytes.
Where do i need to add the second line above and why do i get the error message?

import tkinter
from math import sin, cos, radians

# DrawWindow - a class for creating a window with a canvas
class DrawingWindow(tkinter.Frame):
    def __init__(self):
        tkinter.Frame.__init__(self,None)
        self.canvas = tkinter.Canvas(self,width=500,height=500,bg='white')
        self.canvas.pack(expand=1,anchor=tkinter.CENTER)
        self.pack()

def draw_spiral(x,y,angle,dangle,length,dlength,steps,canvas):
    # x,y - starting position of the current line segment
    # angle - angle of current line segment (0 is straight up)
    # dangle - change in angle for each recursion
    # length - length of current line segment
    # dlength -  value to divide length with each step
    # steps - number of recursion steps left
    # canvas - canvas window to draw on
    if steps == 0:
        # Bas case -stop here
        pass
    else:
        # Recursive case
        # Draw one line segment from (x,y) with the given angle and length
        # Calulate end points of this line
        x2 = x+sin(radians(angle))*length
        y2 = y-cos(radians(angle))*length
        # Draw the line on the canvas
        canvas.create_line(x,y,x2,y2)
        # Recursive call, with end points the drawn lines as new start points,
        # with an increase in the angle (turn clockwise)
        # and a smaller length, and with step counted down with 1
        draw_spiral(x2,y2,angle+dangle, dangle,length/dlength,dlength,steps-1,canvas)

# Create window for drawing on
w = DrawingWindow()
# Draw the spiral on the canvas of that window
# Try to change the parameters and see what happens
draw_spiral(250,250,90,60,100,1.1,35,w.canvas)
# Start the window, so it is displayed
w.mainloop()



RE: Manipulating code to draw a tree - ichabod801 - Oct-09-2019

Please post the full text of your error message.


RE: Manipulating code to draw a tree - Py_thon - Oct-10-2019

The error message is unable to alloc 28 bytes

Process finished with exit code 1073741855


RE: Manipulating code to draw a tree - ichabod801 - Oct-10-2019

That's not a Python error message. How are you running this? The error might be with whatever you are using to run the program.


RE: Manipulating code to draw a tree - Py_thon - Oct-10-2019

I am running pycharm


RE: Manipulating code to draw a tree - ichabod801 - Oct-10-2019

Does it work when you run it from the command line?


RE: Manipulating code to draw a tree - Py_thon - Oct-10-2019

I am sorry but i dont`t know how to run it from the command line, i am new to programming.


RE: Manipulating code to draw a tree - Gribouillis - Oct-11-2019

If you make two recursive calls with steps - 1, it means that starting at level 35 you are attempting to draw 2**34 lines on the canvas. That's more than 17 billion line segments and it may well exhaust some memory resource. Try with 6 steps instead of 35.


RE: Manipulating code to draw a tree - sumana - Nov-21-2019

from turtle import Turtle, mainloop
def tree(plist, l, a, f):
    """ plist is list of pens
    l is length of branch
    a is half of the angle between 2 branches
    f is factor by which branch is shortened
    from level to level."""
    if l > 5: 
        lst = []
        for p in plist:
            p.forward(l)
            q = p.clone()
            p.left(a)
            q.right(a)
            lst.append(p)
            lst.append(q)
        tree(lst, l*f, a, f)

def main():
    p = Turtle()
    p.color("green")
    p.pensize(5)
    p.speed(100)
    p.left(90)
    p.penup()
    p.goto(0,-200)
    p.pendown()
t = tree([p], 200, 65, 0.6)
main()