MATLAB Answers

polyshapes formed by intersection of polygons/polyshapes

12 ビュー (過去 30 日間)
jahanzaib ahmad
jahanzaib ahmad 2018 年 12 月 31 日
編集済み: jahanzaib ahmad 2019 年 1 月 1 日
i want to make a new polyshape formed by intersection of polygons/polyshapes .
i have attached the code uploaded by MR bruno .
1.PNG

  2 件のコメント

jahanzaib ahmad
jahanzaib ahmad 2018 年 12 月 31 日
11.PNG
jahanzaib ahmad
jahanzaib ahmad 2018 年 12 月 31 日
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),'r');
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),'k');
axis equal;
axis([-1.1 1.1 -0.1 1.1]);

サインインしてコメントする。

回答 (1 件)

Steven Lord
Steven Lord 2018 年 12 月 31 日
Try calling intersect on your polyshape objects as I suggested in my answer for one of your previous questions.

  1 件のコメント

jahanzaib ahmad
jahanzaib ahmad 2019 年 1 月 1 日
"intersection "will give me the point of intersections . i m after the logic to connect those points so i get the polyshape .thats the problem

サインインしてコメントする。

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by