我在评论中提到可以使用convhull
和inpolygon
来解决这个问题,但是inpolygon
似乎不适用于3D多边形。我们将使用delaunayTriangulation
和pointLocation
来得到结果。
[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');
如果您希望您的代码可以自动适应2D或3D数组,则只需要修改绘图调用。只需编写一个if
语句来检查A和B中的列数即可。
convhull
函数,然后使用inpolygon
函数来检查交集中的点。 - BillBokeey