public
Last active

Alpha Hull Example

  • Download Gist
alphahull.R
R
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
doInstall <- TRUE
toInstall <- c("alphahull")
if(doInstall){install.packages(toInstall, repos = "http://cran.us.r-project.org")}
lapply(toInstall, library, character.only = TRUE)
 
# Generate some sample data:
myData <- data.frame(x = rnorm(500), y = rnorm(500))
# With a unit-circle hole in the center:
myData <- myData[sqrt(rowSums(myData^2)) > 1, ]
plot(myData)
 
# Convex hull
cHullPoints <- chull(myData)
polygon(myData[cHullPoints, ])
 
aHullPoints <- ahull(myData, alpha = 1)
plot(aHullPoints) # Plot alpha hull
plot(aHullPoints, wlines = "both") # with Delaunay triangulation and Voronoi
 
# Area and lenght of alpha hull shape
areaahull(aHullPoints)
lengthahull(aHullPoints$arcs)
 
# See how the hull varies with alpha:
par(mfcol = c(4, 4), mai = c(0, 0, 0, 0))
lapply((1:16/10)^2, function(ii){
plot(ahull(myData, alpha = ii))
text(0, 0, ii) })
par(mfcol = c(1, 1), mai = c(1, 1, 1, 1))

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.