我想獲得幾個重疊矩形的外邊界。計算相鄰矩形的邊界
我寫了一個函數來計算給定的矩形的鄰居,但我想看看是否有辦法獲得相鄰矩形的外邊界。
這是我有:
def get_neighboring_rectangles (r, rectList):
overlapDict = {}
rminX, rmaxX, rminY, rmaxY = return_bbox_of_rectangle(r)
for rectCheck,ri in rectList:
if r == rectCheck: continue
rcminX, rcmaxX, rcminY, rcmaxY = return_bbox_of_rectangle(rectCheck)
for d in ['E', 'W', 'N', 'S']:
if not ((rcmaxY < rminY or rcminY > rmaxY) or (rcmaxY == rminY) or (rcminY == rmaxY)):
if d == 'W' and rcmaxX == rminX:
if d not in overlapDict: overlapDict[d] = []
overlapDict[d].append((rectCheck,ri))
if d == 'E' and rcminX == rmaxX:
if d not in overlapDict: overlapDict[d] = []
overlapDict[d].append((rectCheck,ri))
if not ((rcmaxX < rminX or rcminX > rmaxX) or (rcmaxX == rminX) or (rcminX == rmaxX)):
if d == 'S' and rcmaxY == rminY:
if d not in overlapDict: overlapDict[d] = []
overlapDict[d].append((rectCheck,ri))
if d == 'N' and rcminY == rmaxY:
if d not in overlapDict: overlapDict[d] = []
overlapDict[d].append((rectCheck,ri))
return overlapDict
上述程序返回每個方向上的鄰居:E,W,N,S。
下面是此功能覆蓋情況:
函數只返回什麼是近鄰,但我希望有一個功能來返回所有對接矩形的外邊界:
在這種情況下,輸入將所有3個矩形的邊界列表:
[[(0,0) (7,0) (7,4) (0,4)] [(4,4) (7,4) (7,6) (4,6)] [(1,6) (7,6) (7,10) (1,10)]]
預期輸出 - [(0,0) (7,0) (7,10) (1,10) (1,6) (4,6) (4,4) (0,4)]
而且,請注意,有可能是不對接了上述3個矩形的第四矩形。理想情況下,第四個邊界應該返回原樣。 (我可以使用我的鄰居檢測算法完成)
我有點失落,我該如何解決這個問題。有沒有一個Python庫具有這種功能?
嘿,你可以添加輸入的預期輸出? – Neil
感謝您指出。我現在添加了它。 – sanjay