在MATLAB中连接随机点而不相交线

Dom*_*čík 5 arrays matlab geometry computational-geometry

我需要帮助来解决这个问题.我有随机生成的点(图片#1上的例子),我想用线连接它们(图片#2上的例子).线不能相交,连接后,连接点看起来应该是不规则区域.

%Generating random points
xn = randi([3 7],1,10);
yn = randi([3 6],1,10);

%Generated points
xn = [6,3,7,7,6,6,6,4,6,3];
yn = [5,3,4,3,3,6,5,4,6,3];
Run Code Online (Sandbox Code Playgroud)

图片1: 在此输入图像描述

结果应该是这样的:图片#2: 在此输入图像描述

不知道怎么解决这个问题?

Sha*_*hai 8

我想,对于一般情况,提出解决方案可能非常困难.但是,假设你的分数很"分散",那么就有一个简单的解决方案.

如果根据连接点和点云中心的矢量x轴上方的角度对点进行排序,则:

P = [xn;yn]; %// group the points as columns in a matrix
c = mean(P,2); %// center point relative to which you compute the angles
d = bsxfun(@minus, P, c ); %// vectors connecting the central point and the dots
th = atan2(d(2,:),d(1,:)); %// angle above x axis
[st si] = sort(th); 
sP = P(:,si); %// sorting the points
Run Code Online (Sandbox Code Playgroud)

这就是它.要绘制结果:

sP = [sP sP(:,1)]; %// add the first point again to close the polygon
figure;plot( sP(1,:), sP(2,:), 'x-');axis([0 10 0 10]);
Run Code Online (Sandbox Code Playgroud)

如果几个点与点云的中心具有相同的角度,则该算法将失败.

一个有20个随机点的例子:

P = rand(2,50);
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述