Order Points for plotting

5 visualizzazioni (ultimi 30 giorni)
Ahmed Abdulla
Ahmed Abdulla il 24 Mag 2022
Commentato: Ahmed Abdulla il 24 Mag 2022
I have the coordinates of the above two shapes imported from a CAD software, however when I plot the shape the points are not in order. Is there a method to order the coordinates so that points on the perimeter of the shape that are side by side are next to each other in the matrix.
If there is any mathematical method that would help me, I am willing to understand it and code it myself. Thank you
  5 Commenti
Walter Roberson
Walter Roberson il 24 Mag 2022
use a different shrink factor?
Ahmed Abdulla
Ahmed Abdulla il 24 Mag 2022
@Walter Roberson Here I am using a shrink factor of 1 which is the highest I can go :(

Accedi per commentare.

Risposte (1)

KSSV
KSSV il 24 Mag 2022
load('A.mat') ;
x = A(:,1) ; y = A(:,2) ;
n = length(x) ;
iwant = zeros(n,2) ;
i = 1 ;
iwant(i,:) = [x(i) y(i)] ;
x(i) = [] ; y(i) = [] ;
while ~isempty(x)
i = i+1;
idx = knnsearch([x y],iwant(i-1,:));
iwant(i,:) = [x(idx) y(idx)] ;
x(idx) = [] ; y(idx)= [];
end
iwant(end+1,:) = iwant(1,:) ;
plot(iwant(:,1),iwant(:,2))
  3 Commenti
KSSV
KSSV il 24 Mag 2022
Modificato: KSSV il 24 Mag 2022
Lets randomize the points and run the code.
load('A.mat') ;
x = A(:,1) ; y = A(:,2) ;
n = length(x) ;
idx = randperm(n,n) ;
x = x(idx) ; y = y(idx) ;
iwant = zeros(n,2) ;
i = 1 ;
iwant(i,:) = [x(i) y(i)] ;
x(i) = [] ; y(i) = [] ;
while ~isempty(x)
i = i+1;
idx = knnsearch([x y],iwant(i-1,:));
iwant(i,:) = [x(idx) y(idx)] ;
x(idx) = [] ; y(idx)= [];
end
iwant(end+1,:) = iwant(1,:) ;
plot(iwant(:,1),iwant(:,2))
Ahmed Abdulla
Ahmed Abdulla il 24 Mag 2022
Thank you @KSSV for your efforts I really appreciate it. Unfortunately this doesnt work every time and I am trying to make a generalised function that will work for any shape and at all times

Accedi per commentare.

Categorie

Scopri di più su 2-D and 3-D Plots in Help Center e File Exchange

Prodotti


Release

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by