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


Messages In This Thread
Manipulating code to draw a tree - by Py_thon - Oct-09-2019, 04:28 AM
RE: Manipulating code to draw a tree - by Py_thon - Oct-10-2019, 11:24 AM
RE: Manipulating code to draw a tree - by Py_thon - Oct-10-2019, 07:06 PM
RE: Manipulating code to draw a tree - by Py_thon - Oct-10-2019, 09:15 PM
RE: Manipulating code to draw a tree - by sumana - Nov-21-2019, 05:00 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Manipulating panda dataframes more python-like badtwistoffate 4 2,146 Jan-31-2023, 04:30 AM
Last Post: deanhystad
  Manipulating List frenchyinspace 2 2,770 Oct-08-2019, 07:57 AM
Last Post: perfringo
  Manipulating __init__ method schniefen 5 3,621 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