# [R] ordering points as vertex of a polygon

(Ted Harding) Ted.Harding at nessie.mcc.ac.uk
Mon Jun 14 12:31:55 CEST 2004

```On 14-Jun-04 Luca Scrucca wrote:
> Dear R-users,
>
> Suppose I have the following x-y coordinates which give the
> boundaries of a polygon:
>> x <- c(5,4,5,9,6,6,4,7,10,7,10,4,10)
>> y <- c(6,3,2,6,3,7,5,4,4,7, 5,4, 6)
>
> I would like to plot the following graph:
>> plot(x,y)
>> ord <- c(7,12,2,3,5,8,9,11,13,4,10,6,1)
>> polygon(x[ord],y[ord])
>
> How I can obtain the above ordering (in the example an anti-clockwise
> ordering) such that I can use polygon() to connect the points?
>
> I searched previous messages but I did not find any relevant to this
> problem.

I don't think your problem is well specified! It looks as though you
want to draw a polygon, such that the edges linking successive
vertices do not intersect (and. as it happens, "anticlockwise").

However, your vertices above do not give a unique solution if the
problem is stated in this way. For instance (just looking at the plot)
there are the following (and others):

1  7 12  2  3  5  8  9 11 13  4 10  6  1
(the most "natural" order, perhaps)

1  7 12  8  2  3  5  9 11  4 13 10  6  1
1  7 12  2  8  5  3  9 11 13  4 10  6  1

So what more do you need to state, to make the solution unique?

Best wishes,
Ted.

--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at nessie.mcc.ac.uk>
Fax-to-email: +44 (0)870 167 1972
Date: 14-Jun-04                                       Time: 11:31:55
------------------------------ XFMail ------------------------------

```