MATLAB: Sorting of points in 2D

aerofoilairfoilalphashapeboundarycentroidconvmmpolyfitmovmeanpolyfitpolyvalsorting in 2d

Dear All,
I face issue in arranging the airfoil coordinates coming from a CAD program. The coordinates are arranged from minumum x value to maximum x value. But I require to arrange the points starting from Trailing Edge ( Max X coordinate) and then proceed on top side all the way to Leading Edge ( Minimum X coordinate) and then again proceed in the bottom side and terminate in the Trailing Edge.
The input.txt ( required input file to be sorted)
Output.txt (the coordinates sorted manually).
I have several airfoil coordinates of similar nature, so a matlab function could help me out doing the job manually everytime.
Any help is really appreciated.
Thanks in Advance,
K Vijay Anand

Best Answer

Here is some simple code that that works quite nicely for your aerofoil. The code assumes that the top and bottom are both functions of x (i.e. no implicit curves in some other variable) and that they are both sampled with approximately the same step size (like your data). A convolution is used to smooth the data to create a moving average over 3 data points. The y-data are compared against this moving average to decide if the data point belongs to the top or bottom of the aerofoil, and then those two sets of points are sorted separately.
Inp = readtable('Input.txt','Delimiter','\t');
tmp = conv(Inp.y([1,1:end,end]),ones(1,3)/3,'valid'); % smoothed data
idx = Inp.y >= tmp; % true/false == top/bottom
idx(end) = true; % trailing edge -> top
Out = [sortrows(Inp(idx,:),-1);sortrows(Inp(~idx,:),+1)];
writetable(Out,'Out.txt','Delimiter','\t')
Checking against your original "Output.txt" and plotting the data:
>> Chk = readtable('Output.txt','Delimiter','\t');
>> isequal(Chk,Out)
ans =
1
>> plot(Inp.x,Inp.y,'-*', Inp.x,tmp,'-+', Out.x,Out.y,'-x')
>> legend('original','moving average','sorted')