我正在尝试将外框内的框(坐标)放入。我已经使用交集联合方法完成了工作,并且我希望其他方法也可以这样做。 python - 如何知道边界框(矩形)是否位于另一个边界框(矩形)内?-LMLPHP

另外,能否请您告诉我如何比较这两个内盒?

最佳答案

通过比较边界框和内部框的左上角和右下角的坐标,很容易知道后者是否在前者内部。

以下代码是一个仅包含一个边界框和一个内部框的简单示例:

# Bounding box
boundb = {
    'x': 150,
    'y': 150,
    'height': 50,
    'width': 100
}

# Inner box
innerb = {
    'x': 160,
    'y': 160,
    'height': 25,
    'width': 25
}

# If top-left inner box corner is inside the bounding box
if boundb['x'] < innerb['x'] and boundb['y'] < innerb['y']:
    # If bottom-right inner box corner is inside the bounding box
    if innerb['x'] + innerb['width'] < boundb['x'] + boundb['width'] \
            and innerb['y'] + innerb['height'] < boundb['y'] + boundb['height']:
        print('The entire box is inside the bounding box.')
    else:
        print('Some part of the box is outside the bounding box.')

10-07 18:58
查看更多