## Convex Hull of Polygon using Boost.Geometry

Sameer D'Costa — written Feb 23, 2014 — source

Rcpp can be used to convert basic R data types to and from Boost.Geometry models.

In this example, we take a matrix of 2d-points and convert it into a Boost.Geometry polygon. We then compute the convex hull of this polygon using the Boost.Geometry function `boost::geometry::convex_hull()`. The convex hull is then converted back to an R matrix.

The conversions to and from R and Boost.Geometry types are are done using two custom `as()` and `wrap()` convenience converter functions which are implemented below, followed by a function using them in order to take data from R, call a Boost.Geometry function, and return the result to R.

Now we can use R to replicate the `convex_hull` example in the Boost.Geometry documentation.

```      [,1] [,2]
[1,]  2.0  1.3
[2,]  2.4  1.7
[3,]  2.8  1.8
[4,]  3.4  1.2
[5,]  3.7  1.6
[6,]  3.4  2.0
[7,]  4.1  3.0
[8,]  5.3  2.6
[9,]  5.4  1.2
[10,]  4.9  0.8
[11,]  2.9  0.7
[12,]  2.0  1.3
```
```     [,1] [,2]
[1,]  2.0  1.3
[2,]  2.4  1.7
[3,]  4.1  3.0
[4,]  5.3  2.6
[5,]  5.4  1.2
[6,]  4.9  0.8
[7,]  2.9  0.7
[8,]  2.0  1.3
```

In fact, because of the interplay because providing `as<>()` and `wrap()` for the compiler, and how `compileAttributes()` works, we can write the function more cleanly with the desired new types in the interface.

We can run this second function as well:

```     [,1] [,2]
[1,]  2.0  1.3
[2,]  2.4  1.7
[3,]  4.1  3.0
[4,]  5.3  2.6
[5,]  5.4  1.2
[6,]  4.9  0.8
[7,]  2.9  0.7
[8,]  2.0  1.3
```

tags: boost