Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
converting vertex orders counterclockwise

Subject: converting vertex orders counterclockwise

From: David Edson

Date: 22 May, 2012 00:24:06

Message: 1 of 8

I have older version of matlab. How can i change my coordinate orders counterclocksie?

is there a free m code.

thanks

Subject: converting vertex orders counterclockwise

From: Roger Stafford

Date: 22 May, 2012 00:53:05

Message: 2 of 8

"David Edson" <tojadeb@example.com> wrote in message <jpemb6$qif$1@newscl01ah.mathworks.com>...
> ...... How can i change my coordinate orders counterclocksie?
- - - - - - - - - -
  What do you mean by "converting vertex orders counterclockwise"? Please give a thorough-going description of your question, preferably accompanied by a simple but illustrative concrete example.

  (My crystal ball has failed to decipher what it is you are asking. A number of possibilities have occurred to me but that is the trouble - there are too many such possibilities.)

Roger Stafford

Subject: converting vertex orders counterclockwise

From: David Edson

Date: 22 May, 2012 01:01:06

Message: 3 of 8


I have a closed curve in 2D. Starting from a point I can go clockwise or anticlockwise. I want a code that always garantees that I go anticlockwise. Inother words, given a its coordinates, I want to arrange the coordinates so that they are arranged in anticlockwise manner to define the closed curve.

It is like what poly2ccw -does in the latest version of matlab. I have older version.

Convert polygon contour to counterclockwise vertex ordering
Syntax
[x2, y2] = poly2ccw(x1, y1)







"Roger Stafford" wrote in message <jpeo1h$3qq$1@newscl01ah.mathworks.com>...
> "David Edson" <tojadeb@example.com> wrote in message <jpemb6$qif$1@newscl01ah.mathworks.com>...
> > ...... How can i change my coordinate orders counterclocksie?
> - - - - - - - - - -
> What do you mean by "converting vertex orders counterclockwise"? Please give a thorough-going description of your question, preferably accompanied by a simple but illustrative concrete example.
>
> (My crystal ball has failed to decipher what it is you are asking. A number of possibilities have occurred to me but that is the trouble - there are too many such possibilities.)
>
> Roger Stafford

Subject: converting vertex orders counterclockwise

From: Nasser M. Abbasi

Date: 22 May, 2012 01:31:35

Message: 4 of 8

On 5/21/2012 7:53 PM, Roger Stafford wrote:
>

> (My crystal ball has failed to decipher what it is you are asking.
>
> Roger Stafford

wow Roger, this is rare !

Many times, I read a question, and I would have no idea
what it is saying, but I say to myself, will wait for
Roger to show up, he will be able to do the translation :)

--Nasser

Subject: converting vertex orders counterclockwise

From: Roger Stafford

Date: 22 May, 2012 01:42:07

Message: 5 of 8

"David Edson" <tojadeb@example.com> wrote in message <jpeogi$5tc$1@newscl01ah.mathworks.com>...
> I have a closed curve in 2D. Starting from a point I can go clockwise or anticlockwise. I want a code that always garantees that I go anticlockwise. Inother words, given a its coordinates, I want to arrange the coordinates so that they are arranged in anticlockwise manner to define the closed curve.
- - - - - - - - - - -
  Here is one way. Let x and y be vectors containing the cartesian coordinates of your closed curve vertices.

 p = [2:length(x),1]; % A "circshift" action
 a = (1/2)*sum((x(p)+x).*(y(p)-y));

This is the signed area within the closed curve. If it is positive, the curve advances counterclockwise with increasing index. If it is negative, the advancing index is moving clockwise (in which case you should reverse the order of the vertices.)

  (Note that it is assumed that the closed curve never crosses itself before the end meets the beginning. Otherwise the "signed area" becomes the difference between two areas, the counterclockwise area and the clockwise area.)

Roger Stafford

Subject: converting vertex orders counterclockwise

From: Bruno Luong

Date: 22 May, 2012 05:52:07

Message: 6 of 8

"David Edson" <tojadeb@example.com> wrote in message <jpemb6$qif$1@newscl01ah.mathworks.com>...
> I have older version of matlab. How can i change my coordinate orders counterclocksie?
>
> is there a free m code.
>

x=randn(1,30);
y=randn(1,30);

xmean=mean(x);
ymean=mean(y);
[~, i] = sort(atan2(y-ymean,x-xmean));
x = x(i);
y = y(i);

plot(x([1:end 1]),y([1:end 1]))
axis equal
 
% Bruno

Subject: converting vertex orders counterclockwise

From: Roger Stafford

Date: 22 May, 2012 12:14:07

Message: 7 of 8

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <jpf9i7$b6m$1@newscl01ah.mathworks.com>...
> x=randn(1,30);
> y=randn(1,30);
>
> xmean=mean(x);
> ymean=mean(y);
> [~, i] = sort(atan2(y-ymean,x-xmean));
> x = x(i);
> y = y(i);
>
> plot(x([1:end 1]),y([1:end 1]))
> axis equal
>
> % Bruno
- - - - - - - - - -
  Bruno, if you have

 x = [0 1 1 -1 -1 0];
 y = [1 -3 2 2 -3 1];

do you consider the vertices along this polygon to be in counterclockwise order? I would have thought they were, but they are not in sorted order according to your 'atan2' test. Two of the edges move the angle with respect to the mean point (at the origin) in a descending direction.

  In his second article in this thread David makes it clear he is talking about a given polygon, rather than simply a collection of points, where he says, "I have a closed curve in 2D. Starting from a point I can go clockwise or anticlockwise."

Roger Stafford

Subject: converting vertex orders counterclockwise

From: Bruno Luong

Date: 22 May, 2012 12:28:07

Message: 8 of 8

"Roger Stafford" wrote in message <jpfvuf$7n3$1@newscl01ah.mathworks.com>...

>
> In his second article in this thread David makes it clear he is talking about a given polygon, rather than simply a collection of points, where he says, "I have a closed curve in 2D. Starting from a point I can go clockwise or anticlockwise."

I clearly overlooked his second post.

Bruno

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us