gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 00:43:33 25 4
gpt4 key购买 nike

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

我需要的是找到所有这些方 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com