Interpolating a trajectory from an unsorted array of two-dimensional points, where questions of order

I need a way to get an Lx2 trajectory from an array of Nx2 points, that is, a way to connect these points into one trajectory (for example, create an array of points 10,000x2 in size from an array of 5x2 points). I tried using interp1and interp2, but either I do not fully understand them, or they do not do what I need.

+1
source share
1 answer

It looks like you need to use interp1in a loop (i.e. keep the original order), interpolating between each consecutive pair of points:

X = [10; 10.0001; 9; 48];   %// You can consider something like X = [10;10;9;48]; X=X+rand(size(X))*0.0001 instead of dealing with equal X values manually
Y = [10; 20; 50; 6];

m = 3333; %//num points between nodes
n = m*(length(X)-1);

Yi = zeros(n,1);
Xi = [];
for k = 1:length(X)-1
    xi = linspace(X(k), X(k+1), m);
    Xi = [Xi, xi];
    Yi(((k-1)*m+1):k*m) = interp1(X(k:k+1), Y(k:k+1),xi); 
end

plot(X,Y,'or');
hold on
plot(Xi,Yi);

To get the pentagon (not W), try this loop code with these inputs:

X = [0.25; 0.75; 1; 0.5; 0; 0.25];
Y = [0; 0; 1; 1.8; 1; 0];

:

enter image description here

+1

Source: https://habr.com/ru/post/1531784/


All Articles