一旦线接触到预定义正方形的周长,如何停止绘制? [英] How to stop plotting once line touches perimeter of predefined square?

查看:113
本文介绍了一旦线接触到预定义正方形的周长,如何停止绘制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在不实际使用Voronoi函数图的情况下重新创建Voronoi图.我有一个预定义的1x1正方形,用作我的测试"区域.我的代码知道该图的线与周长相交的位置,但是该线并没有在此终止.它一直持续到到达随机点为止.

I am trying to recreate a Voronoi diagram without actually using the Voronoi function plot. I have a predefined 1x1 square that acts as my "test" area. My code knows where the lines of the diagram intersect the perimeter, but the line doesn't stop there. It keeps going until it reaches a random point.

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

我希望线条一旦达到边界点就停止绘图.

I want the line to stop plotting once it reaches a perimeter point.

推荐答案

您可以将线的边缘坐标替换为相交点.

You can replace the coordinate of line's edge with the intersection point.

假设行从(x1,y1)到(x2,y2).
检查(x1,y1)是否在矩形的边界之外.
如果(x1<0) || (y1<0) || (x1>1) || (y1>1),则您的情况(x1,y1)在边界之外.
如果(x1,y1)在外面,则用交点(xi,yi)替换(x1,y1).
注意:只有在有交点的情况下才可以更换.

Assume line goes from (x1,y1) to (x2,y2).
Check if (x1,y1) is outside the boundaries of the rectangle.
I your case (x1,y1) is outside the boundaries if (x1<0) || (y1<0) || (x1>1) || (y1>1).
In case (x1,y1) is outside, replace (x1,y1) with intersection point (xi,yi).
Note: Replace it only if there is an intersection point.

我忽略了两个交点的情况,因为这种情况在您的情况下永远不会发生(在这种情况下,您需要同时替换两者).

I ignored the case of two intersection points, because this case never happens in your case (in that case, you need to replace both).

这是经过修改的完整代码:

Here is the complete code with the modifications:

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

结果:

原始图:

这篇关于一旦线接触到预定义正方形的周长,如何停止绘制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