Trace along specific direction of perimeter of plotted object
Show older comments
Consider the following gif:

As you can see, when I loop through the [x y] data, it doesn't pick a direction to travel around the object. How can I make my loop such that it travels a specific direction and returns to where it began? I thought about sorting the data, that didn't work though.
Thanks in advance!
Answers (2)
coldsnack
on 27 Apr 2018
2 votes
Ameer Hamza
on 27 Apr 2018
Edited: Ameer Hamza
on 27 Apr 2018
For gif, it appears that your data is something like this
x = [x1 x1 x2 x2 x3 x3 x4 x4 ....... xn xn]; % every element is repeated twice
y = [y1 y2 y3 y4 y5 y6 y7 y8 .......... yn]; $ y1 y3 y5 to lower line, y2 y4 y6 to upper line
you just need to rearrange your vectors
xNew = zeros(1, length(x));
yNew = zeros(1, length(y));
[~, index] = unique(x);
xNew(1:length(index)) = x(1:2:end);
x(1:2:end) = [];
xNew(length(index)+1:end) = x(end:-1:1);
yNew(1:length(index)) = y(1:2:end);
y(1:2:end) = [];
yNew(length(index)+1:end) = y(end:-1:1);
Try running this and plot using xNew and yNew.
6 Comments
coldsnack
on 27 Apr 2018
Ameer Hamza
on 27 Apr 2018
Oh! I was testing it before posting here and forgot to edit t, now check the edited code.
Ameer Hamza
on 27 Apr 2018
Can you show you x and y vector. It appears that they don't follow this pattern
x = [x1 x1 x2 x2 x3 x3 x4 x4 ....... xn xn]; % every element is repeated twice
y = [y1 y2 y3 y4 y5 y6 y7 y8 .......... yn]; $ y1 y3 y5 to lower line, y2 y4 y6 to upper line
coldsnack
on 27 Apr 2018
coldsnack
on 27 Apr 2018
Categories
Find more on Polygonal Shapes in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!