Reputation:
I have a couple of data x0
and y0
that corresponds to curve. It contains 8 points, for example : K(x0(2),y0(2))
.
The problem is that when I plot x0
and y0
like this :
plot(x0,y0,'c*');
plot(x0,y0,'k');
I get the cyan point at a good place, but the curve is a mess. Check the screenshot:
Can I do some sort of Sorting
?
This question seems simple but I am a newbie.
Upvotes: 1
Views: 124
Reputation: 104545
If you have the Mapping Toolbox, the function poly2ccw
also performs the job for you. Convex Hull is a more general purpose algorithm to handle any convex polygon. If you already have the extremities of your shape, you can simply use poly2ccw
return coordinates reordered that will allow you to draw the perimeter of the shape in counter-clockwise order.
Specifically, you would just do this:
[x1, y1] = poly2ccw(x0, y0);
x1
and y1
would contain the shuffled points of x0
and y0
so that they are now in counter-clockwise order. You can then draw them via:
plot(x1, y1, 'k');
Upvotes: 0
Reputation: 13953
Since the points define a convex polygon, you can simply use the convhull
-function for this. convhull
returns the indexes of the outermost points in x0
and y0
. For this question we exploit that they are arranged counter-clockwise around the hull, so you can plot them nicely after.
h = convhull(x0,y0);
plot(x0(h),y0(h),'k');
The result looks like this:
Points used to generate the output:
x0 = [1 2 3 4 4 3 2 1];
y0 = [2 3 0 2 1 3 0 1];
Upvotes: 3