Вы можете заменить координату края линии точкой пересечения.
Предположим, линия проходит от (x1, y1) до (x2, y2).
Проверьте, находится ли (x1, y1) за пределами прямоугольника.
В вашем случае (x1, y1)) находится за пределами границ, если (x1<0) || (y1<0) || (x1>1) || (y1>1)
.
В случае, если (x1, y1) находится за пределами, замените (x1, y1) точкой пересечения (xi, yi).
Примечание. Заменяйте его, только если имеетсяточка пересечения.
Я проигнорировал случай двух точек пересечения, потому что этот случай никогда не происходит в вашем случае (в этом случае вам нужно заменить оба).
Вот полный код с изменениями:
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
Результат:
Исходный сюжет: