gpt4 book ai didi

arrays - 在没有相交线的情况下连接MATLAB中的随机点

转载 作者:太空宇宙 更新时间:2023-11-03 19:38:36 26 4
gpt4 key购买 nike

我需要帮助解决这个问题。我有随机生成的点(图片 #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];

图片 #1: enter image description here

结果应该是这样的:图片#2: enter image description here

知道如何解决这个问题吗?

最佳答案

我想对于一般情况来说,想出一个解决方案是非常困难的。但是,假设您的观点“很好地”分散,则有一个非常简单的解决方案。

如果您根据连接点和点云中心的向量的 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

仅此而已。绘制结果:

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]);

如果几个点相对于点云的中心具有相同的角度,则此算法将失败。

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

P = rand(2,50);

enter image description here

关于arrays - 在没有相交线的情况下连接MATLAB中的随机点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29210958/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com