Skip to content

draw polygon / path with holes #252

@tdhock

Description

@tdhock

this is apparently possible with d3 https://devdocs.io/d3~3/geo-paths.md#_path

and it can be done with R grid / isoband

# shape with hole
m <- matrix(c(0, 0, 0, 0, 0, 0,
              0, 1, 1, 1, 1, 0,
              0, 1, 0, 0, 1, 0,
              0, 1, 0, 0, 1, 0,
              0, 1, 1, 1, 1, 0,
              0, 0, 0, 0, 0, 0), 6, 6, byrow = TRUE)
isoband::plot_iso(m, 0.5, 1.5)
Image

this animint code gives two filled polygons instead of one with a hole.

res=isoband::isobands((1:ncol(m))/(ncol(m)+1), (nrow(m):1)/(nrow(m)+1), m, 0.5, 1.5)[[1]]
## ?grid.path
##     rule: A character value specifying the fill rule: either
##           ‘"winding"’ or ‘"evenodd"’.
## A path is like a polygon except that the former can contain holes,
## as interpreted by the fill rule; these fill a region if the path
## border encircles it an odd or non-zero number of times,
## respectively.
viz <- animint(poly=ggplot()+
  geom_polygon(aes(
    x, y, group=id, fill=ID),
    data=as.data.table(res)[, ID := factor(id)]))
viz$poly
viz

> res
$x
 [1] 0.7142857 0.5714286 0.4285714 0.2857143 0.2142857 0.2142857 0.2142857 0.2142857 0.2857143
[10] 0.4285714 0.5714286 0.7142857 0.7857143 0.7857143 0.7857143 0.7857143 0.5714286 0.6428571
[19] 0.6428571 0.5714286 0.4285714 0.3571429 0.3571429 0.4285714

$y
 [1] 0.2142857 0.2142857 0.2142857 0.2142857 0.2857143 0.4285714 0.5714286 0.7142857 0.7857143
[10] 0.7857143 0.7857143 0.7857143 0.7142857 0.5714286 0.4285714 0.2857143 0.3571429 0.4285714
[19] 0.5714286 0.6428571 0.6428571 0.5714286 0.4285714 0.3571429

$id
 [1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2
Image Image

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions