在两个数据集之间找到交集

6

我正在生成两个类似于这样的数组:

[x,y,z] = sphere;
A=[x,y,z]
B=[x+0.5,y+0.5,z+0.5]

第二个数组相对于第一个数组有一个偏移量。
我想找到这两个数组A和B的交集空间。
在这种情况下,我使用了球形函数,但是是否可以针对任意两个数据数组进行操作,而不仅限于球形。有没有办法做到这一点?
我附加了一张图片,展示我所寻找的内容。我想要找到这两个区域之间的交集。但是你可以看到值不一定相同。
如果我有每个空间限制的方程式,那么问题是否会更容易解决?

enter image description here


我不太确定你从交集中期望得到什么结果?你是指你有一个边界的表达式吗?那么你只需要找到两个边界相交的点,对吧? - Bernhard
没错,我可以找到边界相交的地方。所以有两个点[x1,y1]和[x2,y2]。然而这还不足以找到存在于交集中的所有数据点。有没有什么办法可以做到这一点?如果这是一个维恩图,那么所有在两个交集中的点都会被包括进来。 - Trippy
在这种情况下,两个边界交点大约是[-2,4]和[4,-2]。 - Trippy
3
我会在两个空间上使用convhull函数,然后使用inpolygon函数来检查交集中的点。 - BillBokeey
我正在发布一个使用delaunayTriangulation的答案,希望能得到你的建议 @Dan - BillBokeey
显示剩余3条评论
1个回答

9

我在评论中提到可以使用convhullinpolygon来解决这个问题,但是inpolygon似乎不适用于3D多边形。我们将使用delaunayTriangulationpointLocation来得到结果。

完整代码:

[x,y,z] = sphere;
A=[x(:),y(:),z(:)];
B=[x(:)+0.5,y(:)+0.5,z(:)+0.5];

tess1=delaunayTriangulation(A); % delaunay Triangulation of points set A
tess2=delaunayTriangulation(B); % delaunay Triangulation of points set B

Tmp=[A;B];

% Point location searches for the triangles in the given delaunay     
% triangulation that contain the points specified in Tmp, here Tmp is 
% the reunion of sets A and B and we check for both triangulations
ids1=~isnan(pointLocation(tess1,Tmp));
ids2=~isnan(pointLocation(tess2,Tmp));

% ids1&ids2 is a logical array indicating which points
% in Tmp are in the intersection
IntersectPoints=Tmp(ids1&ids2,:);


plot3(A(:,1),A(:,2),A(:,3),'+b'); hold on
plot3(B(:,1),B(:,2),B(:,3),'+g');
plot3(IntersectPoints(:,1),IntersectPoints(:,2),IntersectPoints(:,3),'*r')

输出:

在此输入图像描述

编辑 - 二维示例:

[x,y,z] = sphere;
A=[x(:),y(:)];
B=[x(:)+0.5,y(:)+0.5];

tess1=delaunayTriangulation(A); % delaunay Triangulation of points set A
tess2=delaunayTriangulation(B); % delaunay Triangulation of points set B

Tmp=[A;B];

% Point location searches for the triangles in the given delaunay     
% triangulation that contain the points specified in Tmp, here Tmp is 
% the reunion of sets A and B and we check for both triangulations
ids1=~isnan(pointLocation(tess1,Tmp));
ids2=~isnan(pointLocation(tess2,Tmp));

% ids1&ids2 is a logical array indicating which points
% in Tmp are in the intersection
IntersectPoints=Tmp(ids1&ids2,:);


plot(A(:,1),A(:,2),'+b'); hold on
plot(B(:,1),B(:,2),'+g');
plot(IntersectPoints(:,1),IntersectPoints(:,2),'*r');

输出:

在此输入图像描述

编辑2:

如果您希望您的代码可以自动适应2D或3D数组,则只需要修改绘图调用。只需编写一个if语句来检查A和B中的列数即可。


我使用了一个函数,用于替换该方程中theta1和theta2的值,取值范围为0->360。[cos(theta1(t)) + cos(theta1(t) + theta2(t)), sin(theta1(t)) + sin(theta1(t) + theta2(t)), 0] - Trippy
1
请在您的问题中发布实际代码,以显示在您的图表中的点。 - BillBokeey
1
出色!仍然存在凸性假设问题,但正如Dennis所说,在你回答问题之前需要领域知识来重新指定问题。我认为这是OP可以期望的最好答案。 - Dan
1
这条评论让我开心了 @Dan - BillBokeey
2
漂亮。多么出色的努力!+1。 - rayryeng
显示剩余8条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接