简体   繁体   中英

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). 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.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. (Don't check it against itself, though!)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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