python - 找到 2D 中最小重叠共享表面

标签 python

考虑一个以 (0.5, 0.5) 为中心、大小已知 (0.2) 的正方形。我通过在 x 和 y 方向上随机移动原始方 block 一小部分来生成 N 个相同大小的方 block 。

我需要的是找到所有这些方 block 之间的最小重叠区域。这是,我需要它的中心坐标(引用引用正方形),以及它的高度和宽度。

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()

最佳答案

给你

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)

关于python - 找到 2D 中最小重叠共享表面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40789230/

相关文章:

python - Graphite statsd xaxis 每 2 秒

python - 有没有更有效的方法在循环中分配一个类的大量实例?

Python 在字符串列表中的搜索模式

python - 使用 pandas.read_csv 和索引读取 csv 文件会创建 NaN 条目

python - 从哪里可以获得所有已知病毒特征的列表?

Python - 将货币代码转换为其符号

python - 定期检查事件/触发器的更优雅的方式?

python - 如何使用 Django ORM 从子查询中进行选择?

python - 在Python中查找偶数斐波那契数之和的代码的内存错误

python - 为什么 `subprocess.call` 没有调用命令