如何在MATLAB中创建这些方块的voronoi图,因为voronoi不会进入黄色方块?或者,只是它的一些行进入黄色。
答案 0 :(得分:0)
可以放constrains on Delaunay triangulation,然后添加更多检查。也许它可以帮助你:
close all
% generate random rectangels [x,y,w,h] format
n = 8;
w = 0.15;
h = 0.05;
rects = [rand(n,2),w*ones(n,1),h*ones(n,1)];
% convert to [xmin ymin xmax ymax] format
boxes = [rects(:,1:2), rects(:,1:2) + rects(:,3:4)];
% convert to one single polygon (with missing vertexes)
X = boxes(:,[1 3 3 1 1]);
Y = boxes(:,[2 2 4 4 2]);
X(:,end+1) = nan;
Y(:,end+1) = nan;
X = X';X = X(:);
Y = Y';Y = Y(:);
% polygon vertxes without the Nans
XX = X;XX(6:6:end) = [];
YY = Y;YY(6:6:end) = [];
% intersections between rectangles
[xi,yi] = polyxpoly(X,Y,X,Y,'unique');
% remove intersections found inside rectangles
IN = any(bsxfun(@gt,xi',boxes(:,1)) & bsxfun(@lt,xi',boxes(:,3)) & ...
bsxfun(@gt,yi',boxes(:,2)) & bsxfun(@lt,yi',boxes(:,4)),1);
xi(IN) = [];
yi(IN) = [];
% find vertex pairs of the rectangles
xeq = bsxfun(@eq,xi,xi');
yeq = bsxfun(@eq,yi,yi');
xyeq = triu(xeq | yeq,1);
[idx1,idx2] = find(xyeq);
% generate constrains for the triangulation
C = [idx1,idx2];
% triangulate
DT = delaunayTriangulation([xi,yi],C);
% get connections (triangles) list
TRI = DT.ConnectivityList;
remIdx = false(size(TRI,1),1);
% check condition to remove triangles
for ii = 1:size(TRI,1)
% current triangle coordinates
xx = xi(TRI(ii,[1:3 1]));
yy = yi(TRI(ii,[1:3 1]));
% find if triangle inside rectangle
BETx = bsxfun(@ge,xx(1:3)',boxes(:,1)) & bsxfun(@le,xx(1:3)',boxes(:,3));
BETy = bsxfun(@ge,yy(1:3)',boxes(:,2)) & bsxfun(@le,yy(1:3)',boxes(:,4));
IN = BETx & BETy;
if any(all(IN,2))
remIdx(ii) = true;
continue;
end
% find if triangle crosses rectangle
[xxi,yyi] = polyxpoly(xx,yy,X,Y,'unique');
notAllowedVertex = ~ismember([xxi yyi],[xi,yi],'rows');
if any(notAllowedVertex)
remIdx(ii) = true;
continue;
end
end
% remove unwanted triangles
TRI(remIdx,:) = [];
% plot
figure;
hold on
plot(X,Y,'g','LineWidth',2)
triplot(TRI,xi,yi)
plot(xi,yi,'or')
axis equal