library(RCDT)
# random points in a square ####
set.seed(314)
library(uniformly)
square <- rbind(
c(-1, 1), c(1, 1), c(1, -1), c(-1, -1)
)
pts_in_square <- runif_in_cube(10L, d = 2L)
pts <- rbind(square, pts_in_square)
del <- delaunay(pts)
opar <- par(mar = c(0, 0, 0, 0))
plotDelaunay(
del, type = "n", xlab = NA, ylab = NA, asp = 1,
fillcolor = "random", luminosity = "light", lty_edges = "dashed"
)
par(opar)
# the order of the points matters ####
# the Delaunay triangulation is not unique in general;
# it can depend on the order of the points
points <- cbind(
c(1, 2, 1, 3, 2, 1, 4, 3, 2, 1, 4, 3, 2, 4, 3, 4),
c(1, 1, 2, 1, 2, 3, 1, 2, 3, 4, 2, 3, 4, 3, 4, 4)
)
del <- delaunay(points)
opar <- par(mar = c(0, 0, 0, 0))
plotDelaunay(
del, type = "p", pch = 19, xlab = NA, ylab = NA, axes = FALSE,
asp = 1, lwd_edges = 2, lwd_borders = 3
)
par(opar)
# now we randomize the order of the points
set.seed(666L)
points2 <- points[sample.int(nrow(points)), ]
del2 <- delaunay(points2)
opar <- par(mar = c(0, 0, 0, 0))
plotDelaunay(
del2, type = "p", pch = 19, xlab = NA, ylab = NA, axes = FALSE,
asp = 1, lwd_edges = 2, lwd_borders = 3
)
par(opar)
# a constrained Delaunay triangulation: outer and inner dodecagons ####
# points
nsides <- 12L
angles <- seq(0, 2*pi, length.out = nsides+1L)[-1L]
points <- cbind(cos(angles), sin(angles))
points <- rbind(points, points/1.5)
# constraint edges
indices <- 1L:nsides
edges_outer <- cbind(
indices, c(indices[-1L], indices[1L])
)
edges_inner <- edges_outer + nsides
edges <- rbind(edges_outer, edges_inner)
# constrained Delaunay triangulation
del <- delaunay(points, edges)
# plot
opar <- par(mar = c(0, 0, 0, 0))
plotDelaunay(
del, type = "n", fillcolor = "yellow", lwd_borders = 2, asp = 1,
axes = FALSE, xlab = NA, ylab = NA
)
par(opar)
# another constrained Delaunay triangulation: a face ####
V <- read.table(
system.file("extdata", "face_vertices.txt", package = "RCDT")
)
E <- read.table(
system.file("extdata", "face_edges.txt", package = "RCDT")
)
del <- delaunay(
points = as.matrix(V)[, c(2L, 3L)], edges = as.matrix(E)[, c(2L, 3L)]
)
opar <- par(mar = c(0, 0, 0, 0))
plotDelaunay(
del, type="n", col_edges = NULL, fillcolor = "salmon",
col_borders = "black", col_constraints = "purple",
lwd_borders = 3, lwd_constraints = 3,
asp = 1, axes = FALSE, xlab = NA, ylab = NA
)
par(opar)
Run the code above in your browser using DataLab