polyclip {polyclip} | R Documentation |
Polygon Clipping
Description
Find intersection, union or set difference of two polygonal regions or polygonal lines.
Usage
polyclip(A, B, op=c("intersection", "union", "minus", "xor"),
...,
eps, x0, y0,
fillA=c("evenodd", "nonzero", "positive", "negative"),
fillB=c("evenodd", "nonzero", "positive", "negative"),
closed = TRUE)
Arguments
A , B |
Data specifying polygons. See Details. |
op |
Set operation to be performed to combine |
... |
Ignored. |
eps |
Spatial resolution for coordinates. A single positive numeric value. |
x0 , y0 |
Spatial origin for coordinates. Numeric values. |
fillA , fillB |
Polygon-filling rules for |
closed |
Logical value specifying whether |
Details
This is an interface to the polygon-clipping library
Clipper
written by Angus Johnson.
Given two polygonal regions A
and B
the function polyclip
performs one of the following
geometrical operations:
-
op="intersection"
: set intersection ofA
andB
. -
op="union"
: set union ofA
andB
. -
op="minus"
: set subtraction (sometimes called set difference): the region covered byA
that is not covered byB
. -
op="xor"
: exclusive set difference (sometimes called exclusive-or): the region covered by exactly one of the setsA
andB
.
Each of the arguments A
and B
represents a region in the
Euclidean plane bounded by closed polygons. The format of these
arguments is either
-
a list containing two components
x
andy
giving the coordinates of the vertices of a single polygon. The last vertex should not repeat the first vertex. -
a
list
oflist(x,y)
structures giving the coordinates of the vertices of several polygons.
Note that calculations are performed in integer arithmetic: see below.
The interpretation of the polygons
depends on the polygon-filling rule for A
and B
that is specified by the arguments fillA
and fillB
respectively.
- 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
and B
.
Author(s)
Angus Johnson. Ported to R by Adrian Baddeley Adrian.Baddeley@curtin.edu.au. Additional modification by Paul Murrell.
References
Clipper Website: http://www.angusj.com
Vatti, B. (1992) A generic solution to polygon clipping. Communications of the ACM 35 (7) 56–63. https://dl.acm.org/doi/10.1145/129902.129906
Agoston, M.K. (2005) Computer graphics and geometric modeling: implementation and algorithms. Springer-Verlag. http://books.google.com/books?q=vatti+clipping+agoston
Chen, X. and McMains, S. (2005) Polygon Offsetting by Computing Winding Numbers. Paper no. DETC2005-85513 in Proceedings of IDETC/CIE 2005 (ASME 2005 International Design Engineering Technical Conferences and Computers and Information in Engineering Conference), pp. 565–575 https://mcmains.me.berkeley.edu/pubs/DAC05OffsetPolygon.pdf
See Also
polysimplify
,
polyoffset
,
polylineoffset
,
polyminkowski
Examples
A <- list(list(x=1:10, y=c(1:5,5:1)))
B <- list(list(x=c(2,8,8,2),y=c(0,0,10,10)))
plot(c(0,10),c(0,10), type="n", axes=FALSE,
xlab="", ylab="", main="intersection of closed polygons")
invisible(lapply(A, polygon))
invisible(lapply(B, polygon))
C <- polyclip(A, B)
polygon(C[[1]], lwd=3, col=3)
plot(c(0,10),c(0,10), type="n", axes=FALSE,
xlab="", ylab="", main="intersection of open polyline and closed polygon")
invisible(lapply(A, polygon))
invisible(lapply(B, polygon))
E <- polyclip(A, B, closed=FALSE)
invisible(lapply(E, lines, col="purple", lwd=5))