Skip to content
Prev 893 / 29559 Next

Truncating polygons in R

I have one suggested extension to Barry's idea.

With Barry's idea the polygons will all be inside the square, but there
will be some area in the square that is not inside any of the polygons.
If you want the entire square to be included within the polygons then
take Barrys Idea of projecting the points perpendicular to the sides,
but instead of projecting them onto the sides, project them beyond that
side so that they are the same distance from the side of the square.
Then the set of points that are equal distance from the projected point
and the original point will fall along the border of the square.
Consider the following code:

x <- runif(10)
y <- runif(10)

plot(x,y)

plot(voronoi.polygons(voronoi.mosaic(x,y)))
points(x,y)

xx <- c( x,  x,   x, -x, 2-x )
yy <- c( y, -y, 2-y,  y,   y )

plot(voronoi.polygons(voronoi.mosaic(xx,yy)))
points(x,y)


This is a little bit of overkill (you may want to delete some of the
projected points), but you can see that in the center is a set of
polygons that together form the unit square around the data points.  It
is now just a matter of extracting only those polygons.

Hope this helps,