## ------------------------------------------------
## 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))
Run the code above in your browser using DataLab