Skip to content

Instantly share code, notes, and snippets.

@mdsumner
Created March 1, 2016 14:41
Show Gist options
  • Save mdsumner/52f8e6a674590e147ca7 to your computer and use it in GitHub Desktop.
Save mdsumner/52f8e6a674590e147ca7 to your computer and use it in GitHub Desktop.
# centroids
# still need Inner Centroid
v <- cbind(c(0.3, 0.6, 0.4), c(0.2, 0, 0.8))
tri <- c(1, 2, 3)
# centroid (weight)
cw <- matrix(apply(v[tri, ], 2, mean), ncol = 2)
library(tripack)
## minimum enclosing circle centre
mec0 <- circumcircle(v[,1], v[,2])
cc <- matrix(mec0[c("x", "y")], ncol = 2)
## box centroid
cb <- matrix(apply(expand.grid(x = range(v[,1]), y = range(v[,2])), 2, mean), ncol = 2)
plot(v)
polypath(v[tri, ])
points(cw)
text(cw, label = "Centroid (weight)", cex = 0.6, pos = 4)
points(cc)
text(cc, label = "Centroid", cex = 0.6, pos = 4)
points(cb)
text(cb, label = "Centroid (box)", cex = 0.6, pos = 4)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment