roni
roni

Reputation: 1454

draw contour sort matlab

I have a problem. I have a a set of x and y coordinates by which I can draw a contour or a closed figure. However during my operation within the program the order of coordinates may change. So now if the plot is drawn the curve is not drawn right.

An illustration is given below in my code:

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. 

The two plots are shown in the code.

I need to be able to redraw the correct contour no matter what the order of the elements are . Should I use a sort algorithm. How can I determine the order of the points so as to make a good closed contour without any intersections ? Thanks in advance.

Note:: Suppose during operation my set of coordinates becomes this :

 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. 

I hope this example makes my question clear. Thanks again all .

Upvotes: 0

Views: 383

Answers (1)

NKN
NKN

Reputation: 6434

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

Upvotes: 1

Related Questions