Hello
I am quite stuck on a python exercise and not quite sure how to finish it.
My task is to draw an eye using a graphics module, and when the user clicks the mouse on the eye, the program is to specify what part of the eye was clicked on, so either the pupil, white or iris, and to continue this until the user clicks outside the eye to quit the window.
I have sort of managed this but I can't get it to repeat.

def drawCircle(win, centre, radius, colour):
    circle = Circle(centre, radius)
    circle.setFill(colour)
    circle.setWidth(2)
    circle.draw(win)
    
def drawEye(win, centre):
    for circles in range(1):
        drawCircle(win, centre, 50, "white")
        drawCircle(win, centre, 20, "blue")
        drawCircle(win, centre, 10, "black")
def click():    
    win=GraphWin("Clickable eye",900, 300)
    centre=Point(150,150)
    for circles in range(1):
        drawCircle(win, centre , 100, "white")
        drawCircle(win, centre, 40, "blue")
        drawCircle(win, centre, 20, "black")
    p = win.getMouse()
    iris1x=130
    iris1y=130
    iris2x=170
    iris2y=170
    pupil1x=110
    pupil2x=190
    pupil1y=110
    pupil2y=190
    white1x=50
    white2x=250
    white1y=50
    white2y=250
    clickx=p.getX()
    clicky=p.getY()
    while True:
        if clickx>=iris1x and clickx <=iris2x and clicky>=iris1y and clicky<=iris2y:
            message = Text(Point(200,280), "pupil")
            message.draw(win)
        elif clickx>=pupil1x and clickx<=pupil2x and clicky>=pupil1y and clicky<=pupil2y:
            message = Text(Point(150,280), "Iris")
            message.draw(win)
        elif clickx>=white1x and clickx<=white2x and clicky>=white1y and clicky<=white2y:
            message= Text(Point(150,280), "white")
            message.draw(win)
        else:
            win.close()

If anyone could help me get this to work and simplify it I would be greatly thankful.

if clickx>=iris1x and clickx <=iris2x and clicky>=iris1y and clicky<=iris2y:

If you are dealing with eyes, etc. that are circles then your test doesn't work. I think you would have to
1. take the distance of the mouse-click x-coordinate from the center of the circle
2. make sure the difference is less than the radius
3. do the same for the y-coordinate
4. use the distance from the x-coordinate to the center, as the base of a right angle triangle, and then calculate the y distance to the edge of the circle, using the radius as the hypotenuse, and of course in both directions since one can be closer to the edge.
5. check that the length found in #3 is less than #4

Just create one function and pass the center point, radius, and mouse x,y. You can then use 2 function calls at most to tell if it is in the eye or outside, and if in the eye, in the iris or outside.

This article has been dead for over six months. Start a new discussion instead.