0

我有个问题。我有一组 x 和 y 坐标,我可以通过它绘制轮廓或闭合图形。然而,在我在程序中的操作过程中,坐标的顺序可能会改变。因此,现在如果绘制了绘图,则曲线未正确绘制。

下面在我的代码中给出了一个说明:

clc;
clear all 
close all 
xi = [86.7342,186.4808,237.0912,194.8340,84.2774,39.5633,86.7342];
yi = [18.2518,18.2518,102.3394,176.4611,172.1010,88.6363,18.2518];
subplot(1,2,1),plot(xi,yi);
title('original points contour');
xii=xi; yii=yi;
%Suppose the points are interchanged
t=0;
t=xii(3); xii(3)=xii(4); xii(4)=t;
t=yii(3); yii(3)=yii(4); yii(4)=t;
subplot(1,2,2),plot(xii,yii);
title('Redrawn contour with the points exchanged');
%I get this contour. 

这两个图显示在代码中。

无论元素的顺序如何,我都需要能够重新绘制正确的轮廓。我应该使用排序算法吗?如何确定点的顺序以形成没有任何交点的良好闭合轮廓?提前致谢。

注意::假设在操作过程中我的坐标集变成了这样:

 xiiii =[40,200,210,230,50,20,40] 
 yiiii =[50,60,160,80,120,30,50]
 figure();
 plot(xiiii,yiiii,'+r'); hold on;
 % I need to somehow change the matrices in such a way so as to form
 %an non-overlapping closed surface. 
 %after manipulation I get should get this matrices
 xiii =[40,200,230,210,50,20,40];
 yiii =[50,60,80,160,120,30,50];
 plot(xiii,yiii,'+b');
 hold off; 
 %Notice the difference between the two plots. I require the 2nd plot. 

我希望这个例子能说明我的问题。再次感谢大家。

4

1 回答 1

1

The solution is easy. You need to save the index vector in the first stage. Then for each element that you exchange you need to exchange the proper index as well. This works if you are exchanging values (changing the order or place of an element).

%before change
p = [p1 p2 p3 p4 p5];

i = [1 2 3 4 5];

% after change
pp = [p1 p2 p4 p3 p5]; 

ii = [1 2 4 3 5];

if you still need more explanation I can modify the code you posted.

if you just have a bunch of points and you want to find the close loop across those point. then I have to mention that your problem is a famous optimization problem called "Travel Salesman Problem" (TSP).

Given a list of cities and the distances between each pair of cities, what is the shortest possible route that visits each city exactly once and returns to the origin city? It is an NP-hard problem in combinatorial optimization, important in operations research and theoretical computer science. The problem was first formulated in 1930 and is one of the most intensively studied problems in optimization. It is used as a benchmark for many optimization methods. Even though the problem is computationally difficult, a large number of heuristics and exact methods are known, so that some instances with tens of thousands of cities can be solved.

There are many methods that can solve this problem. check this link for more information: link

于 2013-07-08T19:35:31.467 回答