我正在构建一个与 Google Places API 交互的程序,以识别美国县内给定类型的所有机构。谷歌接受半径形式的搜索——所以为了覆盖整个区域,我正在按顺序构建我的搜索半径。但是,此算法会创建很多我想过滤掉的重叠圆圈。所以:
给定一个圆列表,每个圆的中心和半径,我如何判断一个圆是否被其他圆的任何组合完全覆盖?
我已经可以判断一个圆圈是否被另一个圆圈包围 - 我的问题是其中很多被其他几个圆圈包围。
有人询问我现有的代码——我目前有测试一个圆圈是否与另一个圆圈完全重叠的代码——而不是它们的组合。但这就是我所拥有的。您可以看到,如果它与其他 20 个圆圈重叠,我正在通过排除它来近似当前问题,此时它可能被包含在内:
def radiusIsInsidePreviousQuery(self, testQuery):
newSearchCoordinates = (testQuery['center']['lat'], testQuery['center']['lng'])
alreadyBeenSearched = False
numberOfIntersectingCircles = 0
for queryNumber in self.results.keys():
previousQuery = self.results[queryNumber]
previousSearchCoordinates = (previousQuery['center']['lat'],
previousQuery['center']['lng'])
centroidDistance = VincentyDistance(newSearchCoordinates,
previousSearchCoordinates)
centroidDistanceMeters = centroidDistance.meters
newQueryRadius = testQuery['radius']
fullSearchDistance = centroidDistanceMeters + newQueryRadius
#If the full search distance (the sum of the distance between
#the two searches' centroids and the new search's radius) is less
#than the previous search's radius, then the new search is encompassed
#entirely by the old search.
previousQueryRadius = previousQuery['radius']
if fullSearchDistance <= previousQueryRadius:
print "Search area encompassed"
alreadyBeenSearched = True
elif centroidDistanceMeters < newQueryRadius + previousQueryRadius:
numberOfIntersectingCircles += 1
elif self.queriesAreEqual(testQuery, previousQuery):
print "found duplicate"
alreadyBeenSearched = True
#If it intersects with 20 other circles, it's not doing any more good.
if numberOfIntersectingCircles > 20:
alreadyBeenSearched = True
return alreadyBeenSearched