Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

sort 2D points in counter- clockwise way

1,420 views
Skip to first unread message

Ahmad

unread,
Jul 27, 2008, 2:35:03 AM7/27/08
to
Is there a quick way to order points in a counter-clockwise
fashion. i.e. if we have these set of points in 2D space:
[4 1]
[1 4]
[4 4]
[1 1]

we need to organize them in a counter-clockwise fashion:
[1 1]
[1 4]
[4 4]
[4 1]

Oh....and the polygon is not necessarily regular

Bruno Luong

unread,
Jul 27, 2008, 3:31:02 AM7/27/08
to
"Ahmad " <ahmad...@gmail.com> wrote in message
<g6h4un$rs6$1...@fred.mathworks.com>...

Is it convex? If yes you might take the convexhull (qhull)
to order them and take the algebra area to see how they are
oriented.

Bruno

Bruno

Bruno Luong

unread,
Jul 27, 2008, 3:44:02 AM7/27/08
to

> Is it convex? If yes you might take the convexhull (qhull)
> to order them and take the algebra area to see how they are
> oriented.
>
> Bruno
>

Another idea that might or might not work depending on the
shape of your polygons is to remove the barycenter of all
vertexes and take the polar-angle (use function atan2, or
transform to complex and use function angle), and sort the
vertexes with respect to angle.

Bruno

Matt

unread,
Jul 27, 2008, 12:43:05 PM7/27/08
to
> Oh....and the polygon is not necessarily regular

If the polygon is not convex, the problem is very ill-
defined, which leads me to think you might want to add
specifics to the problem before this discussion continues.

For example, suppose you have 5 points consisting of the
vertices of a square, plus it's center.

[0 0]
[1 0]
[1 1]
[0 1]
[0.5 0.5]

Notice that I've ordered the square's vertices counter-
clockwise. However, I can now insert [0.5 0.5] anywhere in
the list and still get an counter-clockwise ordering.

Why? because I can build a non-convex polyhedron by
connecting any two adjacent corners of the square to the
center.

Z

unread,
Jun 26, 2009, 6:22:01 PM6/26/09
to
If you only want them to be counter-clock wise, why not convert the [x,y] point to polar coordinate and sort them by angle?

"Ahmad " <ahmad...@gmail.com> wrote in message <g6h4un$rs6$1...@fred.mathworks.com>...

Bogdan Dzyubak

unread,
Jan 26, 2012, 12:02:10 PM1/26/12
to
Thanks, Z. Simple and elegant.

MrWong

unread,
Mar 7, 2013, 2:51:07 AM3/7/13
to
"Bogdan Dzyubak" <ill...@gmail.com> wrote in message <jfs0ui$s62$1...@newscl01ah.mathworks.com>...
> Thanks, Z. Simple and elegant.

Hi,

great idea but didn't work for my data. Found a work-around using pdist2

dist = pdist2(data,data);
N = size(data,1);
result = NaN(1,N);
% first point is first row in data matrix
result(1) = 1;
for ii=2:N
dist(:,result(ii-1)) = Inf;
[~, closest_idx] = min(dist(result(ii-1),:));
result(ii) = closest_idx;
end
data=data(result,:);

"data" should be a nx2 vector containing x,y values.

Pepe Mandioca

unread,
Aug 2, 2015, 12:25:10 PM8/2/15
to
Wong, this is a great idea, but it fails on ocassion when you have a bifurcation in your contour, generally because of some noise in its estimation.

To solve this, you can add a max_distance parameter to the algorithm, so whenever the minimum distance from the current point to other is above that threshold, you stop the algorithm.

The modified code is:

function [poligon,indices]=poligon_from_set_of_points(points,max_distance)
%
% Note that a subset of the points may be selected if the distance between
% two points exceeds max_distance

dist = pdist2(points,points);

N = size(points,1);
indices = NaN(1,N);
indices(1) = 1; % first point is first row in x matrix

for i=2:N
dist(:,indices(i-1)) = Inf;
[distance, closest_idx] = min(dist(indices(i-1),:));
if (distance<max_distance)
indices(i) = closest_idx;
else
indices(i:end)=[];
break;
end
end

poligon=points(indices,:);

"MrWong" wrote in message <kh9gtb$lao$1...@newscl01ah.mathworks.com>...
0 new messages