Python Forum
Manipulating code to draw a tree
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Manipulating code to draw a tree
#1
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()
Reply
#2
Please post the full text of your error message.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
The error message is unable to alloc 28 bytes

Process finished with exit code 1073741855
Reply
#4
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
I am running pycharm
Reply
#6
Does it work when you run it from the command line?
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#7
I am sorry but i dont`t know how to run it from the command line, i am new to programming.
Reply
#8
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.
Reply
#9
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()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Manipulating panda dataframes more python-like badtwistoffate 4 1,967 Jan-31-2023, 04:30 AM
Last Post: deanhystad
  Manipulating List frenchyinspace 2 2,606 Oct-08-2019, 07:57 AM
Last Post: perfringo
  Manipulating __init__ method schniefen 5 3,448 May-06-2019, 11:22 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020