簡體   English   中英

嵌套列表中的唯一性具有不超過一對重疊的坐標

[英]Uniqueness in nested list to have no more than one overlapping pair of coordinates

因此,我經歷了一大堆圓圈, 像這樣檢測它們重疊的位置,現在我繪制了重疊的圓圈。

但是在拐角處,如箭頭所示,我有兩個紅色的圓圈與一個藍色的圓圈重疊,但是應該只檢測到一個重疊的圓圈。 根據距離,第二個重疊的圓應忽略不計。

如何刪除多余的重疊,以便一個紅色將始終與一個藍色重疊,但也將與另一個重疊?

在此處輸入圖片說明

import matplotlib.pyplot as plt    

# Format is (x1, y1, r1), x2, y2, r2), squared_distance)
circles = (((87, 319, 10), (82, 316, 10), 34),
           ((162, 230, 10), (157, 226, 10), 41),
           ((162, 438, 10), (162, 440, 10), 4),
           ((235, 146, 10), (230, 150, 10), 41),
           ((260, 183, 10), (260, 185, 10), 4),
           ((260, 265, 10), (253, 269, 10), 65),
           ((360, 88, 10), (366, 91, 10), 45),
           ((428, 442, 10), (433, 447, 10), 50), # Two red overlap the same blue
           ((438, 453, 10), (433, 447, 10), 61), # So this one (furthest away) must go
           ((459, 24, 10), (465, 21, 10), 45))

fig, ax = plt.subplots(figsize = (6,6))

ax.set_xlim(0,500)
ax.set_ylim(0,500)

for red, blue, squared_dist in circles:
    x1, y1, r1 = red
    x2, y2, r2 = blue

    c = plt.Circle((x1, y1), r1, color = "red", linewidth = 2, fill = False, alpha = 1)
    ax.add_patch(c)

    c = plt.Circle((x2, y2), r2, color = "blue", linewidth = 2, fill = False, alpha = 1)
    ax.add_patch(c)

ax.arrow(390, 400, 20, 20, head_width=10, head_length=10, fc='k', ec='k')
plt.show()

好吧,所以我用熊貓解決了這個問題。 事實證明,像上面這樣的嵌套列表實際上非常容易用作容器。

將原始圓放入數據框:

df = pd.DataFrame(circles, columns = ["red", "blue", "dist"])

              red            blue  dist
0   (87, 319, 10)   (82, 316, 10)    34
1  (162, 230, 10)  (157, 226, 10)    41
2  (162, 438, 10)  (162, 440, 10)     4
3  (235, 146, 10)  (230, 150, 10)    41
4  (260, 183, 10)  (260, 185, 10)     4
5  (260, 265, 10)  (253, 269, 10)    65
6   (360, 88, 10)   (366, 91, 10)    45
7  (428, 442, 10)  (433, 447, 10)    50 
8  (438, 453, 10)  (433, 447, 10)    61 
9   (459, 24, 10)   (465, 21, 10)    45 

然后,如果按距離排序,則只需丟棄重復項即可。

df = df.sort_values("dist").drop_duplicates("red").drop_duplicates("blue").reset_index(drop = True)

屈服

              red            blue  dist
0   (87, 319, 10)   (82, 316, 10)    34
1  (162, 230, 10)  (157, 226, 10)    41
2  (162, 438, 10)  (162, 440, 10)     4
3  (235, 146, 10)  (230, 150, 10)    41
4  (260, 183, 10)  (260, 185, 10)     4
5  (260, 265, 10)  (253, 269, 10)    65
6   (360, 88, 10)   (366, 91, 10)    45
7  (428, 442, 10)  (433, 447, 10)    50
9   (459, 24, 10)   (465, 21, 10)    45

然后刪除第8行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM