[R] Polygon-like interactive selection of plotted points

Roger Bivand Roger.Bivand at nhh.no
Wed Apr 26 23:07:02 CEST 2006


On Wed, 26 Apr 2006, Clint Bowman wrote:

> Roger,
> 
> Just for fun I tried your script--nothing wrong with the script, but I
> created a seven sided polygon by clicking on seven points and letting
> getpoly complete the figure.  At the end I notice that only three of the
> seven vertices are coded as being inside the ploygon (the blue points.)

Yes, whether a boundary vertex (or point exactly on a boundary line) is 
inside or outside is arbitrary, and the default algorithm isn't picky. 
Setting to bound= argument to TRUE or FALSE uses a different algorithm 
which does what you expect:

> inout(poly, poly, bound=NULL)
[1] FALSE FALSE  TRUE  TRUE  TRUE FALSE FALSE
> inout(poly, poly, bound=TRUE)
[1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE
> inout(poly, poly, bound=FALSE)
[1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE

Computational geometry gets even more fun beyond 2D!

Roger

> 
> I'd send you a screen dump but I haven't gotten xwd to work with Exceed.
> Also I haven't checked any docs to see whether this is a known problem but
> suspect that Marc could be surprised by the behavior.
> 
> Clint
> 
> Clint Bowman			INTERNET:	clint at ecy.wa.gov
> Air Dispersion Modeler		INTERNET:	clint at math.utah.edu
> Air Quality Program		VOICE:		(360) 407-6815
> Department of Ecology		FAX:		(360) 407-7534
> 
> 	USPS:  		PO Box 47600, Olympia, WA 98504-7600
> 	Parcels:	300 Desmond Drive, Lacey, WA 98503-1274
> 
> On Wed, 26 Apr 2006, Roger Bivand wrote:
> 
> > On Wed, 26 Apr 2006, Marc Schwartz (via MN) wrote:
> >
> > > On Wed, 2006-04-26 at 18:13 +0100, Florian Nigsch wrote:
> > > > [Please CC me for all replies, since I am not currently subscribed to
> > > > the list.]
> > > >
> > > > Hi all,
> > > >
> > > > I have the following problem/question: Imagine you have a two-
> > > > dimensional plot, and you want to select a number of points, around
> > > > which you could draw a polygon. The points of the polygon are defined
> > > > by clicking in the graphics window (locator()/identify()), all points
> > > > inside the polygon are returned as an object.
> > > >
> > > > Is something like this already implemented?
> > > >
> > > > Thanks a lot in advance,
> > > >
> > > > Florian
> > >
> > > I don't know if anyone has created a single function do to this (though
> > > it is always possible).
> > >
> > > However, using:
> > >
> > >   RSiteSearch("points inside polygon")
> > >
> > > brings up several function hits that, if put together with the above
> > > interactive functions, could be used to do what you wish. That is, input
> > > the matrix of x,y coords of the interactively selected polygon and the
> > > x,y coords of the underlying points set to return the points inside or
> > > outside the polygon boundaries.
> >
> > This sequence from the splancs package should do it:
> >
> > library(splancs)
> > set.seed(20060426)
> > xy <- cbind(x=runif(100), y=runif(100))
> > plot(xy)
> > poly <- getpoly() # this gets the polygon on-screen
> > plot(xy)
> > polygon(poly)
> > io <- inout(xy, poly)
> > # this returns a logical vector for points in the polygon
> > points(xy[io,], pch=16, col="blue")
> >
> > Roger
> >
> > >
> > > Just as an FYI, you might also want to look at ?chull, which is in the
> > > base R distribution and returns the set of points on the convex hull of
> > > the underlying point set. This is to some extent, the inverse of what
> > > you wish to do.
> > >
> > > HTH,
> > >
> > > Marc Schwartz
> > >
> >
> > --
> > Roger Bivand
> > Economic Geography Section, Department of Economics, Norwegian School of
> > Economics and Business Administration, Helleveien 30, N-5045 Bergen,
> > Norway. voice: +47 55 95 93 55; fax +47 55 95 95 43
> > e-mail: Roger.Bivand at nhh.no
> >
> > ______________________________________________
> > R-help at stat.math.ethz.ch mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
> >
> 

-- 
Roger Bivand
Economic Geography Section, Department of Economics, Norwegian School of
Economics and Business Administration, Helleveien 30, N-5045 Bergen,
Norway. voice: +47 55 95 93 55; fax +47 55 95 95 43
e-mail: Roger.Bivand at nhh.no




More information about the R-help mailing list