Circle loop

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #16
    I swapped columns and rows, imported the module and call each function or class module.call() so it is clear that the calls are made to graphics objects. Other than that, it works as I modified it in my previous post.
    Code:
    import graphics
    
    def drawCircle(win, centre, radius, colour):
        circle = graphics.Circle(centre, radius)
        circle.setFill(colour)
        circle.setWidth(2)
        circle.draw(win)
     
    def drawEye(win,x,y):
        centre = graphics.Point(x,y)
        drawCircle(win, centre, 50, "white")
        drawCircle(win, centre, 25, "blue")
        drawCircle(win, centre, 10, "black")
     
    def windowsOfEyes(columns,rows):
        win = graphics.GraphWin("", 100 * columns, 100 * rows)
        for i in range(columns):
            for j in range(rows):
                drawEye(win,i*100+50,j*100+50)
    
    windowsOfEyes(4,2)

    Comment

    • OzzyB
      New Member
      • Nov 2009
      • 15

      #17
      I am so excited, it finally works. I couldn't have done it without your help thank.

      Comment

      • OzzyB
        New Member
        • Nov 2009
        • 15

        #18
        I am now trying to display a triangle of eyes.

        Is there any way of changing the above code to display a triangle of eyes.

        for example:
        Unlimited space to host images, easy to use image uploader, albums, photo hosting, sharing, dynamic image resizing on web and mobile.


        Thanks

        Comment

        • Glenton
          Recognized Expert Contributor
          • Nov 2008
          • 391

          #19
          Yes, there is a way.

          Comment

          • Glenton
            Recognized Expert Contributor
            • Nov 2008
            • 391

            #20
            So let's examine in more detail what's happening in the squares. You effectively have coordinates where you want the squares. They are:
            (0,0) (0,1) (0,2) (0,3)
            (1,0) (1,1) (1,2) (1,3), right.

            So you get all these done by running two nested loops.
            The first loop runs through all the columns, ie it runs from 0 to 3.
            The for each column the second loop runs through all the rows from 0 to 1.

            So let's spell this out even more explicitly, but for a 3x3 square:
            Column 0 - Row 0,1,2
            Column 1 - Row 0,1,2
            Column 2 - Row 0,1,2.

            Now for a triangle you'll want to do this:
            Column 0 - Row 0,1,2
            Column 1 - Row 1,2
            Column 2 - Row 2

            So in each case the row starts not from 0 (as in the square) but from the column number you're currently working with.

            You might be able to figure it out from here...

            Comment

            • OzzyB
              New Member
              • Nov 2009
              • 15

              #21
              Do I use the getX and getY?...or input the the coordinates in the range i & j.

              Comment

              • Glenton
                Recognized Expert Contributor
                • Nov 2008
                • 391

                #22
                I see no reference to getX or getY in the preceding discussion. Can you explain what you mean?

                The statement
                Code:
                drawEye(win,i*100+50,j*100+50)
                is an expression in that plots a point in row i and column j. Thus fiddling with the preceding loops will allow you to plot whatever combination of i and j you want.

                I mean to be crude you could have:
                Code:
                for i,j in [[0,0],[1,1],[2,2]]:
                But there's a much more elegant solution to the triangle issue, which is also trying to teach you a specific lesson. Reread my previous post carefully, and you'll figure it out.

                Comment

                • Glenton
                  Recognized Expert Contributor
                  • Nov 2008
                  • 391

                  #23
                  E.g. try this in terminal:
                  Code:
                  >>> for i,j in [[1,1],[2,2],[3,3]]:
                  ...     print i,j
                  ... 
                  1 1
                  2 2
                  3 3
                  Play around in terminal, trying different things.

                  Comment

                  • OzzyB
                    New Member
                    • Nov 2009
                    • 15

                    #24
                    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(n):
                        win = GraphWin("", 100 * n, 100 * n)
                        for i in range((0,0),(0,1),(0,2),(0,3)):
                            for j in range((1,0),(1,1),(1,2),(1,3)):
                                drawEye(win,i*100+50,j*100+50)
                    when I call windowsOfEyes(3 )

                    I get the following error
                    range expected at most 3 arguments, got 4

                    Can you corect the code, help me find a sloution.

                    Thanks

                    Comment

                    • Glenton
                      Recognized Expert Contributor
                      • Nov 2008
                      • 391

                      #25
                      for i in range((0,0),(0, 1),(0,2),(0,3)) has no real meaning.
                      Range can be used in a couple of ways. Try look it up in the docs, or google something like "python range", and see how range can be used.

                      Comment

                      • Glenton
                        Recognized Expert Contributor
                        • Nov 2008
                        • 391

                        #26
                        By the way, what are you planning to use python for? You may need a different book, and perhaps some people can suggest resources for you. But it depends on your usage...

                        Comment

                        • OzzyB
                          New Member
                          • Nov 2009
                          • 15

                          #27
                          I figured it out at the end..I read your notes carefully...

                          Comment

                          Working...