alphahull: an R Package for Alpha-Convex Hull

April 16, 2010
By

(This article was first published on Statistics, R, Graphics and Fun » R Language, and kindly contributed to R-bloggers)

A new paper on the α-convex hull appeared in the Journal of Statistical Software today (http://www.jstatsoft.org/v34/i05/paper). The α-convex hull is an interesting problem which caught my attention long time ago but I didn’t know a solution then. R has a function chull() which can generate (indices of) the convex hull for a series of points. Now we can use the R package alphahull to compute the α-convex hull. For those who are not familiar with the α-convex hull, the animation below might be a good illustration for the difference between a convex hull and an α-convex hull. Note how the parameter α affects the shape of the hull:

alpha-convex hull with different alpha's

alpha-convex hull with different alpha's

The above animation can be reproduced with the code below (uncomment the lines to create a GIF animation with the animation package):

set.seed(123)
theta = runif(n <- 300, 0, 2 * pi)
r = sqrt(runif(n, 0.25^2, 0.5^2))
x = cbind(0.5 + r * cos(theta), 0.5 + r * sin(theta))

## library(animation)
## saveMovie({
    library(alphahull)
    par(mar = rep(0, 4), xaxt = "n", yaxt = "n", bg = "black", col = "white")
    for (alpha in seq(0.25, 0, -0.01)) {
        plot(ahull(x, alpha = alpha), pch = 20, col = "white", panel.last = text(0.5,
            0.5, sprintf("alpha = %.2f", alpha), cex = 2))
    }
## }, moviename = "alpha-convex-hull")

Again, you may interactively play with the convex hull using the gWidgets package:

## install.packages('gWidgetsRGtk2') first if not installed
library(gWidgetsRGtk2)
options(guiToolkit = "RGtk2")

g = glayout(container = gwindow("alpha-convex hull demo"))
g[1, 1:2, expand = TRUE] = ggraphics(container = g)
g[2, 1] = "alpha"
g[2, 2, expand = TRUE] = gslider(from = 0, to = 0.3, value = 0.2, by = 0.01,
    container = g, handler = function(h, ...) {
        par(mar = rep(0, 4), xaxt = "n", yaxt = "n")
        plot(ahull(x, alpha = svalue(h$obj)), pch = 20, ann = FALSE)
    })

Related Posts

To leave a comment for the author, please follow the link and comment on his blog: Statistics, R, Graphics and Fun » R Language.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Tags: , , , , , , ,

Comments are closed.