cgalPolygon: R6 class to represent a CGAL polygon

cgalPolygonR Documentation

R6 class to represent a CGAL polygon

Description

R6 class to represent a CGAL polygon.

Methods

Public methods


Method new()

Creates a new cgalpolygon object.

Usage
cgalPolygon$new(vertices)
Arguments
vertices

a numeric matrix with two columns

Returns

A cgalPolygon object.

Examples
library(cgalPolygons)
ptg <- cgalPolygon$new(pentagram)
ptg

Method print()

Print the cgalPolygon object.

Usage
cgalPolygon$print(...)
Arguments
...

ignored

Returns

No value, just prints some information about the polygon.


Method area()

Signed area of the polygon.

Usage
cgalPolygon$area()
Returns

A number, the signed area of the polygon; it is positive if the polygon is counter-clockwise oriented, negative otherwise.

Examples
library(cgalPolygons)
ptg <- cgalPolygon$new(pentagram)
ptg$area() # should be 5 / sqrt(130 + 58*sqrt(5))
5 / sqrt(130 + 58*sqrt(5))

Method boundingBox()

Bounding box of the polygon.

Usage
cgalPolygon$boundingBox()
Returns

A 2x2 matrix giving the lower corner of the bounding box in its first row and the upper corner in its second row.

Examples
library(cgalPolygons)
ptg <- cgalPolygon$new(pentagram)
plot(ptg$boundingBox(), asp = 1)
polygon(pentagram)

Method convexParts()

Decomposition into convex parts. The polygon must be simple and counter-clockwise oriented.

Usage
cgalPolygon$convexParts(method = "optimal")
Arguments
method

the method used: "approx", "greene", or "optimal"

Returns

A list of matrices; each matrix has two columns and represents a convex polygon.

Examples
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 getVertices()

Vertices of the polygon.

Usage
cgalPolygon$getVertices()
Returns

The vertices in a matrix with two columns.

Examples
library(cgalPolygons)
ptg <- cgalPolygon$new(pentagram)
ptg$getVertices()

Method intersection()

Intersection of the polygon with another polygon.

Usage
cgalPolygon$intersection(plg2)
Arguments
plg2

a cgalPolygon object or a cgalPolygonWithHoles object

Returns

A list whose each element is either a cgalPolygon object or a cgalPolygonWithHoles object.

Examples
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 isCWO()

Checks whether the polygon is clockwise oriented.

Usage
cgalPolygon$isCWO()
Returns

A Boolean value.

Examples
library(cgalPolygons)
ptg <- cgalPolygon$new(pentagram)
ptg$isCWO()

Method isCCWO()

Checks whether the polygon is counter-clockwise oriented.

Usage
cgalPolygon$isCCWO()
Returns

A Boolean value.

Examples
library(cgalPolygons)
ptg <- cgalPolygon$new(pentagram)
ptg$isCCWO()

Method isConvex()

Checks whether the polygon is convex.

Usage
cgalPolygon$isConvex()
Returns

A Boolean value.

Examples
library(cgalPolygons)
ptg <- cgalPolygon$new(pentagram)
ptg$isConvex()

Method isSimple()

Checks whether the polygon is simple; that means its edges do not intersect (except two consecutive edges which intersect at their common vertex)

Usage
cgalPolygon$isSimple()
Returns

A Boolean value.

Examples
library(cgalPolygons)
ptg <- cgalPolygon$new(pentagram)
ptg$isSimple()

Method minkowskiSum()

Minkowski sum of the polygon and another polygon.

Usage
cgalPolygon$minkowskiSum(plg2)
Arguments
plg2

a cgalPolygon object or a cgalPolygonWithHoles object, the polygon to add to the reference polygon

Returns

Either a cgalPolygonWithHoles object, or, in the case if there is no hole in the Minkowski sum, a cgalPolygon object.

Examples
library(cgalPolygons)
plg1 <- cgalPolygon$new(decagram)
plg2 <- cgalPolygon$new(star)
minko <- plg1$minkowskiSum(plg2)
minko$plot(lwd = 2, col = "yellowgreen")

Method plot()

Plot the polygon.

Usage
cgalPolygon$plot(..., new = TRUE)
Arguments
...

arguments passed to polygon

new

Boolean, whether to create a new plot

Returns

No returned value, called for side-effect.

Examples
library(cgalPolygons)
ptg <- cgalPolygon$new(pentagram)
ptg$plot(lwd = 3, col = "red")

Method reverseOrientation()

Reverse the orientation of the polygon.

Usage
cgalPolygon$reverseOrientation()
Returns

The cgalPolygon object, invisibly.

Examples
library(cgalPolygons)
ptg <- cgalPolygon$new(pentagram)
ptg$isCCWO()
ptg$reverseOrientation()
ptg$isCCWO()

Method subtract()

Difference between the polygon and another polygon.

Usage
cgalPolygon$subtract(plg2)
Arguments
plg2

a cgalPolygon object or a cgalPolygonWithHoles object

Returns

A list whose each element is either a cgalPolygon object or a cgalPolygonWithHoles object.

Examples
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 symdiff()

Symmetric difference of the polygon and another polygon.

Usage
cgalPolygon$symdiff(plg2)
Arguments
plg2

a cgalPolygon object or a cgalPolygonWithHoles object

Returns

A list whose each element is either a cgalPolygon object or a cgalPolygonWithHoles object.

Examples
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 union()

Union of the polygon with another polygon.

Usage
cgalPolygon$union(plg2)
Arguments
plg2

a cgalPolygon object or a cgalPolygonWithHoles object

Returns

A list whose each element is either a cgalPolygon object or a cgalPolygonWithHoles object.

Examples
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 whereIs()

Locate point(s) with respect to the polygon. The polygon must be simple.

Usage
cgalPolygon$whereIs(points)
Arguments
points

a numeric matrix with two columns, or a numeric vector of length 2 (for a single point)

Returns

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.

Examples
library(cgalPolygons)
ptg <- cgalPolygon$new(pentagram)
pt1 <- c(0, 0) # inside
pt2 <- c(4, 0) # outside
ptg$whereIs(rbind(pt1, pt2))

Examples


## ------------------------------------------------
## 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))

cgalPolygons documentation built on May 31, 2023, 8:16 p.m.