我试图在不实际使用Voronoi函数图的情况下重新创建Voronoi图。我有一个预定义的1x1正方形,用作我的“测试”区域。我的代码知道该图的线与周长相交的位置,但该线并不止于此。它一直持续到到达随机点为止。
x1 = 6
y1 = x1
x = gallery('uniformdata',[1,x1],0)
y = gallery('uniformdata',[1,y1],1)
sizeofx = size(x,2)
sizeofy = size(y,2)
reshapedx = reshape(x,sizeofx,1)
reshapedy = reshape(y,sizeofy,1)
[vxx,vyy] = voronoi(x,y)
hold on
[v,c] = voronoin([x(:) y(:)]) %intersection point matrix
for i=1:numel(c)
v(c{i},:)
ans = ans( ~any( isnan( ans ) | isinf( ans ), 2 ),: )%deletes infs or nans from polygon points
plot(ans(:,1),ans(:,2),'b-','linewidth',2) %%this code plots the points
end
%for our test purposes , the voronoi diagram will only be in a 1x1 square.
v(v<0) = inf
v(v>1) = inf
v = v( ~any( isnan( v ) | isinf( v ), 2 ),: ) %deletes the inf or nan
DT = delaunayTriangulation(reshapedx,reshapedy)
[V,R] = voronoiDiagram(DT)
sizeofR = size(R,1)
rectangle('Position',[0,0,1,1])
axis equal
xlimit = [0 1];
ylimit = [0 1];
xbox = xlimit([1 1 2 2 1]);
ybox = ylimit([1 2 2 1 1]);
%finds intersection from diagram to square
for j=1:length(vxx(1,:))
line([vxx(1,j) vxx(2,j)],[vyy(1,j) vyy(2,j)]);
[xi, yi, ii] = ...
polyxpoly([vxx(1,j) vxx(2,j)], [vyy(1,j) vyy(2,j)], xbox, ybox);
if ~isempty(xi)
intersectx(j) = xi
intersecty(j) = yi
plot(xi,yi,'r*');
axis equal
end
end
我希望线条一旦达到边界点就停止绘图。
答案 0 :(得分:1)
您可以用交点替换直线边缘的坐标。
假设行从(x1,y1)到(x2,y2)。
检查(x1,y1)是否在矩形的边界之外。
如果(x1<0) || (y1<0) || (x1>1) || (y1>1)
,则您的情况(x1,y1)在边界之外。
如果(x1,y1)在外面,则用交点(xi,yi)替换(x1,y1)。
注意:只有在有交点的情况下才可以更换。
我忽略了两个交点的情况,因为这种情况在您的情况下永远不会发生(在这种情况下,您需要同时替换两个)。
这是经过修改的完整代码:
x1 = 6;
y1 = x1;
x = gallery('uniformdata',[1,x1],0);
y = gallery('uniformdata',[1,y1],1);
sizeofx = size(x,2);
sizeofy = size(y,2);
reshapedx = reshape(x,sizeofx,1);
reshapedy = reshape(y,sizeofy,1);
[vxx,vyy] = voronoi(x,y);
hold on
[v,c] = voronoin([x(:) y(:)]); %intersection point matrix
for i=1:numel(c)
v(c{i},:);
ans = ans( ~any( isnan( ans ) | isinf( ans ), 2 ),: );%deletes infs or nans from polygon points
plot(ans(:,1),ans(:,2),'b-','linewidth',2); %%this code plots the points
end
%for our test purposes , the voronoi diagram will only be in a 1x1 square.
v(v<0) = inf;
v(v>1) = inf;
v = v( ~any( isnan( v ) | isinf( v ), 2 ),: ); %deletes the inf or nan
DT = delaunayTriangulation(reshapedx,reshapedy);
[V,R] = voronoiDiagram(DT);
sizeofR = size(R,1);
rectangle('Position',[0,0,1,1]);
axis equal
xlimit = [0 1];
ylimit = [0 1];
xbox = xlimit([1 1 2 2 1]);
ybox = ylimit([1 2 2 1 1]);
%Vxx and Vyy are going to be a new set of line coordinates, replacing vxx and vyy.
Vxx = vxx;
Vyy = vyy;
%finds intersection from diagram to square
for j=1:length(vxx(1,:))
%line([vxx(1,j) vxx(2,j)],[vyy(1,j) vyy(2,j)]);
[xi, yi, ii] = ...
polyxpoly([vxx(1,j) vxx(2,j)], [vyy(1,j) vyy(2,j)], xbox, ybox);
if ~isempty(xi)
intersectx(j) = xi;
intersecty(j) = yi;
plot(xi,yi,'r*', 'MarkerSize', 10);
axis equal
%Replace line edges outsize the rectangle with the inersection point.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
x1 = vxx(1,j);
x2 = vxx(2,j);
y1 = vyy(1,j);
y2 = vyy(2,j);
is_outsize1 = (x1 < 0) || (y1 < 0) || (x1 > 1) || (y1 > 1);
is_outsize2 = (x2 < 0) || (y2 < 0) || (x2 > 1) || (y2 > 1);
%Assume rectangle's boundaries are (0,0) to (1,1)
if is_outsize1
%Replace the coordinate [vxx(1,j), vyy(1,j)] with [xi, yi].
Vxx(1,j) = xi;
Vyy(1,j) = yi;
end
if is_outsize2
%Replace the coordinate [vxx(2,j), vyy(2,j)] with [xi, yi].
Vxx(2,j) = xi;
Vyy(2,j) = yi;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
end
%Plot the line with the replaces coordinates
line([Vxx(1,j) Vxx(2,j)],[Vyy(1,j) Vyy(2,j)], 'color', 'g', 'LineWidth', 1, 'Marker', '+');
end