给定一个元组列表(包括 x, y, width, height 这四个值以二维坐标定义一个矩形)。 目标是在原始列表中检查一个矩形是否在另一个矩形内部(如果是,则只取较小的,较大的丢弃)
最佳答案
如果不是太慢,最明显的解决方案是将 rect1.contains(rect2)
定义为:
rect1.x <= rect2.x && rect1.y <= rect2.y && rect1.x + rect1.width >= rect2.x + rect2.width && rect1.y + rect1.height >= rect2.y + rect2.height
然后检查列表中的每个矩形,看看它是否包含任何其他矩形。 (不过,不要检查它本身!)
关于算法题: how to determine rectangle is inside the another rectangle?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38531331/