在2D中找到最小重叠的共享曲面

时间:2016-11-24 14:44:32

标签: python

考虑一个以(0.5, 0.5)为中心且已知大小(0.2)的方格。我通过在x和y中随机移动原始的一小部分来生成更多相同大小的N个方块。

我需要的是在所有这些方块之间找到最小重叠区域。这是,我需要它的中心坐标(参考参考方块),以及它的高度和宽度。

enter image description here

我已经玩了一段时间了,我发现没有简单的方法可以做到这一点。任何帮助将不胜感激。

import numpy as np
from matplotlib import pyplot as plt
from matplotlib.patches import Rectangle
from itertools import cycle

xc, yc = 0.4, 0.4
plt.figure()
currentAxis = plt.gca(aspect='equal')
currentAxis.add_patch(Rectangle((xc, yc), 0.2, 0.2, fill=None, alpha=1, lw=2.))
plt.scatter(0.5, 0.5)
print("Reference square centered at (0.5, 0.5)")

cols = ['r', 'b', 'g', 'm', 'c']
col_cyc = cycle(cols)
for _ in range(4):
    xs, ys = np.random.uniform(-0.1, 0.1, 2)
    print("Square {} shifted by: {:.3f}, {:.3f}".format(_, xs, ys))
    currentAxis.add_patch(
        Rectangle((xc + xs, yc + ys), 0.2, 0.2, fill=None, alpha=1,
                  color=next(col_cyc)))
plt.xlim(0.2, 0.8)
plt.ylim(0.2, 0.8)
plt.show()

3 个答案:

答案 0 :(得分:1)

如果我明白了,你想要的就是你生成的所有方块的交叉区域。

在这种情况下,截取区域是具有位置(max(xs),max(ys))和大小(0.2 - (max(xs)-min(xs)),0.2 - (max(xs))的矩形-min(XS))。

在您的代码中:

import numpy as np
from matplotlib import pyplot as plt
from matplotlib.patches import Rectangle
from itertools import cycle

xc, yc = 0.4, 0.4
plt.figure()
currentAxis = plt.gca(aspect='equal')
currentAxis.add_patch(Rectangle((xc, yc), 0.2, 0.2, fill=None, alpha=1, lw=2.))
plt.scatter(0.5, 0.5)
print("Reference square centered at (0.5, 0.5)")

xs_list = []
ys_list = []

cols = ['r', 'b', 'g', 'm', 'c']
col_cyc = cycle(cols)
for _ in range(4):
    xs, ys = np.random.uniform(-0.1, 0.1, 2)
    xs_list.append(xs)
    ys_list.append(ys)
    print("Square {} shifted by: {:.3f}, {:.3f}".format(_, xs, ys))
    currentAxis.add_patch(
        Rectangle((xc + xs, yc + ys), 0.2, 0.2, fill=None, alpha=1,
                  color=next(col_cyc)))

# This is the actual intersecting region
currentAxis.add_patch(
        Rectangle((max(xs_list)+xc, max(ys_list)+yc), 0.2 - (max(xs_list)-min(xs_list)), 
                    0.2 - (max(ys_list)-min(ys_list)), fill=True, alpha=1,
                      color=next(col_cyc)))

plt.xlim(0.2, 0.8)
plt.ylim(0.2, 0.8)
plt.show()

答案 1 :(得分:1)

你去了

import numpy as np
#size of squares
size=0.2

#centers of all squares [x,y]
centers=[[0.5,0.5],
        [0.45,0.45],
        [0.6,0.6]]

centers=np.asarray(centers) #convert to numpy array

left_overlap=centers[::,0].max()-size #left border
right_overlap=centers[::,0].min()+size #right border
bottom_overlap=centers[::,1].max()-size #bottom border
top_overlap=centers[::,1].min()+size #top border

if left_overlap>right_overlap or bottom_overlap>top_overlap:
    print "No overlap"
else:
    x_center_overlap=(left_overlap+right_overlap)/2. #center x
    y_center_overlap=(bottom_overlap+top_overlap)/2. #center y

    print "Center of overlapping area is %s,%s"%(x_center_overlap,y_center_overlap)

答案 2 :(得分:1)

右边的最大偏移平方给出左边坐标,最大偏移顶部给出底部坐标,等等。

import numpy as np
from matplotlib import pyplot as plt
from matplotlib.patches import Rectangle
from itertools import cycle

xc, yc = 0.4, 0.4
plt.figure()
currentAxis = plt.gca(aspect='equal')
currentAxis.add_patch(Rectangle((xc, yc), 0.2, 0.2, fill=None, alpha=1, lw=2.))
plt.scatter(0.5, 0.5)
print("Reference square centered at (0.5, 0.5)")

cols = ['r', 'b', 'g', 'm', 'c']
col_cyc = cycle(cols)

x_crds=[]
y_crds=[]

for _ in range(4):
    xs, ys = np.random.uniform(-0.1, 0.1, 2)
    x_crds.append(xs)
    y_crds.append(ys)
    print("Square {} shifted by: {:.3f}, {:.3f}".format(_, xs, ys))
    currentAxis.add_patch(
        Rectangle((xc + xs, yc + ys), 0.2, 0.2, fill=None, alpha=1,
                  color=next(col_cyc)))
left  =np.max(x_crds)+xc
bottom=np.max(y_crds)+yc
right =np.min(x_crds)+xc+.2
top   =np.min(y_crds)+yc+.2

currentAxis.add_patch(
    Rectangle((left, bottom), right-left, top-bottom, fill=1, alpha=.5,
              color=next(col_cyc)))
plt.xlim(0.2, 0.8)
plt.ylim(0.2, 0.8)
plt.show()