检查点击是否在图形对象内 [Python 图形模块]
Checking if clicks are within a graphic object [Python Graphics Module]
这是我正在使用的模块:http://mcsp.wartburg.edu/zelle/python/graphics/graphics.pdf
我想看看用户的点击是否在一个形状内。我使用了 in 运算符,但我知道这是不正确的。下面是我的代码块:
win = GraphWin("Click Speed", 700, 700)
theTarget = drawTarget(win, random.randrange(0,685), random.randrange(0,685))
while theTarget in win:
click = win.getMouse()
if click in theTarget:
print("Good job")
我省略了绘制目标形状的代码,因为它很长而且没有必要。 是一个动圈
我正在使用 while 循环,这样我就可以不断获得用户的点击。
如何使用 getMouse() 命令检查用户的点击是否在指定的目标形状中?
我以后要用这个来做更抽象的形状(不是简单的圆圈)。
圆形
对于一个简单的圆圈,可以通过距离公式判断鼠标是否在里面。例如:
# checks whether pt1 is in circ
def inCircle(pt1, circ):
# get the distance between pt1 and circ using the
# distance formula
dx = pt1.getX() - circ.getCenter().getX()
dy = pt1.getY() - circ.getCenter().getY()
dist = math.sqrt(dx*dx + dy*dy)
# check whether the distance is less than the radius
return dist <= circ.getRadius()
def main():
win = GraphWin("Click Speed", 700, 700)
# create a simple circle
circ = Circle(Point(350,350),50)
circ.setFill("red")
circ.draw(win)
while True:
mouse = win.getMouse()
if inCircle(mouse,circ):
print ("Good job")
main()
椭圆形
对于更高级的椭圆示例,我们需要使用找到的公式 here。这是实现的功能:
def inOval(pt1, oval):
# get the radii
rx = abs(oval.getP1().getX() - oval.getP2().getX())/2
ry = abs(oval.getP1().getY() - oval.getP2().getY())/2
# get the center
h = oval.getCenter().getX()
k = oval.getCenter().getY()
# get the point
x = pt1.getX()
y = pt1.getY()
# use the formula
return (x-h)**2/rx**2 + (y-k)**2/ry**2 <= 1
多边形
对于任意形状的多边形,我们需要参考this。我已将其转换为 python 等价物。检查 link 看看它为什么有效,因为老实说我不确定
def inPoly(pt1, poly):
points = poly.getPoints()
nvert = len(points) #the number of vertices in the polygon
#get x and y of pt1
x = pt1.getX()
y = pt1.getY()
# I don't know why this works
# See the link I provided for details
result = False
for i in range(nvert):
# note: points[-1] will give you the last element
# convenient!
j = i - 1
#get x and y of vertex at index i
vix = points[i].getX()
viy = points[i].getY()
#get x and y of vertex at index j
vjx = points[j].getX()
vjy = points[j].getY()
if (viy > y) != (vjy > y) and (x < (vjx - vix) * (y - viy) / (vjy - viy) + vix):
result = not result
return result
这是我正在使用的模块:http://mcsp.wartburg.edu/zelle/python/graphics/graphics.pdf
我想看看用户的点击是否在一个形状内。我使用了 in 运算符,但我知道这是不正确的。下面是我的代码块:
win = GraphWin("Click Speed", 700, 700)
theTarget = drawTarget(win, random.randrange(0,685), random.randrange(0,685))
while theTarget in win:
click = win.getMouse()
if click in theTarget:
print("Good job")
我省略了绘制目标形状的代码,因为它很长而且没有必要。 是一个动圈
我正在使用 while 循环,这样我就可以不断获得用户的点击。
如何使用 getMouse() 命令检查用户的点击是否在指定的目标形状中?
我以后要用这个来做更抽象的形状(不是简单的圆圈)。
圆形
对于一个简单的圆圈,可以通过距离公式判断鼠标是否在里面。例如:
# checks whether pt1 is in circ
def inCircle(pt1, circ):
# get the distance between pt1 and circ using the
# distance formula
dx = pt1.getX() - circ.getCenter().getX()
dy = pt1.getY() - circ.getCenter().getY()
dist = math.sqrt(dx*dx + dy*dy)
# check whether the distance is less than the radius
return dist <= circ.getRadius()
def main():
win = GraphWin("Click Speed", 700, 700)
# create a simple circle
circ = Circle(Point(350,350),50)
circ.setFill("red")
circ.draw(win)
while True:
mouse = win.getMouse()
if inCircle(mouse,circ):
print ("Good job")
main()
椭圆形
对于更高级的椭圆示例,我们需要使用找到的公式 here。这是实现的功能:
def inOval(pt1, oval):
# get the radii
rx = abs(oval.getP1().getX() - oval.getP2().getX())/2
ry = abs(oval.getP1().getY() - oval.getP2().getY())/2
# get the center
h = oval.getCenter().getX()
k = oval.getCenter().getY()
# get the point
x = pt1.getX()
y = pt1.getY()
# use the formula
return (x-h)**2/rx**2 + (y-k)**2/ry**2 <= 1
多边形
对于任意形状的多边形,我们需要参考this。我已将其转换为 python 等价物。检查 link 看看它为什么有效,因为老实说我不确定
def inPoly(pt1, poly):
points = poly.getPoints()
nvert = len(points) #the number of vertices in the polygon
#get x and y of pt1
x = pt1.getX()
y = pt1.getY()
# I don't know why this works
# See the link I provided for details
result = False
for i in range(nvert):
# note: points[-1] will give you the last element
# convenient!
j = i - 1
#get x and y of vertex at index i
vix = points[i].getX()
viy = points[i].getY()
#get x and y of vertex at index j
vjx = points[j].getX()
vjy = points[j].getY()
if (viy > y) != (vjy > y) and (x < (vjx - vix) * (y - viy) / (vjy - viy) + vix):
result = not result
return result