检查点击是否在图形对象内[Python图形模块] [英] Checking if clicks are within a graphic object [Python Graphics Module]

查看:196
本文介绍了检查点击是否在图形对象内[Python图形模块]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我正在使用的模块: 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))

而win中的目标:
click = win.getMouse()
如果点击目标:
print(Good job)

我遗漏了绘制目标形状的代码,因为它的长度和不必要。 它是一个动态的循环。



我使用了一个while循环,因此它可以不断获得用户的点击次数。



如何使用getMouse()命令检查用户的点击是否在指定的目标形状中?

/ div>

Circle



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

#检查pt1是否在循环
def inCircle(pt1, )

#使用
#距离公式计算pt1和circ之间的距离
dx = pt1.getX() - circ.getCenter()。getX()
dy = pt1.getY() - circ.getCenter()。getY()
dist = math.sqrt(dx * dx + dy * dy)

检查距离小于半径
return dist <= circ.getRadius()

def main():
win = GraphWin(点击速度,700,700)

#创建一个简单的圆圈
circ =圆形(点(350,350),50)
circ.setFill(红色)
circ.draw(win)

而真:
mouse = win.getMouse()
如果inCircle(鼠标,circ):
print(Good job)

main()



椭圆形



对于更高级的椭圆例子,我们需要使用一个公式此处。这里是函数实现:

  def inOval(pt1,oval):

)得到半径
rx = abs(oval.getP1()。getX() - oval.getP2()。getX())/ 2
ry = abs(oval.getP1()。getY () - get_value() - get_value() - getvalue()。getY())/ 2

#获取中心
h = oval.getCenter()。getX()
k = oval.getCenter ).getY()

#得到点
x = pt1.getX()
y = pt1.getY()

#使用公式
return(xh)** 2 / rx ** 2 +(yk)** 2 / ry ** 2 <= 1



$ p

多边形



对于abitrary形状的多边形,我们需要参考。我已经把它转换成一个python等价物给你。检查链接,看看它为什么工作,因为我真的不知道

  def inPoly(pt1,poly):

points = poly.getPoints()
nvert = len(points)#多边形中顶点的数量

#get x和y pt1
x = pt1 .getX()
y = pt1.getY()

#我不知道为什么会这样工作
#请参阅我提供的链接详细信息
result = False
我在范围内(nvert):

#注意:points [-1]会给你最后一个元素
#方便!
j = i - 1

#get x和y的顶点索引i
vix = points [i] .getX()
viy = points [i] .getY()

#get x和y的顶点在索引j
vjx = points [j] .getX()
vjy = points [j] .getY() (vyy> y)和(x <(vjx-vix)*(y-viy)/(vjy -viy)+ vix):

: b $ b结果=不是结果

返回结果


Here is the module I'm using: http://mcsp.wartburg.edu/zelle/python/graphics/graphics.pdf

I want to see whether a user's clicks are within a shape or not. I used the in operator, but I know that is incorrect. Below is a chunk of my code:

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")

I left out the code that draws theTarget shape because it is length and unnecessary. It is a moving circle.

I'm using a while loop so it allows me to constantly get the user's clicks.

How do I go about checking whether or not a user's clicks are in the specified Target shape by using the getMouse() command?

I'm going to have to use this in the future for more abstract shapes (not simple circles).

解决方案

Circle

For the simple case of a circle, you can determine whether the mouse is inside using the distance formula. For example:

# 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()

Oval

For the more advanced example of an ellipse we will need to use a formula found here. Here is the function implemting that:

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

Polygon

For a polygon of abitrary shape we need to reference this. I have converted that to a python equivalent for you. Check the link to see why it works because I am honestly not sure

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

这篇关于检查点击是否在图形对象内[Python图形模块]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