Circle loop

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • OzzyB
    New Member
    • Nov 2009
    • 15

    Circle loop

    This is another problem in the Zelle book.
    Code:
    def drawCircle(win, centre, radius, colour):
        circle = Circle(radius)
        circle.setFill(colour)
        circle.setWidth(2)
        circle.draw(win)
        
    def eyes(row, columns):
        win = GraphWin()
        drawCircle(win, centre, 50, "white")
        drawCircle(win, centre, 25, "blue")
        drawCircle(win, centre, 10, "black")
    How do I loop this code so that when I call the function eyes(2,4) it outputs 8 eyes on the graphic window for example

    OOOO
    OOOO



    Please help me find a sloution.
    Last edited by bvdet; Nov 17 '09, 06:02 PM. Reason: Add code tags
  • Glenton
    Recognized Expert Contributor
    • Nov 2008
    • 391

    #2
    I might rather define a function that made 1 eye with a given centre. Then make the eyes call that function.

    Be that as it may, you're going to need a nested loop
    Code:
    for i in range(rows):
        for j in range(columns):
            #draw a eye at position i, j

    Comment

    • OzzyB
      New Member
      • Nov 2009
      • 15

      #3
      I still don't understand it. Please do not mind, but can you show me the solution because I seriously can't figuare it out, I been trying for hours.

      Comment

      • Glenton
        Recognized Expert Contributor
        • Nov 2008
        • 391

        #4
        Have you managed to get it to print a circle yet? Have you tried with the functions you've got? Once you can do that, the rest should be easy.

        Comment

        • OzzyB
          New Member
          • Nov 2009
          • 15

          #5
          Code:
          from graphics import*     
          def drawCircle(win, radius, colour):
              circle = Circle(radius)
              circle.setFill(colour)
              circle.setWidth(2)
              circle.draw(win)
              
          def drawEye():
              win = GraphWin()
              drawCircle(win, 50, "white")
              drawCircle(win, 25, "blue")
              drawCircle(win, 10, "black")
              
          def eightEyes(row,columns):
              
              for i in range(rows):
               for j in range(columns):
                print i, j
          this is my code at the moment, yes I can get one cricle to print. but it does print 8 cricles (like below)

          0000
          0000

          As I missing something from the code, if I am can you correct the code, please.
          Last edited by bvdet; Nov 18 '09, 03:47 PM. Reason: Add code tags

          Comment

          • Glenton
            Recognized Expert Contributor
            • Nov 2008
            • 391

            #6
            Did you mean it does not print 8 circles?

            What was the output of your code?
            What did you run to get the circle to print?

            Comment

            • OzzyB
              New Member
              • Nov 2009
              • 15

              #7
              Code:
              def drawCircle(win, centre, radius):
                  circle = Circle(centre, radius)
                  circle.setFill(colour)
                  circle.setWidth(2)
                  circle.draw(win)
              
              def drawEyes():
                  win = GraphWin()
                  centre = Point(55,55)
                  drawCircle(win, centre, 50, "white")
                  drawCircle(win, centre, 25, "blue")
                  drawCircle(win, centre, 10, "black")
              
              def eyes(rows,columns):
                  win = GraphWin("", 100 * rows, 100 * columns)
                  for i in range(rows):
                      for j in range(columns):
                          eyes(win, centre, 50)
              At the moment i can't output 8 cricles in the graphic window and I don't known where I have gone wrong. Please help!
              Last edited by bvdet; Nov 18 '09, 03:47 PM. Reason: Add code tags

              Comment

              • bvdet
                Recognized Expert Specialist
                • Oct 2006
                • 2851

                #8
                OzzyB,

                When posting code, please use code tags.



                BV
                Moderator

                Comment

                • bvdet
                  Recognized Expert Specialist
                  • Oct 2006
                  • 2851

                  #9
                  In your code, you are calling function eyes() recursively. I think you should be calling drawCircle() instead. In function eyes(), variable centre has not been defined.

                  You need some way to advance the calculation of the circle center point. I am not familiar with your graphics package, but in Tkinter, you create a circle by calculating the upper left and lower right coordinates on the canvas.

                  Let's assume you want a space between the circles. The width of your canvas would be:
                  Code:
                  columns*(radius*2+space)
                  The upper left and lower right coordinates for the circle in any given column, row would be
                  Code:
                  pt1 = Point(space/2.0, space/2.0)
                  pt2 = pt1+Point(radius*2, radius*2)
                  Then you iterate on columns and rows:
                  Code:
                  for i in range(0, columns):
                      for j in range(0, rows):
                          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)
                          w.create_oval(xy, fill='red')
                  Point objects in my code have an addition overload(__add_ _).

                  Here's the full code to create circles in Tkinter:
                  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)
                  Post back with any questions you have.

                  Comment

                  • Glenton
                    Recognized Expert Contributor
                    • Nov 2008
                    • 391

                    #10
                    Originally posted by OzzyB
                    Code:
                    def drawCircle(win, centre, radius):
                        circle = Circle(centre, radius)
                        circle.setFill(colour)
                        circle.setWidth(2)
                        circle.draw(win)
                    
                    def drawEyes():
                        win = GraphWin()
                        centre = Point(55,55)
                        drawCircle(win, centre, 50, "white")
                        drawCircle(win, centre, 25, "blue")
                        drawCircle(win, centre, 10, "black")
                    
                    def eyes(rows,columns):
                        win = GraphWin("", 100 * rows, 100 * columns)
                        for i in range(rows):
                            for j in range(columns):
                                eyes(win, centre, 50)
                    At the moment i can't output 8 cricles in the graphic window and I don't known where I have gone wrong. Please help!
                    Hi. I think you're close. I'm also not familiar with the package you're using, but if drawEyes works, then you should be very close.

                    Firstly, change draw eyes so that it draws where you tell it to, rather than centred on (55,55):
                    Code:
                    def drawEye(x,y):
                        win = GraphWin()
                        centre = Point(x,y)
                        drawCircle(win, centre, 50, "white")
                        drawCircle(win, centre, 25, "blue")
                        drawCircle(win, centre, 10, "black")
                    Now drawEye(55,55) should do the same as drawEyes() did before, but you've added flexibility! Note I've changed it to drawEye because it draws just one eyes, not many!

                    Next, adjust your eyes function to call drawEye in the loop:
                    Code:
                    def eyes(rows,columns):
                        win = GraphWin("", 100 * rows, 100 * columns)
                        for i in range(rows):
                            for j in range(columns):
                                drawEye(i*100+50,j*100+50)
                    Note that the clever bit, is the we call drawEye 8 times (if rows=2 and columns=8 as you suggest), but each time with different parameters. if rows=2, the i will be 0 and 1. I know the size of my window is going to be 200 x 800. So I want it to be centred at 50 and 150. So i*100 is 0 and 100, and i*100+50 gives me the answer I want.

                    If this doesn't work, let us know why. Let us know what's happening with drawEye.

                    Good luck

                    Comment

                    • OzzyB
                      New Member
                      • Nov 2009
                      • 15

                      #11
                      I get a erorr and I am total clueless

                      NameError: global name 'win' is not defined

                      Comment

                      • bvdet
                        Recognized Expert Specialist
                        • Oct 2006
                        • 2851

                        #12
                        Originally posted by OzzyB
                        I get a erorr and I am total clueless

                        NameError: global name 'win' is not defined
                        Show us the code that results in the error. It is probably something simple. This type of error happens when a variable is created in one function and you try to access it in another function. It's a matter of scope.

                        Comment

                        • OzzyB
                          New Member
                          • Nov 2009
                          • 15

                          #13
                          I corrected the error, but I have a new problem....:(

                          the cricle outputs on different windows, instead of one single window.

                          I use portable python 2.6

                          copy of my graphic module



                          from graphics import *
                          import math

                          Code:
                          def drawCircle(win, centre, radius, colour):
                              circle = Circle(centre, radius)
                              circle.setFill(colour)
                              circle.setWidth(2)
                              circle.draw(win)
                          
                          def drawEye(x,y):
                              win = GraphWin()
                              centre = Point(x,y)
                              drawCircle(win, centre, 50, "white")
                              drawCircle(win, centre, 25, "blue")
                              drawCircle(win, centre, 10, "black")
                          
                          def windowsOfEyes(rows,columns):
                              win = GraphWin("", 100 * rows, 100 * columns)
                              for i in range(rows):
                                  for j in range(columns):
                                      drawEye(i*100+50,j*100+50)
                          Sorry if I am being annoying, this function is driving me nuts

                          Comment

                          • bvdet
                            Recognized Expert Specialist
                            • Oct 2006
                            • 2851

                            #14
                            You are creating multiple canvases. Create one canvas and draw each circle to that canvas. Try this:
                            Code:
                            def drawCircle(win, centre, radius, colour):
                                circle = Circle(centre, radius)
                                circle.setFill(colour)
                                circle.setWidth(2)
                                circle.draw(win)
                             
                            def drawEye(win,x,y):
                                centre = Point(x,y)
                                drawCircle(win, centre, 50, "white")
                                drawCircle(win, centre, 25, "blue")
                                drawCircle(win, centre, 10, "black")
                             
                            def windowsOfEyes(rows,columns):
                                win = GraphWin("", 100 * rows, 100 * columns)
                                for i in range(rows):
                                    for j in range(columns):
                                        drawEye(win,i*100+50,j*100+50)
                            I cannot download the graphics module without paying for it, so the code is untested. You may need to play with the size of your canvas.

                            Comment

                            • OzzyB
                              New Member
                              • Nov 2009
                              • 15

                              #15


                              try the link above for the graphic module

                              Comment

                              Working...