473,396 Members | 2,029 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Circle loop

15
This is another problem in the Zelle book.
Expand|Select|Wrap|Line Numbers
  1. def drawCircle(win, centre, radius, colour):
  2.     circle = Circle(radius)
  3.     circle.setFill(colour)
  4.     circle.setWidth(2)
  5.     circle.draw(win)
  6.  
  7. def eyes(row, columns):
  8.     win = GraphWin()
  9.     drawCircle(win, centre, 50, "white")
  10.     drawCircle(win, centre, 25, "blue")
  11.     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.
Nov 17 '09 #1
26 9397
Glenton
391 Expert 256MB
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
Expand|Select|Wrap|Line Numbers
  1. for i in range(rows):
  2.     for j in range(columns):
  3.         #draw a eye at position i, j
  4.  
Nov 17 '09 #2
OzzyB
15
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.
Nov 17 '09 #3
Glenton
391 Expert 256MB
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.
Nov 17 '09 #4
OzzyB
15
Expand|Select|Wrap|Line Numbers
  1. from graphics import*     
  2. def drawCircle(win, radius, colour):
  3.     circle = Circle(radius)
  4.     circle.setFill(colour)
  5.     circle.setWidth(2)
  6.     circle.draw(win)
  7.  
  8. def drawEye():
  9.     win = GraphWin()
  10.     drawCircle(win, 50, "white")
  11.     drawCircle(win, 25, "blue")
  12.     drawCircle(win, 10, "black")
  13.  
  14. def eightEyes(row,columns):
  15.  
  16.     for i in range(rows):
  17.      for j in range(columns):
  18.       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.
Nov 18 '09 #5
Glenton
391 Expert 256MB
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?
Nov 18 '09 #6
OzzyB
15
Expand|Select|Wrap|Line Numbers
  1. def drawCircle(win, centre, radius):
  2.     circle = Circle(centre, radius)
  3.     circle.setFill(colour)
  4.     circle.setWidth(2)
  5.     circle.draw(win)
  6.  
  7. def drawEyes():
  8.     win = GraphWin()
  9.     centre = Point(55,55)
  10.     drawCircle(win, centre, 50, "white")
  11.     drawCircle(win, centre, 25, "blue")
  12.     drawCircle(win, centre, 10, "black")
  13.  
  14. def eyes(rows,columns):
  15.     win = GraphWin("", 100 * rows, 100 * columns)
  16.     for i in range(rows):
  17.         for j in range(columns):
  18.             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!
Nov 18 '09 #7
bvdet
2,851 Expert Mod 2GB
OzzyB,

When posting code, please use code tags.

http://bytes.com/faq.php?faq=how_to_..._code_tag_list

BV
Moderator
Nov 18 '09 #8
bvdet
2,851 Expert Mod 2GB
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:
Expand|Select|Wrap|Line Numbers
  1. columns*(radius*2+space)
The upper left and lower right coordinates for the circle in any given column, row would be
Expand|Select|Wrap|Line Numbers
  1. pt1 = Point(space/2.0, space/2.0)
  2. pt2 = pt1+Point(radius*2, radius*2)
Then you iterate on columns and rows:
Expand|Select|Wrap|Line Numbers
  1. for i in range(0, columns):
  2.     for j in range(0, rows):
  3.         xy = pt1.x+i*(radius*2+space), \
  4.              pt1.y+j*(radius*2+space), \
  5.              pt2.x+i*(radius*2+space), \
  6.              pt2.y+j*(radius*2+space)
  7.         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:
Expand|Select|Wrap|Line Numbers
  1. from Tkinter import *
  2.  
  3. class Point(object):
  4.     def __init__(self, x=0.0, y=0.0):
  5.         self.x = float(x)
  6.         self.y = float(y)
  7.     def __add__(self, other):
  8.         return Point(self.x+other.x, self.y+other.y)
  9.  
  10. def drawCircles(columns, rows, radius=100, space=0):
  11.     root = Tk()
  12.     canvas_width = columns*(radius*2+space)
  13.     canvas_height = rows*(radius*2+space)
  14.  
  15.     w = Canvas(root, width=canvas_width, height=canvas_height)
  16.     # create a black background
  17.     w.create_rectangle(0,0,canvas_width,canvas_height,fill="black")
  18.     # calculate UL and LR coordinates in column 1, row 1
  19.     pt1 = Point(space/2.0, space/2.0)
  20.     pt2 = pt1+Point(radius*2, radius*2)
  21.     for i in range(0, columns):
  22.         for j in range(0, rows):
  23.             # xy = UL.x, UL.y, LR.x, LR.y
  24.             xy = pt1.x+i*(radius*2+space), \
  25.                  pt1.y+j*(radius*2+space), \
  26.                  pt2.x+i*(radius*2+space), \
  27.                  pt2.y+j*(radius*2+space)
  28.             # create a circle with red fill
  29.             w.create_oval(xy, fill='red')
  30.  
  31.     w.pack()
  32.     root.mainloop()
  33.  
  34. if __name__ == '__main__':
  35.     drawCircles(4, 3, 125)
  36.     drawCircles(4, 2, 75, 20)
  37.  
