Skip to content

Instantly share code, notes, and snippets.

@dsparks
Created December 4, 2012 02:53
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dsparks/4200109 to your computer and use it in GitHub Desktop.
Save dsparks/4200109 to your computer and use it in GitHub Desktop.
Alpha Hull Example
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))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment