cgalPolygon | R Documentation |
R6 class to represent a CGAL polygon.
new()
Creates a new cgalpolygon
object.
cgalPolygon$new(vertices)
vertices
a numeric matrix with two columns
A cgalPolygon
object.
library(cgalPolygons) ptg <- cgalPolygon$new(pentagram) ptg
print()
Print the cgalPolygon
object.
cgalPolygon$print(...)
...
ignored
No value, just prints some information about the polygon.
area()
Signed area of the polygon.
cgalPolygon$area()
A number, the signed area of the polygon; it is positive if the polygon is counter-clockwise oriented, negative otherwise.
library(cgalPolygons) ptg <- cgalPolygon$new(pentagram) ptg$area() # should be 5 / sqrt(130 + 58*sqrt(5)) 5 / sqrt(130 + 58*sqrt(5))
boundingBox()
Bounding box of the polygon.
cgalPolygon$boundingBox()
A 2x2 matrix giving the lower corner of the bounding box in its first row and the upper corner in its second row.
library(cgalPolygons) ptg <- cgalPolygon$new(pentagram) plot(ptg$boundingBox(), asp = 1) polygon(pentagram)
convexParts()
Decomposition into convex parts. The polygon must be simple and counter-clockwise oriented.
cgalPolygon$convexParts(method = "optimal")
method
the method used: "approx"
, "greene"
,
or "optimal"
A list of matrices; each matrix has two columns and represents a convex polygon.
library(cgalPolygons) ptg <- cgalPolygon$new(pentagram) cxparts <- ptg$convexParts() ptg$plot(col = "yellow", lwd = 3) invisible( lapply(cxparts, function(cxpart) { polygon(cxpart, lwd = 2) }) )
getVertices()
Vertices of the polygon.
cgalPolygon$getVertices()
The vertices in a matrix with two columns.
library(cgalPolygons) ptg <- cgalPolygon$new(pentagram) ptg$getVertices()
intersection()
Intersection of the polygon with another polygon.
cgalPolygon$intersection(plg2)
plg2
a cgalPolygon
object or a cgalPolygonWithHoles
object
A list whose each element is either a cgalPolygon
object
or a cgalPolygonWithHoles
object.
library(cgalPolygons) # function creating a circle circle <- function(x, y, r) { t <- seq(0, 2, length.out = 100)[-1L] t(c(x, y) + r * rbind(cospi(t), sinpi(t))) } # take two circles plg1 <- cgalPolygon$new(circle(-1, 0, 1.25)) plg2 <- cgalPolygon$new(circle(1, 0, 1.25)) # intersection plgList <- plg1$intersection(plg2) plg <- plgList[[1L]] # plot opar <- par(mar = c(0, 0, 0, 0)) plot( NULL, xlim = c(-2.6, 2.6), ylim = c(-1.3, 1.3), asp = 1, xlab = NA, ylab = NA, axes = FALSE ) plg1$plot(lwd = 2, new = FALSE) plg2$plot(lwd = 2, new = FALSE) plg$plot(lwd = 3, col = "red", new = FALSE) par(opar)
isCWO()
Checks whether the polygon is clockwise oriented.
cgalPolygon$isCWO()
A Boolean value.
library(cgalPolygons) ptg <- cgalPolygon$new(pentagram) ptg$isCWO()
isCCWO()
Checks whether the polygon is counter-clockwise oriented.
cgalPolygon$isCCWO()
A Boolean value.
library(cgalPolygons) ptg <- cgalPolygon$new(pentagram) ptg$isCCWO()
isConvex()
Checks whether the polygon is convex.
cgalPolygon$isConvex()
A Boolean value.
library(cgalPolygons) ptg <- cgalPolygon$new(pentagram) ptg$isConvex()
isSimple()
Checks whether the polygon is simple; that means its edges do not intersect (except two consecutive edges which intersect at their common vertex)
cgalPolygon$isSimple()
A Boolean value.
library(cgalPolygons) ptg <- cgalPolygon$new(pentagram) ptg$isSimple()
minkowskiSum()
Minkowski sum of the polygon and another polygon.
cgalPolygon$minkowskiSum(plg2)
plg2
a cgalPolygon
object or a cgalPolygonWithHoles
object, the polygon to add to the reference polygon
Either a cgalPolygonWithHoles
object, or, in the case if
there is no hole in the Minkowski sum, a cgalPolygon
object.
library(cgalPolygons) plg1 <- cgalPolygon$new(decagram) plg2 <- cgalPolygon$new(star) minko <- plg1$minkowskiSum(plg2) minko$plot(lwd = 2, col = "yellowgreen")
plot()
Plot the polygon.
cgalPolygon$plot(..., new = TRUE)
...
arguments passed to polygon
new
Boolean, whether to create a new plot
No returned value, called for side-effect.
library(cgalPolygons) ptg <- cgalPolygon$new(pentagram) ptg$plot(lwd = 3, col = "red")
reverseOrientation()
Reverse the orientation of the polygon.
cgalPolygon$reverseOrientation()
The cgalPolygon
object, invisibly.
library(cgalPolygons) ptg <- cgalPolygon$new(pentagram) ptg$isCCWO() ptg$reverseOrientation() ptg$isCCWO()
subtract()
Difference between the polygon and another polygon.
cgalPolygon$subtract(plg2)
plg2
a cgalPolygon
object or a cgalPolygonWithHoles
object
A list whose each element is either a cgalPolygon
object
or a cgalPolygonWithHoles
object.
library(cgalPolygons) # function creating a circle circle <- function(x, y, r) { t <- seq(0, 2, length.out = 100)[-1L] t(c(x, y) + r * rbind(cospi(t), sinpi(t))) } # take two circles plg1 <- cgalPolygon$new(circle(-1, 0, 1.25)) plg2 <- cgalPolygon$new(circle(1, 0, 1.25)) # difference plgList <- plg1$subtract(plg2) plg <- plgList[[1L]] # plot opar <- par(mar = c(0, 0, 0, 0)) plot( NULL, xlim = c(-2.6, 2.6), ylim = c(-1.3, 1.3), asp = 1, xlab = NA, ylab = NA, axes = FALSE ) plg1$plot(lwd = 2, new = FALSE) plg2$plot(lwd = 2, new = FALSE) plg$plot(lwd = 3, col = "red", new = FALSE) par(opar)
symdiff()
Symmetric difference of the polygon and another polygon.
cgalPolygon$symdiff(plg2)
plg2
a cgalPolygon
object or a cgalPolygonWithHoles
object
A list whose each element is either a cgalPolygon
object
or a cgalPolygonWithHoles
object.
library(cgalPolygons) # function creating a circle circle <- function(x, y, r) { t <- seq(0, 2, length.out = 100)[-1L] t(c(x, y) + r * rbind(cospi(t), sinpi(t))) } # take two circles plg1 <- cgalPolygon$new(circle(-1, 0, 1.25)) plg2 <- cgalPolygon$new(circle(1, 0, 1.25)) # symmetric difference plgList <- plg1$symdiff(plg2) plg <- plgList[[1L]] # plot opar <- par(mar = c(0, 0, 0, 0)) plot( NULL, xlim = c(-2.6, 2.6), ylim = c(-1.3, 1.3), asp = 1, xlab = NA, ylab = NA, axes = FALSE ) plg1$plot(lwd = 2, new = FALSE) plg2$plot(lwd = 2, new = FALSE) plg$plot(list(lwd = 3, col = "red"), col = "white", new = FALSE) par(opar)
union()
Union of the polygon with another polygon.
cgalPolygon$union(plg2)
plg2
a cgalPolygon
object or a cgalPolygonWithHoles
object
A list whose each element is either a cgalPolygon
object
or a cgalPolygonWithHoles
object.
library(cgalPolygons) # function creating a circle circle <- function(x, y, r) { t <- seq(0, 2, length.out = 100)[-1L] t(c(x, y) + r * rbind(cospi(t), sinpi(t))) } # take two circles plg1 <- cgalPolygon$new(circle(-1, 0, 1.25)) plg2 <- cgalPolygon$new(circle(1, 0, 1.25)) # union plgList <- plg1$union(plg2) plg <- plgList[[1L]] # plot opar <- par(mar = c(0, 0, 0, 0)) plot( NULL, xlim = c(-2.6, 2.6), ylim = c(-1.3, 1.3), asp = 1, xlab = NA, ylab = NA, axes = FALSE ) plg1$plot(lwd = 2, new = FALSE) plg2$plot(lwd = 2, new = FALSE) plg$plot(lwd = 3, col = "red", new = FALSE) par(opar)
whereIs()
Locate point(s) with respect to the polygon. The polygon must be simple.
cgalPolygon$whereIs(points)
points
a numeric matrix with two columns, or a numeric vector of length 2 (for a single point)
An integer vector with possible values -1
, 1
, or
0
: value -1
for outside, 1
for inside, and
0
if the point is on the boundary of the polygon.
library(cgalPolygons) ptg <- cgalPolygon$new(pentagram) pt1 <- c(0, 0) # inside pt2 <- c(4, 0) # outside ptg$whereIs(rbind(pt1, pt2))
## ------------------------------------------------
## Method `cgalPolygon$new`
## ------------------------------------------------
library(cgalPolygons)
ptg <- cgalPolygon$new(pentagram)
ptg
## ------------------------------------------------
## Method `cgalPolygon$area`
## ------------------------------------------------
library(cgalPolygons)
ptg <- cgalPolygon$new(pentagram)
ptg$area() # should be 5 / sqrt(130 + 58*sqrt(5))
5 / sqrt(130 + 58*sqrt(5))
## ------------------------------------------------
## Method `cgalPolygon$boundingBox`
## ------------------------------------------------
library(cgalPolygons)
ptg <- cgalPolygon$new(pentagram)
plot(ptg$boundingBox(), asp = 1)
polygon(pentagram)
## ------------------------------------------------
## Method `cgalPolygon$convexParts`
## ------------------------------------------------
library(cgalPolygons)
ptg <- cgalPolygon$new(pentagram)
cxparts <- ptg$convexParts()
ptg$plot(col = "yellow", lwd = 3)
invisible(
lapply(cxparts, function(cxpart) {
polygon(cxpart, lwd = 2)
})
)
## ------------------------------------------------
## Method `cgalPolygon$getVertices`
## ------------------------------------------------
library(cgalPolygons)
ptg <- cgalPolygon$new(pentagram)
ptg$getVertices()
## ------------------------------------------------
## Method `cgalPolygon$intersection`
## ------------------------------------------------
library(cgalPolygons)
# function creating a circle
circle <- function(x, y, r) {
t <- seq(0, 2, length.out = 100)[-1L]
t(c(x, y) + r * rbind(cospi(t), sinpi(t)))
}
# take two circles
plg1 <- cgalPolygon$new(circle(-1, 0, 1.25))
plg2 <- cgalPolygon$new(circle(1, 0, 1.25))
# intersection
plgList <- plg1$intersection(plg2)
plg <- plgList[[1L]]
# plot
opar <- par(mar = c(0, 0, 0, 0))
plot(
NULL, xlim = c(-2.6, 2.6), ylim = c(-1.3, 1.3), asp = 1,
xlab = NA, ylab = NA, axes = FALSE
)
plg1$plot(lwd = 2, new = FALSE)
plg2$plot(lwd = 2, new = FALSE)
plg$plot(lwd = 3, col = "red", new = FALSE)
par(opar)
## ------------------------------------------------
## Method `cgalPolygon$isCWO`
## ------------------------------------------------
library(cgalPolygons)
ptg <- cgalPolygon$new(pentagram)
ptg$isCWO()
## ------------------------------------------------
## Method `cgalPolygon$isCCWO`
## ------------------------------------------------
library(cgalPolygons)
ptg <- cgalPolygon$new(pentagram)
ptg$isCCWO()
## ------------------------------------------------
## Method `cgalPolygon$isConvex`
## ------------------------------------------------
library(cgalPolygons)
ptg <- cgalPolygon$new(pentagram)
ptg$isConvex()
## ------------------------------------------------
## Method `cgalPolygon$isSimple`
## ------------------------------------------------
library(cgalPolygons)
ptg <- cgalPolygon$new(pentagram)
ptg$isSimple()
## ------------------------------------------------
## Method `cgalPolygon$minkowskiSum`
## ------------------------------------------------
library(cgalPolygons)
plg1 <- cgalPolygon$new(decagram)
plg2 <- cgalPolygon$new(star)
minko <- plg1$minkowskiSum(plg2)
minko$plot(lwd = 2, col = "yellowgreen")
## ------------------------------------------------
## Method `cgalPolygon$plot`
## ------------------------------------------------
library(cgalPolygons)
ptg <- cgalPolygon$new(pentagram)
ptg$plot(lwd = 3, col = "red")
## ------------------------------------------------
## Method `cgalPolygon$reverseOrientation`
## ------------------------------------------------
library(cgalPolygons)
ptg <- cgalPolygon$new(pentagram)
ptg$isCCWO()
ptg$reverseOrientation()
ptg$isCCWO()
## ------------------------------------------------
## Method `cgalPolygon$subtract`
## ------------------------------------------------
library(cgalPolygons)
# function creating a circle
circle <- function(x, y, r) {
t <- seq(0, 2, length.out = 100)[-1L]
t(c(x, y) + r * rbind(cospi(t), sinpi(t)))
}
# take two circles
plg1 <- cgalPolygon$new(circle(-1, 0, 1.25))
plg2 <- cgalPolygon$new(circle(1, 0, 1.25))
# difference
plgList <- plg1$subtract(plg2)
plg <- plgList[[1L]]
# plot
opar <- par(mar = c(0, 0, 0, 0))
plot(
NULL, xlim = c(-2.6, 2.6), ylim = c(-1.3, 1.3), asp = 1,
xlab = NA, ylab = NA, axes = FALSE
)
plg1$plot(lwd = 2, new = FALSE)
plg2$plot(lwd = 2, new = FALSE)
plg$plot(lwd = 3, col = "red", new = FALSE)
par(opar)
## ------------------------------------------------
## Method `cgalPolygon$symdiff`
## ------------------------------------------------
library(cgalPolygons)
# function creating a circle
circle <- function(x, y, r) {
t <- seq(0, 2, length.out = 100)[-1L]
t(c(x, y) + r * rbind(cospi(t), sinpi(t)))
}
# take two circles
plg1 <- cgalPolygon$new(circle(-1, 0, 1.25))
plg2 <- cgalPolygon$new(circle(1, 0, 1.25))
# symmetric difference
plgList <- plg1$symdiff(plg2)
plg <- plgList[[1L]]
# plot
opar <- par(mar = c(0, 0, 0, 0))
plot(
NULL, xlim = c(-2.6, 2.6), ylim = c(-1.3, 1.3), asp = 1,
xlab = NA, ylab = NA, axes = FALSE
)
plg1$plot(lwd = 2, new = FALSE)
plg2$plot(lwd = 2, new = FALSE)
plg$plot(list(lwd = 3, col = "red"), col = "white", new = FALSE)
par(opar)
## ------------------------------------------------
## Method `cgalPolygon$union`
## ------------------------------------------------
library(cgalPolygons)
# function creating a circle
circle <- function(x, y, r) {
t <- seq(0, 2, length.out = 100)[-1L]
t(c(x, y) + r * rbind(cospi(t), sinpi(t)))
}
# take two circles
plg1 <- cgalPolygon$new(circle(-1, 0, 1.25))
plg2 <- cgalPolygon$new(circle(1, 0, 1.25))
# union
plgList <- plg1$union(plg2)
plg <- plgList[[1L]]
# plot
opar <- par(mar = c(0, 0, 0, 0))
plot(
NULL, xlim = c(-2.6, 2.6), ylim = c(-1.3, 1.3), asp = 1,
xlab = NA, ylab = NA, axes = FALSE
)
plg1$plot(lwd = 2, new = FALSE)
plg2$plot(lwd = 2, new = FALSE)
plg$plot(lwd = 3, col = "red", new = FALSE)
par(opar)
## ------------------------------------------------
## Method `cgalPolygon$whereIs`
## ------------------------------------------------
library(cgalPolygons)
ptg <- cgalPolygon$new(pentagram)
pt1 <- c(0, 0) # inside
pt2 <- c(4, 0) # outside
ptg$whereIs(rbind(pt1, pt2))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.