Post back with any questions you have.
Nov 18 '09 #9
Glenton
391 Expert 256MB
@OzzyB
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):
Expand|Select|Wrap|Line Numbers
  1. def drawEye(x,y):
  2.     win = GraphWin()
  3.     centre = Point(x,y)
  4.     drawCircle(win, centre, 50, "white")
  5.     drawCircle(win, centre, 25, "blue")
  6.     drawCircle(win, centre, 10, "black")
  7.  
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:
Expand|Select|Wrap|Line Numbers
  1. def eyes(rows,columns):
  2.     win = GraphWin("", 100 * rows, 100 * columns)
  3.     for i in range(rows):
  4.         for j in range(columns):
  5.             drawEye(i*100+50,j*100+50)
  6.  
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
Nov 19 '09 #10
OzzyB
15
I get a erorr and I am total clueless

NameError: global name 'win' is not defined
Nov 19 '09 #11
bvdet
2,851 Expert Mod 2GB
@OzzyB
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.
Nov 19 '09 #12
OzzyB
15
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

http://rapidshare.com/files/309307694/graphics.py.html

from graphics import *
import math

Expand|Select|Wrap|Line Numbers
  1. def drawCircle(win, centre, radius, colour):
  2.     circle = Circle(centre, radius)
  3.     circle.setFill(colour)
  4.     circle.setWidth(2)
  5.     circle.draw(win)
  6.  
  7. def drawEye(x,y):
  8.     win = GraphWin()
  9.     centre = Point(x,y)
  10.     drawCircle(win, centre, 50, "white")
  11.     drawCircle(win, centre, 25, "blue")
  12.     drawCircle(win, centre, 10, "black")
  13.  
  14. def windowsOfEyes(rows,columns):
  15.     win = GraphWin("", 100 * rows, 100 * columns)
  16.     for i in range(rows):
  17.         for j in range(columns):
  18.             drawEye(i*100+50,j*100+50)
Sorry if I am being annoying, this function is driving me nuts
Nov 19 '09 #13
bvdet
2,851 Expert Mod 2GB
You are creating multiple canvases. Create one canvas and draw each circle to that canvas. Try this:
Expand|Select|Wrap|Line Numbers
  1. def drawCircle(win, centre, radius, colour):
  2.     circle = Circle(centre, radius)
  3.     circle.setFill(colour)
  4.     circle.setWidth(2)
  5.     circle.draw(win)
  6.  
  7. def drawEye(win,x,y):
  8.     centre = Point(x,y)
  9.     drawCircle(win, centre, 50, "white")
  10.     drawCircle(win, centre, 25, "blue")
  11.     drawCircle(win, centre, 10, "black")
  12.  
  13. def windowsOfEyes(rows,columns):
  14.     win = GraphWin("", 100 * rows, 100 * columns)
  15.     for i in range(rows):
  16.         for j in range(columns):
  17.             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.
Nov 19 '09 #14
OzzyB
15
http://www.megaupload.com/?d=24ZJ549A

try the link above for the graphic module
Nov 20 '09 #15
bvdet
2,851 Expert Mod 2GB
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.
Expand|Select|Wrap|Line Numbers
  1. import graphics
  2.  
  3. def drawCircle(win, centre, radius, colour):
  4.     circle = graphics.Circle(centre, radius)
  5.     circle.setFill(colour)
  6.     circle.setWidth(2)
  7.     circle.draw(win)
  8.  
  9. def drawEye(win,x,y):
  10.     centre = graphics.Point(x,y)
  11.     drawCircle(win, centre, 50, "white")
  12.     drawCircle(win, centre, 25, "blue")
  13.     drawCircle(win, centre, 10, "black")
  14.  
  15. def windowsOfEyes(columns,rows):
  16.     win = graphics.GraphWin("", 100 * columns, 100 * rows)
  17.     for i in range(columns):
  18.         for j in range(rows):
  19.             drawEye(win,i*100+50,j*100+50)
  20.  
  21. windowsOfEyes(4,2)
  22.  
