简而言之,我想制作各种类型的雷达,特别是使用graphics.py库,它可以检测点上是否存在预先绘制的Shape。
下面我已经起草了一个代码,它会向我显示雷达内的所有点(并将其作为数组返回),但我希望能够获取一个点/坐标并检查它是否在内部圆形或矩形等没有知道其他任何东西(即只有x和y参数,不知道其他现有形状)。类似的东西:
if Point(x,y).getObject() is None:
return False
甚至
if Point(x,y).getObject().config["fill"] == "#f00":
return True
这是我到目前为止所做的事情:
from graphics import *
def getAllPointsInRadius(circle, win):
cx = int(circle.getCenter().getX())
cy = int(circle.getCenter().getY())
r = circle.getRadius()# + 2
points = []
print cx, cy
for i in range(cx-r, cx+r):
for j in range(cy-r, cy+r):
x = i-circle.getRadius()
y = j - circle.getRadius()
if ((i - cx) * (i - cx) + (j - cy) * (j - cy) <= r * r):
points.append(Point(i,j)) #if within, append as Point
p = Point(i, j)
p.setFill('#551A8B')
p.draw(win) #illustrate all points inside radar
else:
p = Point(i, j)
p.setFill('#0000ff')
p.draw(win) #points outside
return points
win = GraphWin('Radar', width = 500, height = 500)
win.setCoords(0, 0, 30, 30)
win.setBackground('black')
#object to be detected
objectCircle = Circle(Point(11, 12), 2)
objectCircle.setFill('#f00')
objectCircle.draw(win)
#radius within objects may be detected
radiusCircle = Circle(Point(10, 10), 3)
radiusCircle.setOutline('#fff')
radiusCircle.draw(win)
print getAllPointsInRadius(radiusCircle, win)
#print Point(11, 12).config["outline"]
win.getMouse()
附录:我想也许通过制作Point(x,y)我实际创建一个点并且不返回坐标。不过,我不知道怎么办。