## ------------------------------------------------
## Method `cgalPolygonWithHoles$new`
## ------------------------------------------------
library(cgalPolygons)
pwh <- cgalPolygonWithHoles$new(
squareWithHole[["outerSquare"]], list(squareWithHole[["innerSquare"]])
)
pwh
## ------------------------------------------------
## Method `cgalPolygonWithHoles$area`
## ------------------------------------------------
library(cgalPolygons)
pwh <- cgalPolygonWithHoles$new(
squareWithHole[["outerSquare"]], list(squareWithHole[["innerSquare"]])
)
pwh$area() # should be 12
## ------------------------------------------------
## Method `cgalPolygonWithHoles$boundingBox`
## ------------------------------------------------
library(cgalPolygons)
pwh <- cgalPolygonWithHoles$new(
squareWithHole[["outerSquare"]], list(squareWithHole[["innerSquare"]])
)
pwh$boundingBox()
## ------------------------------------------------
## Method `cgalPolygonWithHoles$convexParts`
## ------------------------------------------------
library(cgalPolygons)
pwh <- cgalPolygonWithHoles$new(
squareWithHole[["outerSquare"]], list(squareWithHole[["innerSquare"]])
)
cxparts <- pwh$convexParts()
pwh$plot(list(), density = 10)
invisible(
lapply(cxparts, function(cxpart) {
polygon(cxpart, lwd = 2)
})
)
## ------------------------------------------------
## Method `cgalPolygonWithHoles$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 with a hole
plg1 <- cgalPolygonWithHoles$new(
circle(-1, 0, 1.5), holes = list(circle(-1, 0, 0.8))
)
plg2 <- cgalPolygonWithHoles$new(
circle(1, 0, 1.5), holes = list(circle(1, 0, 0.8))
)
# intersection
plgList <- plg1$intersection(plg2)
plg <- plgList[[1]]
# plot
opar <- par(mar = c(0, 0, 0, 0))
plot(
NULL, xlim = c(-2.6, 2.6), ylim = c(-1.6, 1.6), asp = 1,
xlab = NA, ylab = NA, axes = FALSE
)
plg1$plot(list(lwd = 2), lwd = 2, density = 10, new = FALSE)
plg2$plot(list(lwd = 2), lwd = 2, density = 10, new = FALSE)
plg$plot(lwd = 4, col = "red", new = FALSE)
par(opar)
## ------------------------------------------------
## Method `cgalPolygonWithHoles$minkowskiSum`
## ------------------------------------------------
library(cgalPolygons)
plg1 <- cgalPolygonWithHoles$new(decagram)
plg2 <- cgalPolygonWithHoles$new(star)
minko <- plg1$minkowskiSum(plg2)
minko$plot(lwd = 2, col = "limegreen")
## ------------------------------------------------
## Method `cgalPolygonWithHoles$plot`
## ------------------------------------------------
library(cgalPolygons)
pwh <- cgalPolygonWithHoles$new(
squareWithHole[["outerSquare"]], list(squareWithHole[["innerSquare"]])
)
pwh$plot(
outerpars = list(lwd = 2), density = 10
)
## ------------------------------------------------
## Method `cgalPolygonWithHoles$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 with a hole
plg1 <- cgalPolygonWithHoles$new(
circle(-1, 0, 1.5), holes = list(circle(-1, 0, 0.8))
)
plg2 <- cgalPolygonWithHoles$new(
circle(1, 0, 1.5), holes = list(circle(1, 0, 0.8))
)
# difference
plgList <- plg1$subtract(plg2)
# plot
opar <- par(mar = c(0, 0, 0, 0))
plot(
NULL, xlim = c(-2.6, 2.6), ylim = c(-1.6, 1.6), asp = 1,
xlab = NA, ylab = NA, axes = FALSE
)
plgList[[1]]$plot(lwd = 4, col = "red", new = FALSE)
plgList[[2]]$plot(lwd = 4, col = "red", new = FALSE)
par(opar)
## ------------------------------------------------
## Method `cgalPolygonWithHoles$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 with a hole
plg1 <- cgalPolygonWithHoles$new(
circle(-1, 0, 1.5), holes = list(circle(-1, 0, 0.8))
)
plg2 <- cgalPolygonWithHoles$new(
circle(1, 0, 1.5), holes = list(circle(1, 0, 0.8))
)
# symmetric difference
plgList <- plg1$symdiff(plg2)
plg <- plgList[[1L]]
# plot
opar <- par(mar = c(0, 0, 0, 0))
plg$plot(list(lwd = 4, col = "red"), lwd = 4, col = "white")
par(opar)
## ------------------------------------------------
## Method `cgalPolygonWithHoles$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 with a hole
plg1 <- cgalPolygonWithHoles$new(
circle(-1, 0, 1.5), holes = list(circle(-1, 0, 0.8))
)
plg2 <- cgalPolygonWithHoles$new(
circle(1, 0, 1.5), holes = list(circle(1, 0, 0.8))
)
# union
plgList <- plg1$union(plg2)
plg <- plgList[[1]]
# plot
opar <- par(mar = c(0, 0, 0, 0))
plg$plot(list(lwd = 4, col = "red"), lwd = 4, col = "white")
par(opar)
Run the code above in your browser using DataLab