Reputation: 55
In my program I have three circles drawn in the graphics window, and I need different responses to occur depending on which one the user clicks on.
cup1 = Circle(Point(35,100),25)
cup1.draw(win)
cup2 = cup1.clone()
cup2.move(65,0)
cup2.draw(win)
cup3 = cup1.clone()
cup3.move(130,0)
cup3.draw(win)
So I need something that would work like this:
userchoice = win.getMouse()
cup1choice = False
cup2choice = False
cup3choice = False
if userchoice in cup1:
cup1choice = True
if userchoice in cup2:
cup2choice = True
if userchoice in cup3:
cup3choice = True
But I realize that a Circle is not iterable like that, so I'm looking for some other kind of method to determine whether the user is clicking inside cup 1, 2, or 3. If anyone can help I'd really appreciate it
Upvotes: 0
Views: 1858
Reputation: 122032
You already have the centre and radius of each Circle
, you could write a function to determine whether a given Point
is within that as follows:
from math import sqrt
def is_within(point, circle):
distance = sqrt(((point.x - circle.x) ** 2) +
((point.y - circle.y) ** 2))
return distance < circle.radius
Note that you will have to tweak the attribute names according to the graphics library you are using.
Upvotes: 1