多边形相交以在Matlab中形成新的多边形

时间:2019-01-01 14:45:07

标签: matlab polygon intersection

我想制作一个新的由多边形的交点形成的多边形或多边形,该多边形由黑色的缓冲区命令生成。我还附上了图片。

编码或使用任何其他软件的最简单方法是什么?如何找到多边形与所有其他相邻多边形的交点的顶点,然后将这些顶点合并成一个新的顶点?逐一存储一个多边形与其他多边形的交点的顶点?如果还有其他简单的方法,请建议是否有人可以为此问题添加一些代码。

N = 80; % aproximative number of polygonals to be generated
n = 2000; % control size and number of vertexes of polygonal
nrepulsion = 3; % control the size of the polygonal and the randomness of the position
X = randn(N,2);
R = sqrt(rand(N,1));
X = R .* X ./ sqrt(sum(X.^2,2));
X(:,2) = abs(X(:,2));
nb = max(N,100);
theta = linspace(0,pi,nb)';
XC = [cos(theta), sin(theta)];
XY0 = linspace(-1,1,ceil(nb*2/pi))' .* [1 0];
XY0([1 end],:) = [];
n1 = size(X,1);
n2 = size(XC,1);
n3 = size(XY0,1);
CC = n1+(1:n2-1)' + [0 1];
C0 = (n1+n2)+(1:n3-1)' + [0 1];
C = [CC; C0];
% Repulsion of seeds to avoid them to be too close to each other
for k = 1:nrepulsion-1
    XALL = [X; XC; XY0];
    DT = delaunayTriangulation(XALL, C);
    T = DT.ConnectivityList;
    containX = ismember(T,1:n1);
    b = any(containX,2);
    TX = T(b,:);
    containX = containX(b,:);
    [r,i0] = find(containX);
    i = mod(i0+(-1:1),3)+1;
    m = size(TX,1);
    i = TX(r + (i-1)*m);
    T = accumarray([i(:,1);i(:,1)],[i(:,2);i(:,3)],[n1 1],@(x) {unique(x)});
    maxd2 = 0;
    R = zeros(n1,2);
    for i=1:n1
        Ti = T{i};
        P = XALL(Ti,:);
        P = X(i,:) - P;
        nP2 = sum(P.^2,2);
        maxd2 = max(maxd2,max(nP2));
        b = Ti > n1;
        nP2(b) = nP2(b)*5; % less repulsion from each point of the border
        R(i,:) = sum(P./nP2,1);
    end
    if k==1
        v0 = 0.05/sqrt(maxd2);
    end
    v = v0/sqrt(max(sum(R.^2,2)));
    X = X + v*R;
    % Project back if points falling outside the half-circle
    r2 = sum(X.^2,2);
    out = r2>1;
    X(out,:) = X(out,:) .* (0.95 ./ sqrt(r2(out))); % 95percent back from previous point 
    X(:,2) = max(X(:,2),0.01);
end
XALL = [X; XC; XY0];
DT = delaunayTriangulation(XALL, C);
[V,P] = voronoiDiagram(DT);
yV = V(:,2);
xV = V(:,1);
inside = (yV > 0) & (xV.^2+yV.^2) < 1;
inside = cellfun(@(id) all(inside(id)), P);
P = P(inside);
for k=1:length(P)
    Pk = P{k};
    m = length(Pk);
    W = rand(n,m-1) .^ (1./(m-1:-1:1));
    W = cumprod([ones(n,1),W],2) .* (1-[W, zeros(n,1)]);
   Pk = W*V(Pk,:);
    K = convhull(Pk);
    P{k} = Pk(K,:);
end
% Check
close all
hold on
for k=1:length(P)
    Pk = P{k};
    plot(Pk([1:end 1],1),Pk([1:end 1],2),'B');
   polyin=polyshape(P{k});
   polyout3 = polybuffer(polyin,0.01,'JointType','square');
   plot(polyout3,'FaceColor', 'none');



end


XB = [XC; XY0];
plot(XB([1:end 1],1),XB([1:end 1],2),'B');
axis equal;
axis([-1.1 1.1 -0.1 1.1]);

Polygons

0 个答案:

没有答案