circle loop?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cid11
    New Member
    • Jun 2010
    • 10

    circle loop?

    I am trying to make some code which creates 5 set of circles. But this is the best i could do

    00000(all red)
    00000(all white)
    00000(all red)
    00000(all white)

    Code:
    from graphics import *
    
    def main():
        win = GraphWin("My Circle", 300, 300)
        # count backwards
        for k in range(6, 1, -1):
            c = Circle(Point(150,150), 20*k)
            # alternate
            if k % 2 == 0:
                c.setFill('red')
            else:
                c.setFill('white')
            c.draw(win)
        
        win.getMouse() # pause for click in window
        win.close()
    
    main()
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Originally posted by cid11
    I am trying to make some code which creates 5 set of circles. But this is the best i could do

    00000(all red)
    00000(all white)
    00000(all red)
    00000(all white)

    Code:
    from graphics import *
    
    def main():
        win = GraphWin("My Circle", 300, 300)
        # count backwards
        for k in range(6, 1, -1):
            c = Circle(Point(150,150), 20*k)
            # alternate
            if k % 2 == 0:
                c.setFill('red')
            else:
                c.setFill('white')
            c.draw(win)
        
        win.getMouse() # pause for click in window
        win.close()
    
    main()
    cid11,

    Do you have a question or does it not work the way you want?

    Comment

    • cid11
      New Member
      • Jun 2010
      • 10

      #3
      sorry i posted in a hurry so none of tht make sense...it works but not the way i want it to.. now it does concentric circles.. but i want it to have seperate circles as i have tried to show below.. any help would be appreciated..

      00000(all red)
      00000(all white)
      00000(all red)
      00000(all white)

      Comment

      • bvdet
        Recognized Expert Specialist
        • Oct 2006
        • 2851

        #4
        cid11,

        You want the circles to be the same size organized in rows and columns. Alternate the color for each row. Calculate the size of the canvas based on the number of rows and columns and the size of the circle. Iterate on the number of rows and the number of columns in a nested for loop. Calculate the position of each circle using the row number (Y direction) and the column number (X direction).


        Here's part of some sample code:
        Code:
            win = GraphWin("My Circle", length, height)
            for row in range(rows):
                for col in range(cols):
                    x = size+col*size
                    y = size+row*size
                    c = Circle(Point(x,y), size/2)

        Comment

        • cid11
          New Member
          • Jun 2010
          • 10

          #5
          Originally posted by bvdet
          cid11,

          You want the circles to be the same size organized in rows and columns. Alternate the color for each row. Calculate the size of the canvas based on the number of rows and columns and the size of the circle. Iterate on the number of rows and the number of columns in a nested for loop. Calculate the position of each circle using the row number (Y direction) and the column number (X direction).


          Here's part of some sample code:
          Code:
              win = GraphWin("My Circle", length, height)
              for row in range(rows):
                  for col in range(cols):
                      x = size+col*size
                      y = size+row*size
                      c = Circle(Point(x,y), size/2)
          so wht does the sample code u gave do?

          Comment

          • bvdet
            Recognized Expert Specialist
            • Oct 2006
            • 2851

            #6
            The sample code does part of what I described in my previous post. Do understand what a for loop is?

            Comment

            • cid11
              New Member
              • Jun 2010
              • 10

              #7
              yup i kinda understand what a for loop is.. but a bit rusty on python as i am retaking a module and had studied it in sept and just learned java.. what exactly would I have to do? to complete what i want the program to do?

              thx

              Comment

              • bvdet
                Recognized Expert Specialist
                • Oct 2006
                • 2851

                #8
                Before the sample code you need to calculate length and height and assign a value to size. After the sample code, you need an if/else block to decide on the color. The rest is the same as your original code.

                Comment

                • cid11
                  New Member
                  • Jun 2010
                  • 10

                  #9
                  So the length and height are to calculate the size of the graphic window according to the number of circles required? could I use the if and else from the previous code I gave? and do I keep my orignal code and add code to it and remove and add?

                  thx

                  Comment

                  • cid11
                    New Member
                    • Jun 2010
                    • 10

                    #10
                    for the size of the graphic window.. would it be

                    size of the window - ((number of circles + 1) * spacing)) / number of circles

                    Comment

                    Working...