[This article was first published on

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

**exploRations in R**, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

### Intro

This function allows us to generate the Sierpinski Triangle and explore other recursive shapes with equal length sides following the same algorithm.

```
shape <- function(corners, trials = 100000){
corners <- as.integer(corners)
points <- list()
if (corners < 3) stop("Value should be 3 or greater")
for (n in 1:(corners)){
points$x[n] <- 0 + cos((2*pi*n)/corners)
points$y[n] <- 0 + sin((2*pi*n)/corners)
}
x <- points$x[1]
y <- points$y[1]
trials <- trials
sierpinski <- list()
for (t in 1:trials){
r <- sample(1:corners,1)
x <- (x + points$x[r]) / sqrt(corners + 1)
y <- (y + points$y[r]) / sqrt(corners + 1)
sierpinski$x[t] <- x
sierpinski$y[t] <- y
}
# I use these colors for random color selection. Update for your own desired selection.
color <- sample(c("royalblue2", "firebrick2", "gold2", "springgreen3", "purple2", "darkorange1"),1)
plot(sierpinski$x[corners:trials], sierpinski$y[corners:trials],
xlab = paste0(corners, " Sides Chosen"), ylab = "", xaxt = "n", yaxt = "n", col = color)
}
```

When you run the function, you indicate the number of sides for the polygon and adjust the number of trials to change the resolution if desired. Here are some examples:

`shape(3)`

`shape(4)`

`shape(6)`

To

**leave a comment**for the author, please follow the link and comment on their blog:**exploRations in R**.R-bloggers.com offers

**daily e-mail updates**about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.