<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by November Williams on Medium]]></title>
        <description><![CDATA[Stories by November Williams on Medium]]></description>
        <link>https://medium.com/@nnwilliams8?source=rss-69b0bb7d6626------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*8NlhyqQOOu4A8uPT</url>
            <title>Stories by November Williams on Medium</title>
            <link>https://medium.com/@nnwilliams8?source=rss-69b0bb7d6626------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 01 Dec 2026 02:00:45 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@nnwilliams8/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[The Chaos Game in JavaScript]]></title>
            <link>https://medium.com/@nnwilliams8/the-chaos-game-in-javascript-69af056c6ec?source=rss-69b0bb7d6626------2</link>
            <guid isPermaLink="false">https://medium.com/p/69af056c6ec</guid>
            <category><![CDATA[mathematics]]></category>
            <category><![CDATA[fractals]]></category>
            <category><![CDATA[geometry]]></category>
            <category><![CDATA[math]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[November Williams]]></dc:creator>
            <pubDate>Mon, 13 Aug 2018 02:40:57 GMT</pubDate>
            <atom:updated>2018-08-13T02:40:57.412Z</atom:updated>
            <content:encoded><![CDATA[<p>A ‘Chaos Game’ is a one-player, mathematical game. As is typical of these sorts of mathematical things, it’s not really much of a ‘game.’ Let’s play it anyway.</p><p>The rules are simple. We’re going to be drawing some points on a plane, in relation to some starting points, or vertices. These can be any arbitrary points, but let’s go with the three vertices of an equilateral triangle.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/730/1*dMV0AVuEckpplTJ2R2As7A.png" /><figcaption>Our starting configuration.</figcaption></figure><p>To begin, we pick one of the vertices. This is our ‘current’ point. Then, we pick another vertex, at random, and draw a point halfway between our ‘current’ point and that vertex. This is our new ‘current’ point. We the repeat the process. To clarify: we’ll only ever be going halfway to one of our three original vertices, but our ‘current’ point will move all over the place.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/654/1*oUYm22mBcRE3b3s6DCkZaw.png" /><figcaption>We ‘moved’ to the halfway point between the rightmost two vertices.</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/729/1*oOOc8usHlmp15-UrhwsZRA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/754/1*1tdg8CsEOBidC0iRG9Ilig.png" /><figcaption>Then we ‘move’ halfway between that point and the rightmost vertex. And ten more times.</figcaption></figure><p>Not much of a game, right? It seems like all the points between our three vertices will eventually be landed on, filling in our ‘triangle.’ Furthermore, I’m far too lazy to keep drawing these points. Just in case something interesting might happen, let’s write a small program to draw an arbitrary number of points in this way for us.</p><p>We’ll be using JavaScript, and interacting with the HTML &lt;canvas&gt; element to draw our points. A &lt;canvas&gt; element accepts width and height attributes.</p><pre>&lt;canvas width=&quot;900&quot; height=&quot;900&quot; &gt;&lt;/canvas&gt;<br>&lt;script src=&quot;./ourChaosGame.js&quot;&gt;&lt;/script&gt;</pre><p>900 by 900 pixels seems adequately triangular. In our script file, we can access the canvas element and store a reference to it in a variable. I also think we should draw dots with the fillRect method, by just passing it a height and width of 1 pixel. Let’s make our dots red.</p><pre>let cx = document.querySelector(&#39;canvas&#39;).getContext(&#39;2d&#39;)</pre><pre>cx.fillStyle = &#39;red&#39;</pre><pre>const pixel = (x, y) =&gt; cx.fillRect(x, y, 1, 1)</pre><p>Now, pixel(x, y) will draw a single red pixel on the canvas at (x, y). It’s kind of tedious, not to mention ambiguous, to pass in an x and y value to this function in that order, though, so let’s represent points themselves as a little class, like Java’s Point2D or Point . We know we’ll be finding a lot of midpoints, so besides a constructor, we’ll need a midpoint method. It could be static, and take two instances of Point , or an instance method, and find the midpoint from an instance of Point to another point passed in as a parameter. Here’s a class with both implemented, one using the other, and a new pixel method which takes an instance of class Point :</p><pre>class Point {</pre><pre>  constructor(x, y) {<br>    this.x = x<br>    this.y = y<br>  }</pre><pre>  midpointTo(a) {<br>    return Point.midpoint(this, a)<br>  }</pre><pre>  static midpoint(a, b) {<br>    return new Point(<br>      Math.round((a.x + b.x) / 2),<br>      Math.round((a.y + b.y) / 2)<br>    )<br>  }</pre><pre>}</pre><pre>const pixel = point =&gt; cx.fillRect(point.x, point.y, 1, 1)</pre><p>Remember that the midpoint of two points is the point with x coordinate equal to half of the sum of the two points’ x coordinates, and y coordinate equal to half the sum of the two points’ y coordinates. Which makes sense.</p><p>As for the starting points, let’s use an equilateral triangle’s vertices, like before. On a 900 by 900 pixel &lt;canvas&gt; , some nice points might be:</p><pre>const A = new Point(450, 190),<br>  B = new Point(150, 710),<br>  C = new Point(750, 710)</pre><pre>const vertices = [A, B, C]</pre><p>We store them in an array so we can use Math.random to pick a random one to move to:</p><pre>const pickVertex = () =&gt;<br>  vertices[<br>    Math.floor(<br>      Math.random() * (vertices.length)<br>    )<br>  ]</pre><p>Of course, we know that there are 3 vertices, so we could just hard-code that value. I suspect, however, that we may eventually wish to allow ourselves to play this game with any number of vertices.</p><p>A function which takes a point, and returns a new point halfway between that point and a randomly chosen vertex:</p><pre>const getNewPoint = oldPoint =&gt; oldPoint.midpointTo(pickVertex())</pre><p>That’s all the setup we need! Our function should pick a vertex to start at, set our ‘current point,’ currentPoint, equal to that, draw a pixel there, then reassign currentPoint to the value of getNewPoint(currentPoint) and repeat. How many times do we want to repeat? Let’s try different values to see what happens, starting low. Our function:</p><pre>const playGame = iterations =&gt; {<br>  const startVertex = pickVertex()<br>  let currentPos = startVertex</pre><pre>  for (let i = 0; i &lt; iterations; i++) {<br>    currentPos = getNewPoint(currentPos)<br>    pixel(currentPos)<br>  }</pre><pre>}</pre><p>That’s all! If we just wanted to fill a shape, we could have used the fill() method on the canvas context object…</p><p>playGame(100) doesn’t output enough points to even be very well discernible from the dust on your screen.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/684/1*gG-JbYP8j-09pMIfcjou-Q.png" /><figcaption>playGame(1000)</figcaption></figure><p>playGame(1000) starts to be visible. And recognizable.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/667/1*RyxAC4Xwegy8bcXqXEDgDQ.png" /><figcaption>playGame(10000)</figcaption></figure><p>This doesn’t fill our shape at all, does it? I’ve tricked you into making some kind of fractal! In fact, this three-vertex chaos game constructs what look like <a href="https://en.wikipedia.org/wiki/Sierpinski_triangle">Sierpinski’s Triangle</a>, a very famous fractal.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/667/1*1l1h0ygA3gsT_JBOBWXBYg.png" /><figcaption>playgame(100000)</figcaption></figure><p>Questions:</p><ol><li>Why did a Sierpinksi’s triangle come out of our nice function that just plots halfway points?</li><li>How could such a complex structure arise out of such a simple set of rules?</li></ol><p>With regards to question 1, it may be because we never proved that our game must eventually fill all points within the given vertices. In general, if you prove something must happen, it will happen. Perhaps if we prove that we will eventually land on all points, our function will act accordingly.</p><p>After all, the points in the middle might start appearing only after a very large number of iterations.</p><p>Let’s try to see if we can get a point in that big empty space in the middle. I don’t care where exactly — it could be in the very center, or arbitrarily close to one of the ‘edges’ of the empty space.</p><p>We know that for a point to be plotted, it must have been the halfway point between another point and one of our three vertices. So what point must have already existed for our point-in-the-middle to exist ? A point that is the endpoint of a line with a midpoint of our point and an opposite endpoint of one of the three vertices… Well, let’s draw it:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/667/1*B1eBbofuGOuGqf3MHGlGnw.jpeg" /><figcaption>A point in the middle (the intersection of the two lines) would require an already existing point outside of the figure (the lower endpoint of the long line) if it were ever to be plotted itself.</figcaption></figure><p>Essentially, any point in the middle would require that there be a point outside of the boundary of the figure to exist. The ‘edges’ of the empty space are really the sets of all points halfway between a vertex and its opposite edge:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/667/1*E5lmpLLC4PCYRYvE9TCx5g.jpeg" /><figcaption>The dark blue line intersects all the midpoints of the light blue lines, which all begin at one vertex and end at its opposite side (black).</figcaption></figure><p>It seems we’ve severely underestimated Sierpinski’s triangle’s connection to <em>halfway points</em>.</p><p>Back to our original desire, a point in the middle. Sure, it requires a point outside of the original figure, but we can try to get one of those, too. In fact, you might have thought that the chaos game would just make points anywhere on the plane, mostly disregarding the vertices, when you originally read the rules. It doesn’t seem implausible.</p><p>But it is impossible. Any point outside the region bounded by our vertices would require a point twice as far away, and that point would require a point twice as far away as that one, and so on.</p><p>So it is impossible for any points to be in the middle. That explains that. We’ve sketched a proof that the conditions we desired will <em>never</em> arise.</p><p>Will all points on an edge of the original triangle eventually be plotted?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/396/1*eIP8iUnfqgev_gvJN4qfUg.png" /><figcaption>playGame(100000) where our array of vertices only contains A and B</figcaption></figure><p>It sure <em>looks</em> like it. Figuring out for sure might be beyond the scope of this article. Suffice to say that <em>many </em>points on a given edge will be plotted. I would hesitate to say ‘most,’ due to the nature of continuous things, like lines. There are countably infinitely many <a href="https://en.wikipedia.org/wiki/Algebraic_number">algebraic numbers</a>, like 0, 1, √2, and φ — nearly all the numbers anyone can think of are algebraic — but they take up a negligible portion of the real line (or complex plane, for that matter). Ostensibly, our number of steps is countable, and so a compelling argument could be made for the mostly-emptiness of any edge of our figure.</p><p>It’s evident that all the pixels on any edge will eventually be colored, though. a 900 by 900 pixel world makes certain things much simpler.</p><p>The game is meant to be played infinitely — in a ‘true’ Chaos Game, if a point existed, a midpoint between it and one of the vertices would also exist. Luckily, we start with three points. Unluckily, we can’t play for nearly that long. So, if a point exists, the midpoint between it and one of the figure’s vertices probably exists.</p><p>We observe that all the edges of our figure get pretty much colored in. Taking that as given, and knowing that if a point exists, a midpoint should exist, we can see that those ‘halfway-lines’ we noticed earlier will appear, since those lines essentially consist of the set of all midpoints between an edge and its opposite vertex.</p><p>Even moreso, we can see that the midpoints of THOSE lines and the vertices they are closest to will appear, by the same phenomenon. And so on, and we have a whole pile of these lines. But what about the other two? They don’t have vertices opposite them, like all three lines did in our original figure.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/658/1*KKnlrfeZP_QGUfaeEUb2Bg.jpeg" /><figcaption>Lines we think will appear, so far.</figcaption></figure><p>To figure out the two extra edges we haven’t accounted for, let’s just use the same reasoning as before. If a line exists, the set of all midpoints between it and one of the vertices (more like all of them, right?) should exist. Let’s check the midpoints of the middle edges and the vertices that AREN’T opposite them.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/658/1*J8Ki_6dv9bTduGGO49otqA.png" /><figcaption>The green line intersects the midpoints of all the blue lines.</figcaption></figure><p>There’s the line we were looking for! You can see how the argument could continue, recursively down and down. Continue finding midpoint-lines like this forever, or formalize this argument, and you’ll have shown that this fractal appears.</p><p>So with regards to question 2 — it might be said that this isn’t a complex structure at all. Sierpinski’s triangle might be just as simple as the concept of a midpoint, and even more fundamental — you might say that finding one midpoint wasn’t going far enough. But really, mathematics is ABOUT complex structures arising from simple rules. If you have the number 1 (a very special number) and you can add it, you get all the natural numbers, including the fantastical structure of the primes. If you have the empty set and a concept of size, you can make <a href="https://en.wikipedia.org/wiki/Surreal_number">nearly everything</a>.</p><p>The most mathematical thing to do at this point is to ask “what else can we make with these rules?” After all, we only tried it with three vertices, and halfway-jumps.</p><p><a href="https://nvmbrw.github.io/interactive-chaos-game/">Here’s an interactive version for you to play with</a>. ‘R-value’ is the amount we’re jumping towards a vertex — .5 is what we started with, but what about 1/3 of the way (.333) or 1/4 (.25)? Why does 4 vertices and .5 r just produce noise? What happens at negative r? And can someone explain what happens at r = .995 ?</p><p>These are all mathematical questions. You’d have to do math to figure out the answers. But you wouldn’t be working with numbers or writing down equations. You’d just be reasoning and drawing patterns. And, I hope, enjoying yourself.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=69af056c6ec" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>