简体   繁体   English

算法问题:如何确定矩形是否在另一个矩形内?

[英]Algorithm Questions: how to determine rectangle is inside the another rectangle?

Given a list of tuples (including x, y, width, height which four values define a rectangle in two-dimension coordination). 给定一个元组列表(包括x,y,宽度,高度,其中四个值定义了二维坐标中的矩形)。 The goal is to check in the original list, whether one rectangle is inside another rectangle (if it is, only take the smaller one, larger one is discarded) 目的是检查原始列表,一个矩形是否在另一个矩形内(如果是,则仅取较小的矩形,将较大的矩形丢弃)

The most obvious solution, if it isn't too slow, is to define rect1.contains(rect2) as: 如果不太慢,最明显的解决方案是将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

and then check every rect in the list to see if it contains any others. 然后检查列表中的每个rect以查看是否包含其他任何rect。 (Don't check it against itself, though!) (不过,请不要将其与自己对照!)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM