【问题标题】:Checking if clicks are within a graphic object [Python Graphics Module]检查点击是否在图形对象内 [Python 图形模块]
【发布时间】:2015-07-20 02:29:45
【问题描述】:

这是我正在使用的模块: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() 命令检查用户的点击是否在指定的目标形状中?

以后我将不得不使用它来制作更抽象的形状(而不是简单的圆圈)。

【问题讨论】:

    标签: python python-3.x graphics click window


    【解决方案1】:

    对于圆形的简单情况,您可以使用距离公式确定鼠标是否在里面。例如:

    # 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 等价物。检查链接以了解它为什么有效,因为我真的不确定

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-12-23
      • 2014-04-26
      • 1970-01-01
      • 2021-11-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多