Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Manipulating code to draw a tree

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):
        self.canvas = tkinter.Canvas(self,width=500,height=500,bg='white')

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
        # 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
        # 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
# Start the window, so it is displayed
ichabod801 wrote Oct-09-2019, 01:17 PM:
Please use python and output tags when posting code and results. I put them in for you this time. Here are instructions for doing it yourself next time.
Please post the full text of your error message.
Craig "Ichabod" O'Brien -
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures

The error message is unable to alloc 28 bytes

Process finished with exit code 1073741855
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 -
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures

I am running pycharm
Does it work when you run it from the command line?
Craig "Ichabod" O'Brien -
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures

I am sorry but i dont`t know how to run it from the command line, i am new to programming.
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.
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:
            q = p.clone()
        tree(lst, l*f, a, f)

def main():
    p = Turtle()
t = tree([p], 200, 65, 0.6)
Gribouillis wrote Nov-21-2019, 06:03 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.

I fixed for you this time. Please use code tags on future posts.

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Manipulating List frenchyinspace 2 364 Oct-08-2019, 07:57 AM
Last Post: perfringo

Forum Jump:

Users browsing this thread: 1 Guest(s)