draw closed contour in 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 .

댓글 수: 1

Are there any limitations to how the coordinates can be re-arranged? Because otherwise it seems that the solution is not unique. For example, any of the following permutations of (xiiii,yiiii) would satisfy the closed, non-self-intersecting requirement:
p = [40,200,210,230,50,20,40;
50,60,160,80,120,30,50]'; %your xiiii,yiiii pairs
figure();
subplot(2,2,1), plot(p(:,1), p(:,2), 'bo-');
tstr = ['Original order: (', sprintf('%d ', 1:7), ')'];
title(tstr);
%solution 1
order1 = [1 6 5 3 4 2 7];
subplot(2,2,2); plot(p(order1,1), p(order1,2), 'bo-');
tstr = ['Order 1: (', sprintf('%d ', order1), ')'];
title(tstr);
%solution 2
order2 = [ 1 4 3 5 6 2 7];
subplot(2,2,3); plot(p(order2,1), p(order2,2), 'bo-');
tstr = ['Order 2: (', sprintf('%d ', order2), ')'];
title(tstr);
%solution 3
order3 = [1 5 3 4 2 6 7];
subplot(2,2,4); plot(p(order3,1), p(order3,2), 'bo-');
tstr = ['Order 3: (', sprintf('%d ', order3), ')'];
title(tstr);
How are you determining the "correct" solution? Maximum area? Minimum perimeter? etc.

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

도움말 센터File Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

질문:

2013년 7월 8일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by