Nov 20 '09 #16
OzzyB
15
I am so excited, it finally works. I couldn't have done it without your help thank.
Nov 20 '09 #17
OzzyB
15
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:
http://img99.imageshack.us/i/cricle.jpg/

Thanks
Nov 20 '09 #18
Glenton
391 Expert 256MB
Yes, there is a way.
Nov 20 '09 #19
Glenton
391 Expert 256MB
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...
Nov 21 '09 #20
OzzyB
15
Do I use the getX and getY?...or input the the coordinates in the range i & j.
Nov 21 '09 #21
Glenton
391 Expert 256MB
I see no reference to getX or getY in the preceding discussion. Can you explain what you mean?

The statement
Expand|Select|Wrap|Line Numbers
  1. 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:
Expand|Select|Wrap|Line Numbers
  1. for i,j in [[0,0],[1,1],[2,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.
Nov 22 '09 #22
Glenton
391 Expert 256MB
E.g. try this in terminal:
Expand|Select|Wrap|Line Numbers
  1. >>> for i,j in [[1,1],[2,2],[3,3]]:
  2. ...     print i,j
  3. ... 
  4. 1 1
  5. 2 2
  6. 3 3
  7.  
Play around in terminal, trying different things.
Nov 22 '09 #23
OzzyB
15
Expand|Select|Wrap|Line Numbers
  1. def drawCircle(win, centre, radius, colour):
  2.     circle = Circle(centre, radius)
  3.     circle.setFill(colour)
  4.     circle.setWidth(2)
  5.     circle.draw(win)
  6.  
  7. def drawEye(win,x,y):
  8.     centre = Point(x,y)
  9.     drawCircle(win, centre, 50, "white")
  10.     drawCircle(win, centre, 25, "blue")
  11.     drawCircle(win, centre, 10, "black")
  12.  
  13. def windowsOfEyes(n):
  14.     win = GraphWin("", 100 * n, 100 * n)
  15.     for i in range((0,0),(0,1),(0,2),(0,3)):
  16.         for j in range((1,0),(1,1),(1,2),(1,3)):
  17.             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
Nov 22 '09 #24
Glenton
391 Expert 256MB
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.
Nov 22 '09 #25
Glenton
391 Expert 256MB
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...
Nov 22 '09 #26
OzzyB
15
I figured it out at the end..I read your notes carefully...
Nov 23 '09 #27

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Talon | last post by:
Hi all, I am new to Tk, so please bear with me. I need someone better at math than me to help me figure this out. I am drawing multiple arcs on the same circle. All arcs start at 90 and have...
1
by: rdeaton | last post by:
I need to design and code a Java program that calculates and prints the (D) diameter, the (C) circumference, or the (A) area of a circle, given the radius. The program inputs two data items: the...
0
by: Chua Wen Ching | last post by:
Hi.. just wonder i draw a circle in the picturebox1 1) and i want to store the circle in memory (only circle) when i store into bmp... i want to see the circle with transparent...
6
by: Craig Parsons | last post by:
Folks, I have an image of a circle, which I am trying to straighten out into a flat line. I am essentially wanting to look at the image, and read a straight line from the centre, and then plot...
14
by: Pythor | last post by:
I wrote the following code for a personal project. I need a function that will plot a filled circle in a two dimensional array. I found Bresenham's algorithm, and produced this code. Please tell...
0
by: Carl Gilbert | last post by:
Hi I am trying to plot a series of shapes in a circular fashion. All shapes are evenly spaced with lines going between each shape. At present, all shapes are the same size so I can easily plot...
9
by: saraaana | last post by:
Given the center and a point on the circle, you can use this formula to find the radius of the circle. Write a program that prompts the user to enter the center and a point on the circle. The program...
7
by: heterodon7 | last post by:
hello, can anyone give me a clue or simple code on task: for example we have in 2D an equation fo circle: (x - 3)^2 + (y - 4)^2 = 25. now the program must return for example a 40 pairs of...
14
by: DeadSilent | last post by:
I have this code and for some reason I keep getting an error where it says "exporting non-public type through public api ".. I'm not sure why this keeps happening but it does so for getCircleInfo /...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.