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. Thanks, Luca +-----------------------------------------------------------------------+ | Dr. Luca Scrucca | | Dipartimento di Scienze Statistiche tel. +39 - 075 - 5855278 | | Universitä½® degli Studi di Perugia fax. +39 - 075 - 5855950 | | Via Pascoli - C.P. 1315 Succ. 1 | | 06100 PERUGIA (ITALY) | | (o_ (o_ (o_ | | E-mail: luca at stat.unipg.it //\ //\ //\ | | Web page: http://www.stat.unipg.it/luca V_/_ V_/_ V_/_ | +-----------------------------------------------------------------------+
On Mon, 14 Jun 2004, 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?What exactly is the ordering? The polygon is not convex, so the ordering depends on where you measure angles from. Generally though you could try something like. xy <- cbind(x,y) angM <- xy - rep(c(7,5), each=length(x)) angs <- apply(angM, 1, function(x) atan2(x[2], x[1])) ord <- sort.list(angs) which seems to replicate your solution.> I searched previous messages but I did not find any relevant to this > problem.It is not really an R question. -- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
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 ------------------------------