Trace along specific direction of perimeter of plotted object

2 views (last 30 days)
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
coldsnack on 27 Apr 2018
Alright I figured it out. For any future people who has this problem use bwtraceboundary. It will return a matrix with the correctly sorted data to loop around the object.

Ameer Hamza
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
coldsnack on 27 Apr 2018
Thank you for the patience. I have attached the two coordinate vectors.

Sign in to comment.

Categories

Find more on MATLAB 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!