polygon | R Documentation |
Set of functions to create, manipulate and query polygon objects.
polygon(x, ...)
## Default S3 method:
polygon(x, ...)
as.polygon(x, y = NULL, hole = NULL, ...)
## S3 method for class 'polygon'
plot(p, labels = FALSE, as.lines = FALSE, ...)
in.polygon(p, x, y, ...)
which.polygon(p, x, y, as.list = FALSE)
## S3 method for class 'polygon'
as.data.frame(x)
is.polygon(p)
x |
May be either a ‘polygon’ object, a list containing polygon vertex definitions (with ‘x’ and ‘y’ coordinates or longitude and latitude) or a numeric vector of horizontal polygon vertex coordinates. |
y |
A numeric vector of vertical polygon vertex coordinates. |
hole |
A logical vector specifying whether component polygons are to be treated as holes. The length of the vector may be either the same as numeric ‘x’ and ‘y’ or its length may be that of the number of component polygons of the object. Polygons labelled as holes have negative areas. |
p |
List vector of polygon object definitions. Each element in the list
corresponds to a single polygon object. They contain two fields, |
logical value TRUE
is returned when a point (x
, y
)
lies within and FALSE
if it lies outside each polygon.
polygon
, as.polygon
Creates or converts to a ‘polygon’ object.
plot.polygon
Graphically display a polygon object.
in.polygon
Determine whether a coordinate point lies within a polygon. A logical (n
by k
) matrix is returned where n
is the number of
component polygons in p
and k
is the length of the x
and y
vectors.
which.polygon
Returns the indices specifying to which polygon(s) in a list a point belongs.
area.polygon
Calculates the area of a polygon.
as.data.frame.polygon
Convert polygon object to data frame.
bbox.polygon
Determine bounding box for a polygon object.
draw.polygon
Draw a polygon on a plot.
is.polygon
Checks whether object is a ‘polygon’ object.
point.in.polygon
# Define set of polygon vertices:
x <- c(0, 0, 0.5, 1, 1, 0)
y <- c(0, 1, 0.5, 1, 0, 0)
p <- as.polygon(x, y)
plot(p) # Plot polygon.
area(p) # Calculate area of polygon.
# Generate set of random points:
x <- runif(1000)*2-0.5
y <- runif(1000)*2-0.5
# Plot points:
plot(c(-0.5, 1.5), c(-0.5, 1.5), type = "n")
index <- in.polygon(p, x, y)
points(x[index], y[index], pch = 21, bg = "red")
points(x[!index], y[!index], pch = 21, bg = "blue")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.