<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>algorithm on smarx.com</title>
    <link>https://smarx.com/tags/algorithm/</link>
    <description>Recent content on smarx.com</description>
    <language>en-us</language>
    <managingEditor>smarx@smarx.com (Steve Marx)</managingEditor>
    <webMaster>smarx@smarx.com (Steve Marx)</webMaster>
    <lastBuildDate>Sun, 09 Aug 2020 00:00:00 +0000</lastBuildDate>
    
        <atom:link href="https://smarx.com/tags/algorithm/index.xml" rel="self" type="application/rss+xml" />
    
    
    <item>
      <title>Chaikin Curves: a Beautifully Simple Algorithm</title>
      <link>https://smarx.com/posts/2020/08/chaikin-curves-a-beautifully-simple-algorithm/</link>
      <pubDate>Sun, 09 Aug 2020 00:00:00 +0000</pubDate>
      <author>smarx@smarx.com (Steve Marx)</author>
      <guid>https://smarx.com/posts/2020/08/chaikin-curves-a-beautifully-simple-algorithm/</guid>
      <description>&lt;p&gt;Chaikin&amp;rsquo;s corner cutting algorithm is an astonishingly simple way to turn a jagged line into a smooth curve.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;It&#39;s extremely effective, and it will take you like ten minutes to write.&lt;/p&gt;
&lt;cite&gt;&amp;ndash; Benjamin Kovach, &lt;a href=&#34;https://www.youtube.com/watch?v=kZNTozzsNqk&#34;&gt;A Box of Chaos: The Generative Artist&#39;s Toolkit&lt;/a&gt;&lt;/cite&gt;
&lt;/blockquote&gt;
&lt;h2 id=&#34;a-demo&#34;&gt;A demo&lt;/h2&gt;
&lt;p&gt;The gray line below is the original, randomly generated path. The red line is the path smoothed with Chaikin&amp;rsquo;s algorithm. Drag the slider to change the number of smoothing iterations. You can also drag the points around to form a different path.&lt;/p&gt;
&lt;p&gt;&lt;canvas id=&#34;chaikin-canvas&#34; class=&#34;border w-full&#34; style=&#34;touch-action: none&#34;&gt;&lt;/canvas&gt;&lt;/p&gt;
&lt;div class=&#34;uppercase font-bold text-center tracking-wide text-gray-400&#34;&gt;
&lt;input type=&#34;range&#34; min=&#34;0&#34; max=&#34;5&#34; value=&#34;5&#34; class=&#34;block mx-auto outline-none w-1/2&#34; id=&#34;chaikin-range&#34;&gt;
&lt;span id=&#34;chaikin-iterations&#34; class=&#34;text-gray-700&#34;&gt;5&lt;/span&gt; iterations
&lt;/div&gt;
&lt;h2 id=&#34;the-algorithm&#34;&gt;The algorithm&lt;/h2&gt;
&lt;p&gt;The algorithm is quite simple. It can be intuitively understood as iteratively &amp;ldquo;cutting off&amp;rdquo; the sharp corners in the path:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;For each pair of neighboring points A and B in the path:
&lt;ul&gt;
&lt;li&gt;Find the point C, 25% of the way between A and B.&lt;/li&gt;
&lt;li&gt;Find the point D, 75% of the way between A and B.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;All the C and D points form a new, smoother path.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Then you just iterate as needed. The following animation shows two iterations of the algorithm visually:&lt;/p&gt;
&lt;p&gt;&lt;canvas id=&#34;chaikin-explainer&#34; class=&#34;border sm:w-1/2 mx-auto w-full&#34;&gt;&lt;/canvas&gt;&lt;/p&gt;
&lt;h2 id=&#34;javascript-implementation&#34;&gt;JavaScript implementation&lt;/h2&gt;
&lt;p&gt;Below is the JavaScript implementation I wrote for the demos on this page:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-javascript&#34; data-lang=&#34;javascript&#34;&gt;&lt;span class=&#34;kd&#34;&gt;function&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;chaikin&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;points&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;
    &lt;span class=&#34;kd&#34;&gt;var&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;newPoints&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;[];&lt;/span&gt;

    &lt;span class=&#34;k&#34;&gt;for&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;kd&#34;&gt;var&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;i&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;0&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;;&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;i&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;points&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;length&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;-&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;;&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;i&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;++&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;
        &lt;span class=&#34;kr&#34;&gt;const&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;a&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;points&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;i&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;];&lt;/span&gt;
        &lt;span class=&#34;kr&#34;&gt;const&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;b&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;points&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;i&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;+&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;];&lt;/span&gt;

        &lt;span class=&#34;c1&#34;&gt;// a*0.75 + b*0.25
&lt;/span&gt;&lt;span class=&#34;c1&#34;&gt;&lt;/span&gt;        &lt;span class=&#34;nx&#34;&gt;newPoints&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;push&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;a&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;mul&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;mf&#34;&gt;0.75&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;).&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;add&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;b&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;mul&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;mf&#34;&gt;0.25&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)));&lt;/span&gt;

        &lt;span class=&#34;c1&#34;&gt;// a*0.25 + b*0.75
&lt;/span&gt;&lt;span class=&#34;c1&#34;&gt;&lt;/span&gt;        &lt;span class=&#34;nx&#34;&gt;newPoints&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;push&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;a&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;mul&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;mf&#34;&gt;0.25&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;).&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;add&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;b&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;mul&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;mf&#34;&gt;0.75&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)));&lt;/span&gt;
    &lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;

    &lt;span class=&#34;k&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;newPoints&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;;&lt;/span&gt;
&lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id=&#34;making-pretty-pictures&#34;&gt;Making pretty pictures&lt;/h2&gt;
&lt;p&gt;A few months ago, I made some generative art using &lt;a href=&#34;https://flutter.dev&#34;&gt;Flutter&lt;/a&gt;. I was particularly pleased with the result below, which uses Chaikin&amp;rsquo;s algorithm to draw the smooth curves.&lt;/p&gt;





    




    


&lt;figure class=&#34;flex flex-col items-center&#34;&gt;
    
    &lt;a href=&#34;https://smarx.com/posts/2020/08/chaikin-curves-a-beautifully-simple-algorithm/curve-art-featured-cover.png&#34; data-lightbox=&#34;19722df018a539f277cb9d2ff30fcccc&#34; &gt;
    
    &lt;img class=&#34;mx-auto&#34; src=&#34;https://smarx.com/posts/2020/08/chaikin-curves-a-beautifully-simple-algorithm/curve-art-featured-cover_hu818521f8e945d4c67cc8b51b11f80d98_421837_728x0_resize_lanczos_2.png&#34; width=&#34;728&#34; height=&#34;669&#34;&gt;
    
    &lt;/a&gt;
    
    
&lt;/figure&gt;

&lt;script src=&#34;https://smarx.com/posts/2020/08/chaikin-curves-a-beautifully-simple-algorithm/chaikin.js&#34;&gt;&lt;/script&gt;</description>
    </item>
    
  </channel>
</rss>
