decompose: Decompose complex polygons into triangles

Description Usage Arguments Details Value Examples

View source: R/decompose.R

Description

This function wraps RTriangle::triangulate() to make it easier to call for my use case, and returns results appropriate for plotting in ggplot2.

Usage

1
decompose(polygons_df)

Arguments

polygons_df

polygon data.frame with x,y coordinates, 'group' column denoting coordinates which belong to the same group, and 'subgroup' indicating holes within that polygon. It is assumed that any other column values are consistent across the entire group e.g. 'fill'

Details

By combining polyclip and RTriangle packages, this package will successfully decompose into triangles the following polygon types:

Value

data.frame with x, y coordinates of the vertices of the resulting triangles. Also includes idx numbering the triangles.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
## Not run: 
# Polygon with a hole
polygons_df <- df <- data.frame(
  x        = c(4, 8, 8, 4,   6, 7, 7, 6),
  y        = c(4, 4, 8, 8,   6, 6, 7, 7),
  group    = c(1, 1, 1, 1,   1, 1, 1, 1),
  subgroup = c(1, 1, 1, 1,   2, 2, 2, 2)
)
triangles_df <- triangular::decompose(polygons_df)
ggplot(triangles_df) +
    geom_polygon(aes(x, y, fill = as.factor(idx)))

## End(Not run)

coolbutuseless/triangular documentation built on Oct. 9, 2020, 9:39 p.m.