The other day I saw a cool video on how to generate a Sierpinski triangle. For you who have never heard of such a thing it is like a fractal tri-force from the Zelda games (wiki).

The main idea for how to numerically generate the Sierpinski follows this simple algorithm:

``````1. Pick a random point, p (trace.point in the R-code), on the 2D plane
2. Specify 3 corner points that forms a triangle
3. For n iterations
a. randomly chose one corner
b. move p half way towards the randomly chosen corner
c. save p's new position
d. plot p``````
Since I work mostly with R this seemed like a fun toy problem to code up and see if this algorithm actually generates a proper Sierpinski triangle. Below follows the few lines of code required to produce and plot the triangle in R. I chose to set the number of iterations to 10000, and specified the corners of an equilateral triangle with one corner in origo.
``````# generate sierpinski triangle
iterations = 10000
trace.point = runif(2)
corner.points = data.frame(x=c(0, 1, 0.5),
> > y=c(0, 0, 1))
new.points = data.frame(x=rep(0,iterations),
> > y=rep(0,iterations))
for(i in 1:iterations){
trace.point = (corner.points[sample(3,1),]+trace.point)/2
new.points[i,] = trace.point
}``````

The generated sierpinski triangle is then plotted like this:

``````#plot sierpinski
plot.new()
points(corner.points\$x,corner.points\$y, col="red",pch=20)
points(new.points\$x,new.points\$y, col="black",pch=20, cex =0.2)``````

I find it really amazing that so few lines of code can produce such a complex pattern. This needs some further investigation! The next obvious step is to extend the same idea to the third dimension. The methodology is exactly the same, but this time p is no longer on the 2D plane, but rather in 3D space. The equilateral triangle is replaced with an equilateral pyramid. Compare the 3D code below with the 2D counterpart above to see how tiny the differance is.

``````# generate 3D sierpinski
iterations = 10000
trace.point   = runif(3)
corner.points = data.frame(x =c(  0,  1, 0.5, 0.5),
y =c(  0,  0,   1, 0.5),
z =c(  0,  0,   0,   1))
new.points    = data.frame(x =rep(0,iterations),
y =rep(0,iterations),
z =rep(0,iterations))
for(i in 1:iterations){
trace.point    = (corner.points[sample(4,1),]+trace.point)/2
new.points[i,] = trace.point
}``````

In my job as data analyst it is really important to provide compelling visualizations for customers to appreachiate complex data. This is why I extended my little experiment to the third dimension. I am trying to learn more about an R-package called Plotly, the nice thing about Plotly is that it offers lots of cool posibilities for visualizations, both in 2D and 3D. Plotly is supported by Power Bi’s R-integration and can also provide interactive plots that the user can play around with, try it out down below and have a look at the code.

``````# plotting 3D sierpinski
library(plotly)
p <- plot_ly(data = new.points,
x = ~x, y =  ~y, z = ~z, mode = "markers",
p``````

I find it really amazing that so few lines of code can produce such a complex pattern. This needs some further investigation! The next obvious step is to extend the same idea to the third dimension. The methodology is exactly the same, but this time p is no longer on the 2D plane, but rather in 3D space. The equilateral triangle is replaced with an equilateral pyramid. Compare the 3D code below with the 2D counterpart above to see how tiny the differance is.

``````# generate 3D sierpinski
iterations = 10000
trace.point   = runif(3)
corner.points = data.frame(x =c(  0,  1, 0.5, 0.5),
y =c(  0,  0,   1, 0.5),
z =c(  0,  0,   0,   1))
new.points    = data.frame(x =rep(0,iterations),
y =rep(0,iterations),
z =rep(0,iterations))
for(i in 1:iterations){
trace.point    = (corner.points[sample(4,1),]+trace.point)/2
new.points[i,] = trace.point
}``````

In my job as data analyst it is really important to provide compelling visualizations for customers to appreachiate complex data. This is why I extended my little experiment to the third dimension. I am trying to learn more about an R-package called Plotly, the nice thing about Plotly is that it offers lots of cool posibilities for visualizations, both in 2D and 3D. Plotly is supported by Power Bi’s R-integration and can also provide interactive plots that the user can play around with, try it out down below and have a look at the code.

``````# plotting 3D sierpinski
library(plotly)
p <- plot_ly(data = new.points,
x = ~x, y =  ~y, z = ~z, mode = "markers",
p`````` I find it really amazing that so few lines of code can produce such a complex pattern. This needs some further investigation! The next obvious step is to extend the same idea to the third dimension. The methodology is exactly the same, but this time p is no longer on the 2D plane, but rather in 3D space. The equilateral triangle is replaced with an equilateral pyramid. Compare the 3D code below with the 2D counterpart above to see how tiny the differance is.

``````# generate 3D sierpinski
iterations = 10000
trace.point   = runif(3)
corner.points = data.frame(x =c(  0,  1, 0.5, 0.5),
y =c(  0,  0,   1, 0.5),
z =c(  0,  0,   0,   1))
new.points    = data.frame(x =rep(0,iterations),
y =rep(0,iterations),
z =rep(0,iterations))
for(i in 1:iterations){
trace.point    = (corner.points[sample(4,1),]+trace.point)/2
new.points[i,] = trace.point
}``````

In my job as data analyst it is really important to provide compelling visualizations for customers to appreachiate complex data. This is why I extended my little experiment to the third dimension. I am trying to learn more about an R-package called Plotly, the nice thing about Plotly is that it offers lots of cool posibilities for visualizations, both in 2D and 3D. Plotly is supported by Power Bi’s R-integration and can also provide interactive plots that the user can play around with, try it out down below and have a look at the code.

``````# plotting 3D sierpinski
library(plotly)
p <- plot_ly(data = new.points,
x = ~x, y =  ~y, z = ~z, mode = "markers",