Remove Self-Intersections from a Polygon

Share:

Description

This function attempts to remove self-intersections and duplicated vertices from the given polygon.

Usage

1
2
  polysimplify(A, ..., eps, x0, y0,
               filltype = c("evenodd", "nonzero", "positive", "negative"))

Arguments

A

Data specifying a polygon or polygons. See Details.

...

Ignored.

eps

Spatial resolution for coordinates.

x0,y0

Spatial origin for coordinates.

filltype

Polygon-filling rule. See Details.

Details

This is an interface to the function SimplifyPolygons in Angus Johnson's C++ library Clipper. It tries to remove self-intersections from the supplied polygon, by performing a boolean union operation using the nominated filltype. The result may be one or several polygons.

The argument A should be either

  • a list containing two components x and y giving the coordinates of the vertices of a single polygon. The last vertex should not repeat the first vertex.

  • a list of list(x,y) structures giving the coordinates of the vertices of several polygons.

The argument filltype determines the polygon fill type.

Even-Odd:

The default rule is even-odd filling, in which every polygon edge demarcates a boundary between the inside and outside of the region. It does not matter whether a polygon is traversed in clockwise or anticlockwise order. Holes are determined simply by their locations relative to other polygons such that outers contain holes and holes contain outers.

Non-Zero:

Under the nonzero filling rule, an outer boundary must be traversed in clockwise order, while a hole must be traversed in anticlockwise order.

Positive:

Under the positive filling rule, the filled region consists of all points with positive winding number.

Negative:

Under the negative filling rule, the filled region consists of all points with negative winding number.

Calculations are performed in integer arithmetic after subtracting x0,y0 from the coordinates, dividing by eps, and rounding to the nearest integer. Thus, eps is the effective spatial resolution. The default values ensure reasonable accuracy.

Value

Data specifying polygons, in the same format as A.

Author(s)

Angus Johnson. Ported to R by Adrian Baddeley Adrian.Baddeley@curtin.edu.au.

References

Clipper Website: http://www.angusj.com

See Also

polyclip, polyoffset, polylineoffset, polyminkowski

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
  theta <- 2 * pi * (0:5) * 2/5

  A <- list(list(x=sin(theta), y=cos(theta)))
  B <- polysimplify(A, filltype="nonzero")

  opa <- par(mfrow=c(1,2))
  plot(c(-1,1),c(-1,1), type="n", axes=FALSE, xlab="", ylab="")
  with(A[[1]], segments(x[-6], y[-6], x[-1], y[-1], col="red"))
  points(A[[1]], col="red")

  with(A[[1]], text(x[1:5], y[1:5], labels=1:5, cex=2))
  plot(c(-1,1),c(-1,1), type="n", axes=FALSE, xlab="", ylab="")
  polygon(B[[1]], lwd=3, col="green")
  with(B[[1]], text(x,y,labels=seq_along(x), cex=2))
  par(opa)