<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/the-sierpinski-triangle-visualising-infinity-in-r/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://r.prevos.net/sierpinski-triangle/"> The Devil is in the Data</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p lang="en"><a rel="nofollow" href="http://r.prevos.net/?attachment_id=682" target="_blank"><img data-orig-file="https://i0.wp.com/r.prevos.net/wp-content/uploads/sites/11/2017/05/sierpinski.gif?resize=296%2C258" data-orig-size="346,300" data-medium-file="http://r.prevos.net/wp-content/uploads/sites/11/2017/05/sierpinski-300x260.gif" data-attachment-id="682" data-image-meta="{" data-image-description="" src="https://i0.wp.com/r.prevos.net/wp-content/uploads/sites/11/2017/05/sierpinski.gif?resize=296%2C258" data-permalink="http://r.prevos.net/sierpinski-triangle/sierpinski/" data-large-file="https://i0.wp.com/r.prevos.net/wp-content/uploads/sites/11/2017/05/sierpinski.gif?resize=296%2C258" alt="Sierpinski triangle" data-image-title="sierpinski" data-comments-opened="1" class="wp-image-682" data-recalc-dims="1"></a>Wacław Sierpiński was a mathematical genius who developed several of the earliest fractals. The Sierpiński triangle is an easy to conceptualise geometrical figure but it hides a fascinating mathematical complexity. Start by drawing an equilateral triangle and draw another one in its centre. Then draw equilateral triangles in the four resulting triangles, and so on, <em>ad infinitum</em>.</p>
<p lang="en">The original Sierpinski triangle will eventually disappear into <a rel="nofollow" href="http://mathworld.wolfram.com/CantorDust.html" target="_blank">Cantor dust</a>, a cloud of ever shrinking triangles of infinitesimal size. The triangle is self-similar, no matter how far you zoom in, the basic geometry remains the same.</p>
<p><iframe allowfullscreen="true" src="https://www.youtube.com/embed/wXBJfaZ2LvU?version=3&rel=1&fs=1&autohide=2&showsearch=0&showinfo=1&iv_load_policy=1&wmode=transparent" width="450" type="text/html"></iframe></p>
<h2>The Chaos Game</h2>
<p>A fascinating method to create a Sierpinski Triangle is a <a rel="nofollow" href="https://en.wikipedia.org/wiki/Chaos_game" target="_blank">chaos game</a>. This method uses random numbers and some simple arithmetic rules. Sierpinski Triangles can be created using the following six steps:</p>
<ol>
<li>Define three points in a plane to form a triangle.</li>
<li>Randomly select any point on the plane.</li>
<li>Randomly select any one of the three triangle points.</li>
<li>Move half the distance from your current position to the selected vertex.</li>
<li>Plot the current position.</li>
<li>Repeat from step 3.</li>
</ol>
<p>This fractal is an implementation of chaos theory as this random process attracts to a complex ordered geometry. The game only works with random numbers and when selecting random vertices of the triangle.</p>
<h2>Sierpinski Triangle Code</h2>
<p>This code implements the six rules in R. The code first initializes the triangle, defines a random starting point and then runs a loop to place random dots. The R plot engine does not draw pixels but uses characters, which implies that the diagram is not as accurate as it could be but the general principle is clear. The x(11) and Sys.sleep() commands are used to plot during the for-loop.</p>
<pre>
# Sierpinsky Triangle

# Initialise triangle
p &lt;- c(0, 500, 1000)
q &lt;- c(0, 1000, 0)
x11()
par(mar = rep(0, 4))
plot(p, q, col= &quot;red&quot;, pch = 15, cex = 1, axes = FALSE)

# Random starting point
x &lt;- sample(0:1000, 1)
y &lt;- sample(0:1000, 1)

# Chaos game
for (i in 1:10000) {
    Sys.sleep(.001)
    n &lt;- sample(1:3, 1)
    x &lt;- floor(x + (p[n] - x) / 2)
    y &lt;- floor(y + (q[n] - y) / 2)
    points(x, y, pch = 15, cex = 0.5)
}
</pre>
<p>This algorithm demonstrates how a seemingly chaotic process can result in order. Many other versions of <a rel="nofollow" href="https://en.wikipedia.org/wiki/Chaos_game" target="_blank">chaos games</a> exist, which I leave to the reader to play with. If you create your own versions then please share the code in the comment box below.</p>
<p>The post <a rel="nofollow" href="http://r.prevos.net/sierpinski-triangle/" target="_blank">The Sierpinski Triangle: Visualising infinity in R</a> appeared first on <a rel="nofollow" href="http://r.prevos.net/" target="_blank">The Devil is in the Data</a>.</p>
<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://r.prevos.net/sierpinski-triangle/"> The Devil is in the Data</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/p75PCv7-_Ug" height="1"></a></div>