Making Circles smaller via a loop

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Bob BigMac
    New Member
    • Nov 2011
    • 1

    #1

    Making Circles smaller via a loop

    I am trying to write a program so when it runs, it makes one circle of say 50 radius, then the next one would be 45 and so on going down by 5 each time.

    What I am unsure of is how can I do this with a loop or is it impossible. I look forwards to any help that I can get.
    Last edited by bvdet; Nov 22 '11, 02:42 PM. Reason: spelling
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Are you planning to use Tkinter or some other GUI interface? Your radiuses can be defined in a list comprehension.
    Code:
    >>> [rad for rad in range(50, 0, -5)]
    [50, 45, 40, 35, 30, 25, 20, 15, 10, 5]
    >>>
    Using Tkinter, you can create a canvas and place circles on the canvas. Here's an example:
    Code:
    from Tkinter import *
    
    class Point(object):
        def __init__(self, x=0.0, y=0.0):
            self.x = float(x)
            self.y = float(y)
        def __add__(self, other):
            return Point(self.x+other.x, self.y+other.y)
    
    def drawCircles(columns, rows, radius=100, space=0):
        root = Tk()
        canvas_width = columns*(radius*2+space)
        canvas_height = rows*(radius*2+space)
        
        w = Canvas(root, width=canvas_width, height=canvas_height)
        # create a black background
        w.create_rectangle(0,0,canvas_width,canvas_height,fill="black")
        # calculate UL and LR coordinates in column 1, row 1
        pt1 = Point(space/2.0, space/2.0)
        pt2 = pt1+Point(radius*2, radius*2)
        for i in range(0, columns):
            for j in range(0, rows):
                # xy = UL.x, UL.y, LR.x, LR.y
                xy = pt1.x+i*(radius*2+space), \
                     pt1.y+j*(radius*2+space), \
                     pt2.x+i*(radius*2+space), \
                     pt2.y+j*(radius*2+space)
                # create a circle with red fill
                w.create_oval(xy, fill='red')
                
        w.pack()
        root.mainloop()
    
    if __name__ == '__main__':
        drawCircles(4, 3, 125)
        drawCircles(4, 2, 75, 20)
        drawCircles(4, 2, 75, 100)
        drawCircles(24, 16, 12, 6)

    Comment

    Working...