[英]Algorithm required to determine if a rectangle is completely covered by another set of rectangles
[英]Algorithm to compute the difference between a set of rectangles and a single rectangle
假設我有一個紅色矩形,其坐標為(x,y)
、 (x1,y)
、 (x1,y1)
、 (x,y1)
,它與未定義數量的不同藍色矩形(可能具有不同的寬度和/ 或高度)並隨機放置在一個表面上(它們甚至可能根本不與紅色矩形重疊)。 無論如何,是這樣的:
我將藍色矩形存儲在一個rectangles
數組中,並且我也有每個藍色矩形的坐標。
現在,我需要做的是選擇紅色矩形中不與rectangles
數組的任何藍色矩形重疊的部分(基本上我需要在所有藍色矩形和紅色矩形的並union
之間進行區分) . 在前面的示例中,這些部分將是(由黑色邊框分隔):
而且,我需要從這些部分計算新的矩形,並將結果作為數組。 因此,之前的部分應該變成以下矩形:
或者下面的矩形(它們是如何生成的並不重要,當然越少越好(但看下圖我猜,考慮到它的一部分,不能少於n
矩形,它們將始終是n
,在這種情況下n = 13
(如果我錯了,請糾正我))):
之后,我應該返回一個包含這些由紅色矩形部分創建的矩形的數組。
所以要求是:
請注意,我們不是在類似網頁的表面上,這意味着x < x1
像在網絡上( left
< right
),但是y > y1
(而在網絡top
< bottom
),因此我有以下Rectangle
數據具有overlaps()
方法的結構(偽代碼):
class Rectangle {
this.x,
this.x1,
this.y,
this.y1;
/**
* @constructor
*/
Rectangle(x, y, x1, y1) {
this.x = x;
this.y = y;
this.x1 = x1;
this.y1 = y1;
}
/**
* Tests whether this rectangle overlaps or is overlapped by the rectangle given as parameter.
*
* @returns True if the rectangle is overlapped or overlaps the rectangle given as parameter, false otherwise.
*/
Boolean overlaps(Rectangle rect) {
compareX = function(coordX1, coordX2) {
less = coordX1[0] > coordX2[0] ? coordX2: coordX1;
greater = coordX1[0] > coordX2[0] ? coordX1: coordX2;
return less[1] > greater[0] || less[0] == greater[0];
}
compareY = function(coordY1, coordY2) {
less = coordY1[0] > coordY2[0] ? coordY2 : coordY1;
greater = coordY1[0] > coordY2[0] ? coordY1 : coordY2;
return greater[1] < less[0] || greater[0] == less[0];
}
coordX1 = [this.x, this.x1]; // [] brackets create a new array
coordX2 = [rect.x, rect.x1];
coordY1 = [this.y, this.y1];
coordY2 = [rect.y, rect.y1]
return compareX(coordX1, coord) && compareY(coordY1, coordY2);
}
}
現在,計算差異矩形的有效方法是什么? (例如一個differenceRectangles
函數)
//...
blueRectangles = [ blueRect1, blueRect2, blueRect3, ... ] // This is an array of blue rectangles, I don't know how many there are, but each item of the array is a `Rectangle` data structure with an `overlaps()` method
redRectangle = new Rectangle(x, x1, y, y1);
differenceRectangles = differenceRectangles(blueRectangles, redRectangle);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.