2016-08-20 10 views
2

で複数の重複矩形の共通部分の面積を見つけること:https://discuss.leetcode.com/topic/15733/my-java-solution-sum-of-areas-overlapped-area私はここに示されたアルゴリズムを使用してみましたPythonの

しかし、そのアルゴリズムは2つだけ重なった四角形の領域を見つけることを扱っています。

は、どのように私は長さを知っていれば、各矩形の幅をオーバーラップ矩形の等数を言う3、または4または5の交差領域を見つけることについてに行くのでしょうか?

+2

をあなたは_N_矩形の結合、または交差点をお探しですか?交点の場合は、重複する領域を数えたいのですか? –

+0

すべての四角形が重なっている交差点 – Falcon2908

+2

あなたの質問には、そのような重要な詳細が含まれていますので、間違った問題を解決するために時間を無駄にしないでください。 (あなたが受け取った最初の答えは、 "私はあなたが** union **の領域を見つけたいと思っています..."と始まります) –

答えて

6

Shapelyは、このようなものの良いライブラリです。

from shapely.geometry import box 

# make some rectangles (for demonstration purposes and intersect with each other) 
rect1 = box(0,0,5,2) 
rect2 = box(0.5,0.5,3,3) 
rect3 = box(1.5,1.5,4,6) 

rect_list = [rect1, rect2, rect3] 

# find intersection of rectangles (probably a more elegant way to do this) 
for rect in rect_list[1:]: 
    rect1 = rect1.intersection(rect) 
intersection = rect1 

ここで何が起こっているのかを視覚化します。私は長方形とその交点プロット:

from matplotlib import pyplot as plt 
from matplotlib.collections import PatchCollection 
from matplotlib.patches import Polygon 

# plot the rectangles before and after merging 

patches = PatchCollection([Polygon(a.exterior) for a in rect_list], facecolor='red', linewidth=.5, alpha=.5) 
intersect_patch = PatchCollection([Polygon(intersection.exterior)], facecolor='red', linewidth=.5, alpha=.5) 

# make figure 
fig, ax = plt.subplots(1,2, subplot_kw=dict(aspect='equal')) 
ax[0].add_collection(patches, autolim=True) 
ax[0].autoscale_view() 
ax[0].set_title('separate polygons') 
ax[1].add_collection(intersect_patch, autolim=True) 
ax[1].set_title('intersection = single polygon') 
ax[1].set_xlim(ax[0].get_xlim()) 
ax[1].set_ylim(ax[0].get_ylim()) 
plt.show() 

enter image description here

+0

Mac OS Xでshapely.geometryをインストールするにはどうすればいいですか? – Falcon2908

+1

文書をご覧ください:https://pypi.python.org/pypi/Shapely。 Anaconda distを使用している場合は、コマンドラインで 'conday install shapely'を使用できます(推奨)。 – benten

+0

申し訳ありません、私はcondaを使用してコードのインストール行を実行しましたが、 "usage:install [-bCcpSsv] [-B suffix] [-fフラグ] [-g group] [-m mode] [-o owner ] FILE1 FILE2 インストール[-bCcpSsv] [-Bサフィックス] [-fフラグ] [-gグループ] [-mモード] [所有者-o] FILE1 ... fileNディレクトリ は[-v] [-dインストール-g group] [-m mode] [-o owner] directory ... "今何をすればいいのですか? – Falcon2908

関連する問題