<?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 Kevin Pluck on Medium]]></title>
        <description><![CDATA[Stories by Kevin Pluck on Medium]]></description>
        <link>https://medium.com/@kevpluck?source=rss-a70fd7c25dd8------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*qbCZ0XkLFA7EjwBt.jpg</url>
            <title>Stories by Kevin Pluck on Medium</title>
            <link>https://medium.com/@kevpluck?source=rss-a70fd7c25dd8------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 11:06:50 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@kevpluck/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[New project]]></title>
            <link>https://medium.com/@kevpluck/new-project-5325903591c2?source=rss-a70fd7c25dd8------2</link>
            <guid isPermaLink="false">https://medium.com/p/5325903591c2</guid>
            <dc:creator><![CDATA[Kevin Pluck]]></dc:creator>
            <pubDate>Sun, 11 Jun 2017 22:19:06 GMT</pubDate>
            <atom:updated>2017-06-11T22:19:06.686Z</atom:updated>
            <content:encoded><![CDATA[<p>Apologies for not posting as regularly as I have been! I’ve been quite busy with a new project.</p><p>I’ve been learning how to use <a href="http://blender.org/">Blender</a> and have made this video:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FfnKYWeQh2a8%3Ffeature%3Doembed&amp;url=http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DfnKYWeQh2a8&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FfnKYWeQh2a8%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/04757f80f14df7e8aeff80163509b198/href">https://medium.com/media/04757f80f14df7e8aeff80163509b198/href</a></iframe><p>I may post on how I made this but as there was no programming involved as it’s all point-and-click, it’s not that easy to document the process. Not only that there are hundreds of video tutorials and blogs that detail the nitty-gritty of Blender.</p><p>I hope you find the video informative!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5325903591c2" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Putting it out there]]></title>
            <link>https://medium.com/hackernoon/putting-it-out-there-f830d3846a34?source=rss-a70fd7c25dd8------2</link>
            <guid isPermaLink="false">https://medium.com/p/f830d3846a34</guid>
            <category><![CDATA[climate-change]]></category>
            <category><![CDATA[global-warming]]></category>
            <category><![CDATA[tutorial]]></category>
            <category><![CDATA[animation]]></category>
            <dc:creator><![CDATA[Kevin Pluck]]></dc:creator>
            <pubDate>Sun, 28 May 2017 20:09:32 GMT</pubDate>
            <atom:updated>2019-04-30T09:42:11.910Z</atom:updated>
            <content:encoded><![CDATA[<p>This is part 11 of an N part series detailing how I make <a href="https://medium.com/@kevpluck/spirals-and-barrels-364d1dd78175">my animations</a>.</p><p><a href="https://hackernoon.com/more-polish-59b1a13480a0">Prev</a> <a href="https://medium.com/@kevpluck/patience-now-25d7e59a8c0a">Next</a></p><p>So you’ve made a <a href="https://hackernoon.com/tagged/data-visualisation">data visualisation</a> but how do you show it off?</p><p>To do that we need to output the <a href="https://hackernoon.com/tagged/animation">animation</a> to some sort of video file and then find some site willing to host it.</p><p>I found Processing’s default video creation somewhat underwhelming so I used a slightly more tricky but far superior technique. You will need to download two things, a video creation tool called <em>ffmpeg</em> and a library that plugs into <em>Processing</em> which uses that called <em>Video Export</em>.</p><p>First download <em>ffmpeg </em>from f<a href="https://ffmpeg.org/">fmpeg.org</a>. I use windows so merely unzip it somewhere (I have a folder called c:\tools\ for such things where I unzipped it and renamed the folder to ffmpeg. Put c:\tools\ffmpeg\bin into the user path — I use <a href="http://eveditor.com/">eveditor.com</a> for this — and you’re good to go).</p><p>Now install <em>Video Export</em> by running <em>Processing</em> and open the <em>Contribution Manager</em> by going to the Tools/Add Tool... menu. Select the <em>Libraries </em>tab and search for <em>Video Export</em>. Click install and you’re done. There’s more info on <a href="https://www.funprogramming.org/VideoExport-for-Processing/">funprogramming.org</a>.</p><p>To use <em>Video Export</em> you first need to import it into your sketch so add these lines to the top of the file:</p><pre>import com.hamoid.*;<br>VideoExport videoExport;</pre><p>hamoid is the github user id of the developer (check out his <a href="https://github.com/hamoid">github account</a>) so I guess he named all the libraries with that as a prefix. The second line defines a variable called videoExport of the type VideoExport. Looks confusing until you get used to it.</p><p>Next thing we need to do is tell <em>Video Export</em> about our sketch so in setup() add the following:</p><pre>videoExport = new VideoExport(this);<br>videoExport.setFrameRate(30);<br>videoExport.startMovie();</pre><p>new VideoExport(this); creates an instance of the type VideoExport providing the current sketch denoted by this so that it can access the frames.</p><p>We are now ready to save individual frames to the video so at the end of draw() add:</p><pre>videoExport.saveFrame();</pre><p>It is important to execute videoExport.endMovie() when the animation finishes as the video file may get corrupted, if the video doesn’t play then check that this command is executed.</p><p>This creates a video file called processing-movie.mp4 in your sketch folder which you can play in your favorite video player.</p><p>On your first run of a sketch using <em>Video Export</em> a window will show asking for where you installed <em>ffmpeg.</em></p><p>Check the end of the post to see the full source code of the Keeling animation including the <em>Video Export</em> commands.</p><p>So now how do you create a gif? By using <em>ffmpeg</em> of course!</p><p>You can simply open a dos prompt¹ or terminal and execute:</p><pre>ffmpeg -i processing-movie.mp4 processing-movie.gif</pre><p>This will work most of the time but if you create a video with more than 256 colours you’ll find the quality drops considerably in which case things get a tad more tricky. In this case you need to execute two <em>ffmpeg</em> commands. First is to find out all the colours used in your video and then create the gif using that palette by using these two commands:</p><pre>ffmpeg -i processing-movie.mp4 -vf &quot;palettegen&quot; -y palette.png<br>ffmpeg -i processing-movie.mp4 -i palette.png -lavfi &quot;fps=50,paletteuse&quot; -y out.gif</pre><p>For the full gory details read <a href="http://blog.pkh.me/p/21-high-quality-gif-with-ffmpeg.html">pkh’s blog</a>. I turned the above into a batch script file called makegif.bat:</p><pre>ffmpeg -i %1 -vf &quot;palettegen&quot; -y palette.png<br>ffmpeg -i %1 -i palette.png -lavfi &quot;fps=50,paletteuse&quot; -y out.gif</pre><p>Simply executed by makegif processing-movie.mp4 and it creates a gif called out.gif.</p><p>Now we have an mp4 and a gif file! What do we do with it? We need to host it somewhere so it can be shared. So here are your options:</p><p><strong>Twitter</strong>: Simply attach your mp4 file to a tweet. If it’s longer than a certain amount a trimming dialog appears but if you move the start and stop indicators to the beginning and end of the video twitter will happily host it.</p><p><strong>YouTube</strong>: Upload and go, can add music from YouTube’s free music library, which is nice.</p><p><strong>FaceBook:</strong> Simply attach your mp4 to a post.</p><p><a href="http://imgur.com/"><strong>imgur.com</strong></a><strong>:</strong> (Pronounced “imager” apparently) is a perfect place to host gifs so you can then post to sites like <a href="http://reddit.com/">reddit.com</a>², add the link to emails or perhaps write stories on some site called medium.com. I find that sharing with the imgur community is highly worthwhile too. When hosting there you can either link to the gif directly or to a faster loading gifv simply by adding a v to the end of the URL.</p><p>Here’s the complete source code of the Keeling animation using <em>Video Export</em>:</p><pre>import com.hamoid.*;<br>VideoExport videoExport;</pre><pre>import java.time.*;<br>import de.looksgood.ani.*;<br>Ani _ani;</pre><pre>FloatDict _data = new FloatDict();<br>LocalDate _startDate = LocalDate.of(1958, 3, 29);</pre><pre>void setup()<br>{<br>  loadData();<br>  size(500,500);<br>  background(0);<br>  stroke(255,255,0);<br>  textAlign(CENTER);<br>  <br>  Ani.init(this);<br>  Ani.setDefaultTimeMode(Ani.FRAMES);<br>  _ani = new Ani(this, 530, &quot;_change&quot;, 6, Ani.EXPO_IN);<br>  <br>  videoExport = new VideoExport(this);<br>  videoExport.setFrameRate(30);<br>  videoExport.startMovie();<br>}</pre><pre>float yScale = 20.0;</pre><pre>boolean _coda = false;<br>int _codaCount = 0;</pre><pre>float _change = 1.0;<br>final int MARGIN = 60;<br>final float CO2MIN = 313.04;</pre><pre>void draw()<br>{<br>  if(_change &gt;= 6.0) <br>  {<br>    if(!_coda)<br>    {<br>      _coda = true;<br>      _codaCount = frameCount;<br>    }<br>    if(frameCount - _codaCount &gt; 120) <br>    {<br>      videoExport.endMovie();<br>      exit();<br>    }<br>    videoExport.saveFrame();<br>    return;<br>  }<br>  <br>  background(0);<br>  strokeWeight(1);<br>  <br>  line(MARGIN, 0, MARGIN, height);<br>  line(0, height - MARGIN, width, height - MARGIN);<br>  <br>  drawGraph(MARGIN, height - MARGIN, width - MARGIN, height - MARGIN);<br>  videoExport.saveFrame();<br>}</pre><pre>void drawGraph(int xPos, int yPos, int graphWidth, int graphHeight)<br>{<br>  text(&quot;Atmospheric CO₂ concentration 1958 - 2017\nR. F. Keeling, S. J. Walker, S. C. Piper and A. F. Bollenbacher\nMauna Loa, Observatory, Hawaii&quot;, MARGIN + graphWidth / 2.0, MARGIN);<br>  int deltaX = int(frameCount * _change);<br>    <br>  float xScale = 1.0;<br>  <br>  int xAxisMaximum = graphWidth;<br>  <br>  <br>  if(deltaX &gt; graphWidth)<br>  {<br>    xAxisMaximum = deltaX;<br>    xScale = float(graphWidth) / float(deltaX);<br>  }<br>  <br>  text(&quot;Year&quot;, MARGIN + graphWidth / 2, height - 20);<br>  drawXAxis(xAxisMaximum, xScale);<br>  <br>  strokeWeight(3);<br>  <br>  float co2Max = 0.0;<br>  <br>  for(int dataIndex = 1; dataIndex &lt;= deltaX; dataIndex++)<br>  {<br>    <br>    int daysFromStart = (dataIndex - 1) * 7;<br>    LocalDate frameDate = _startDate.plusDays(daysFromStart);<br>       <br>    <br>    if(_data.hasKey(frameDate.toString()))<br>    {<br>      float co2 = _data.get(frameDate.toString());<br>      <br>      if(co2 &gt; co2Max) co2Max = co2;<br>            <br>      float x = dataIndex;<br>      float y = (co2 - CO2MIN);<br>      <br>      if(y * yScale &gt; graphHeight) <br>      {<br>        yScale = float(graphHeight)/y;<br>      }<br>      <br>      point(xPos + x * xScale, yPos - (y * yScale));<br>    }<br>  }<br>  <br>  if((co2Max - CO2MIN) * yScale &lt; graphHeight) <br>    co2Max = graphHeight / yScale + CO2MIN;<br>  <br>  pushMatrix();<br>    translate(0, graphWidth / 2);<br>    rotate(-PI / 2.0);<br>    text(&quot;CO₂ concentration (PPM)&quot;, 0, 20);<br>  popMatrix();<br>  <br>  drawYAxis(co2Max, yScale);<br>}</pre><pre>void drawYAxis(float co2Max, float yScale)<br>{<br>  float co2Tick = 320.0;<br>  strokeWeight(1);<br>  <br>  while(co2Tick &lt;= co2Max)<br>  {<br>    float yAxisTickPos = height - MARGIN - (co2Tick - CO2MIN) * yScale;<br>    <br>    line(MARGIN, yAxisTickPos, MARGIN - 5.0, yAxisTickPos);<br>    text(int(co2Tick), MARGIN - 15, yAxisTickPos+3);<br>    <br>    co2Tick += 10.0;<br>  }<br>}</pre><pre>void drawXAxis(int xAxisMaximum, float xScale)<br>{<br>  int yPos = height - MARGIN;<br>  float xAxisTick = 279.0 / 7.0;<br>  int axisYear = 1959;<br>  <br>  while(xAxisTick &lt;= xAxisMaximum)<br>  {<br>    int tickLength = 5;<br>    int xPos = MARGIN + int(xAxisTick * xScale);<br>    <br>    if(axisYear % 5 == 0)<br>    {<br>      text(axisYear, xPos, yPos + 22);<br>      tickLength = 10;<br>    }<br>        <br>    line(xPos, yPos, xPos, yPos + tickLength);<br>    <br>    xAxisTick += 365.25 / 7.0;<br>    axisYear++;<br>  }<br>}</pre><pre>void loadData()<br>{<br>  String[] lines = loadStrings(&quot;weekly_in_situ_co2_mlo.csv&quot;);<br>  <br>  for (String line : lines) <br>  {<br>    if( line.startsWith(&quot;\&quot;&quot;) ) continue;<br>    <br>    String[] values = split(line, &#39;,&#39;);<br>    String date = values[0];<br>    float co2 = parseFloat(values[1]);<br>    _data.set(date, co2);<br>  }<br>}</pre><p>To thank you for getting this far here’s a dog wanting more peanut butter (<a href="https://www.reddit.com/r/aww/comments/663iob/more_peanut_butter/">source</a>):</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*alXfhjip-z9VbTY0-VER1g.jpeg" /><figcaption>Yes, this is blatant click-bait.</figcaption></figure><p>¹ Tip for windows users is to open Windows Explorer to the location you want, press F4 and replace the contents of the text box with cmd and press enter. This opens a dos prompt for this directory.</p><p>² Best places on reddit.com are <a href="http://reddit.com/r/dataisbeautiful/">r/dataisbeautiful</a>, <a href="https://www.reddit.com/r/visualization/">r/visualization</a>, and <a href="https://www.reddit.com/r/climate">r/climate</a>.</p><figure><a href="http://bit.ly/HackernoonFB"><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0hqOaABQ7XGPT-OYNgiUBg.png" /></a></figure><figure><a href="https://goo.gl/k7XYbx"><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Vgw1jkA6hgnvwzTsfMlnpg.png" /></a></figure><figure><a href="https://goo.gl/4ofytp"><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gKBpq1ruUi0FVK2UM_I4tQ.png" /></a></figure><blockquote><a href="http://bit.ly/Hackernoon">Hacker Noon</a> is how hackers start their afternoons. We’re a part of the <a href="http://bit.ly/atAMIatAMI">@AMI</a> family. We are now <a href="http://bit.ly/hackernoonsubmission">accepting submissions</a> and happy to <a href="/service/mailto:partners@amipublications.com">discuss advertising &amp; sponsorship</a> opportunities.</blockquote><blockquote>If you enjoyed this story, we recommend reading our <a href="http://bit.ly/hackernoonlatestt">latest tech stories</a> and <a href="https://hackernoon.com/trending">trending tech stories</a>. Until next time, don’t take the realities of the world for granted!</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*35tCjoPcvq6LbB3I6Wegqw.jpeg" /></figure><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fupscri.be%2Fdde502%3Fas_embed%3Dtrue&amp;dntp=1&amp;url=https%3A%2F%2Fupscri.be%2Fhackernoon%2F&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=upscri" width="800" height="400" frameborder="0" scrolling="no"><a href="https://medium.com/media/3c851dac986ab6dbb2d1aaa91205a8eb/href">https://medium.com/media/3c851dac986ab6dbb2d1aaa91205a8eb/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f830d3846a34" width="1" height="1" alt=""><hr><p><a href="https://medium.com/hackernoon/putting-it-out-there-f830d3846a34">Putting it out there</a> was originally published in <a href="https://medium.com/hackernoon">HackerNoon.com</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[More polish]]></title>
            <link>https://medium.com/hackernoon/more-polish-59b1a13480a0?source=rss-a70fd7c25dd8------2</link>
            <guid isPermaLink="false">https://medium.com/p/59b1a13480a0</guid>
            <category><![CDATA[global-warming]]></category>
            <category><![CDATA[tutorial]]></category>
            <category><![CDATA[animation]]></category>
            <category><![CDATA[climate-change]]></category>
            <dc:creator><![CDATA[Kevin Pluck]]></dc:creator>
            <pubDate>Sun, 21 May 2017 11:53:16 GMT</pubDate>
            <atom:updated>2019-04-30T09:45:30.016Z</atom:updated>
            <content:encoded><![CDATA[<p>This is part 10 of an N part series detailing how I make <a href="https://medium.com/@kevpluck/spirals-and-barrels-364d1dd78175">my animations</a>.</p><p><a href="https://hackernoon.com/some-polish-62200a044d27">Prev</a> <a href="https://medium.com/@kevpluck/putting-it-out-there-f830d3846a34">Next</a></p><p>Last week I managed to show you how to render the x-axis which took a bit more effort than I was expecting so hopefully the y-axis won’t be so challenging especially seeing as it is a simple scalar value.</p><p>The y-axis is the concentration of <a href="https://hackernoon.com/tagged/co2">CO2</a> <a href="https://hackernoon.com/tagged/measured">measured</a> in PPM (parts per million) with a range of about 310–400 PPM.</p><p>As an aside; to comprehend those figures here’s an image of what 400 PPM looks like:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/425/1*eVuQreK6yELMGjGG7Eht5A.png" /></figure><p>First thing I’ve noticed about working out the y-axis is that unlike the x-axis where we know the maximum x value ahead of rendering as it’s simply a time variable we don’t know the maximum y value until it’s been extracted from the data and scaled. Not a problem, we simply draw the y-axis after the values have been plotted.</p><p>First of all we need a variable to record the maximum CO2 value:</p><pre>float co2Max = 0.0;</pre><p>Pop that just before the for loop in drawGraph().</p><p>Just after where the CO2 value is extracted in the for loop add:</p><pre>if(co2 &gt; co2Max) co2Max = co2;</pre><p>After the for loop add:</p><pre>drawYAxis(co2Max, yScale);</pre><p>That’ll get red squigglies underneath it as we haven’t created drawYAxis() yet. So let’s do that just after drawGraph():</p><pre>void drawYAxis(float co2Max, float yScale)<br>{<br>  <br>}</pre><p>I think a tick mark starting at 320PPM showing every 10PPM should suffice so let’s set the start value:</p><pre>float co2Tick = 320.0;</pre><p>Now while co2Tick is less than co2Max we want to draw a line for the tick mark and label it with the value, then increase it by 10:</p><pre>while(co2Tick &lt;= co2Max)<br>{<br> float yAxisTickPos = height - MARGIN - (co2Tick - 313.04) * yScale;</pre><pre> line(MARGIN, yAxisTickPos, MARGIN - 5.0, yAxisTickPos);<br> text(int(co2Tick), MARGIN - 15, yAxisTickPos+3);<br> <br> co2Tick += 10.0;<br>}</pre><p>Remember as the y axis is flipped for computers we need to subtract values from the height. Hmm, 313.04 is the minimum CO2 level from the data, perhaps should make that obvious by creating a global constant:</p><pre>final float CO2MIN = 313.04;</pre><p>That’s better:</p><pre>float yAxisTickPos = height - MARGIN - (co2Tick - CO2MIN) * yScale;</pre><p>The line co2Tick += 10.0 is a shorthand way of incrementing a variable by 10. Equivalent to co2Tick = co2Tick + 10.0.</p><p>Let’s give that a whirl:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fi.imgur.com%2FhAUn8cD.mp4&amp;src_secure=1&amp;url=https%3A%2F%2Fi.imgur.com%2FhAUn8cD.gifv&amp;image=https%3A%2F%2Fi.imgur.com%2FhAUn8cDh.jpg&amp;key=d04bfffea46d4aeda930ec88cc64b87c&amp;type=video%2Fmp4&amp;schema=imgur" width="500" height="500" frameborder="0" scrolling="no"><a href="https://medium.com/media/f458be67b14f851863d5c1a590a1194f/href">https://medium.com/media/f458be67b14f851863d5c1a590a1194f/href</a></iframe><p>That’s pretty good, but, it looks like we need to set the initial max CO2 so we get some tick marks to start with. The initial max CO2 will have to be the CO2 value that would be at the top of the graph. Pop this code just before we call drawYAxis():</p><pre>if((co2Max - CO2MIN) * yScale &lt; graphHeight) <br>    co2Max = graphHeight / yScale + CO2MIN;</pre><p>Resulting in:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fi.imgur.com%2FZPpSJgs.mp4&amp;src_secure=1&amp;url=https%3A%2F%2Fi.imgur.com%2FZPpSJgs.gifv&amp;image=https%3A%2F%2Fi.imgur.com%2FZPpSJgsh.jpg&amp;key=d04bfffea46d4aeda930ec88cc64b87c&amp;type=video%2Fmp4&amp;schema=imgur" width="500" height="500" frameborder="0" scrolling="no"><a href="https://medium.com/media/18a7e2f7b28c86c64d0016cb23d29bee/href">https://medium.com/media/18a7e2f7b28c86c64d0016cb23d29bee/href</a></iframe><p>Much better.</p><p>Now we need to label the axes. The x axis is straight forward:</p><pre>text(&quot;Year&quot;, MARGIN + graphWidth / 2, height - 20);</pre><p>But the y axis needs to be rotated. Brace yourself as this involves some <em>matrix manipulation. </em>Don’t worry, we aren’t going to be multiplying matrices or doing dot products (although that would be fun). We are simply going to use these two functions: translate() and rotate().</p><p>If you imagine a grid overlaying our drawing space with the origin sitting in the top left corner then calling translate(x,y) shifts the origin along with the whole grid to your new location while rotate(radian) rotates the whole grid about the origin. After calling these matrix manipulation functions all following drawing functions will plot their points on the modified grid.</p><p>This means that when we draw some rotated text we have to rotate the grid back again. Ugh. Thankfully Processing has a simple solution to that; it’s the matrix stack. (Not sure what a <em>stack</em> is? <a href="https://simple.wikipedia.org/wiki/Stack_(data_structure)">Here we go</a>.) It has two commands; pushMatrix() and popMatrix(). Basically pushMatrix() remembers the current orientation of the grid so you can manipulate it how you want and then popMatrix() restores it. Because it’s a stack you can call those methods repeatedly as long as a popMatrix() call is paired with a preceding pushMatrix().</p><p>So just before drawYAxis() add:</p><pre>pushMatrix();<br>    translate(0, graphWidth / 2);<br>    rotate(-PI / 2.0);<br>    text(&quot;CO₂ concentration (PPM)&quot;, 0, 20);<br>popMatrix();</pre><p>When working with radians (the best unit of angles of course) I like to work with fractions of PI as it’s easier to comprehend as all you need to remember is that 180° is PI, 90° is PI / 2, 45° is PI / 4 etc.</p><p>After titling the main graph we get:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fi.imgur.com%2FIU1Cw7w.mp4&amp;src_secure=1&amp;url=https%3A%2F%2Fi.imgur.com%2FIU1Cw7w.gifv&amp;image=https%3A%2F%2Fi.imgur.com%2FIU1Cw7wh.jpg&amp;key=d04bfffea46d4aeda930ec88cc64b87c&amp;type=video%2Fmp4&amp;schema=imgur" width="500" height="500" frameborder="0" scrolling="no"><a href="https://medium.com/media/63d677c070319b3ead81fa66fb1be353/href">https://medium.com/media/63d677c070319b3ead81fa66fb1be353/href</a></iframe><p>And the now rather long and complete code:</p><pre>import java.time.*;<br>import de.looksgood.ani.*;<br>Ani _ani;</pre><pre>FloatDict _data = new FloatDict();<br>LocalDate _startDate = LocalDate.of(1958, 3, 29);</pre><pre>void setup()<br>{<br>  loadData();<br>  size(500,500);<br>  background(0);<br>  stroke(255,255,0);<br>  textAlign(CENTER);<br>  <br>  Ani.init(this);<br>  Ani.setDefaultTimeMode(Ani.FRAMES);<br>  _ani = new Ani(this, 530, &quot;_change&quot;, 6, Ani.EXPO_IN);<br>}</pre><pre>float yScale = 20.0;</pre><pre>boolean _coda = false;<br>int _codaCount = 0;</pre><pre>float _change = 1.0;<br>final int MARGIN = 60;<br>final float CO2MIN = 313.04;</pre><pre>void draw()<br>{<br>  // The following pauses the animation before exiting<br>  if(_change &gt;= 6.0) <br>  {<br>    if(!_coda)<br>    {<br>      _coda = true;<br>      _codaCount = frameCount;<br>    }<br>    if(frameCount - _codaCount &gt; 120) <br>    {<br>      exit();<br>    }<br>    return;<br>  }<br>  <br>  background(0);<br>  strokeWeight(1);<br>  <br>  line(MARGIN, 0, MARGIN, height);<br>  line(0, height - MARGIN, width, height - MARGIN);<br>  <br>  drawGraph(MARGIN, height - MARGIN, width - MARGIN, height - MARGIN);<br>}</pre><pre>void drawGraph(int xPos, int yPos, int graphWidth, int graphHeight)<br>{<br>  text(&quot;Atmospheric CO₂ concentration 1958 - 2017\nR. F. Keeling, S. J. Walker, S. C. Piper and A. F. Bollenbacher\nMauna Loa, Observatory, Hawaii&quot;, MARGIN + graphWidth / 2.0, MARGIN);<br>  int deltaX = int(frameCount * _change);<br>    <br>  float xScale = 1.0;<br>  <br>  int xAxisMaximum = graphWidth;<br>  <br>  <br>  if(deltaX &gt; graphWidth)<br>  {<br>    xAxisMaximum = deltaX;<br>    xScale = float(graphWidth) / float(deltaX);<br>  }<br>  <br>  text(&quot;Year&quot;, MARGIN + graphWidth / 2, height - 20);<br>  drawXAxis(xAxisMaximum, xScale);<br>  <br>  strokeWeight(3);<br>  <br>  float co2Max = 0.0;<br>  <br>  for(int dataIndex = 1; dataIndex &lt;= deltaX; dataIndex++)<br>  {<br>    <br>    int daysFromStart = (dataIndex - 1) * 7;<br>    LocalDate frameDate = _startDate.plusDays(daysFromStart);<br>       <br>    <br>    if(_data.hasKey(frameDate.toString()))<br>    {<br>      float co2 = _data.get(frameDate.toString());<br>      <br>      if(co2 &gt; co2Max) co2Max = co2;<br>            <br>      float x = dataIndex;<br>      float y = (co2 - CO2MIN);<br>      <br>      if(y * yScale &gt; graphHeight) <br>      {<br>        yScale = float(graphHeight)/y;<br>      }<br>      <br>      point(xPos + x * xScale, yPos - (y * yScale));<br>    }<br>  }<br>  <br>  if((co2Max - CO2MIN) * yScale &lt; graphHeight) <br>    co2Max = graphHeight / yScale + CO2MIN;<br>  <br>  pushMatrix();<br>    translate(0, graphWidth / 2);<br>    rotate(-PI / 2.0);<br>    text(&quot;CO₂ concentration (PPM)&quot;, 0, 20);<br>  popMatrix();<br>  <br>  drawYAxis(co2Max, yScale);<br>}</pre><pre>void drawYAxis(float co2Max, float yScale)<br>{<br>  float co2Tick = 320.0;<br>  strokeWeight(1);<br>  <br>  while(co2Tick &lt;= co2Max)<br>  {<br>    float yAxisTickPos = height - MARGIN - (co2Tick - CO2MIN) * yScale;<br>    <br>    line(MARGIN, yAxisTickPos, MARGIN - 5.0, yAxisTickPos);<br>    text(int(co2Tick), MARGIN - 15, yAxisTickPos+3);<br>    <br>    co2Tick += 10.0;<br>  }<br>}</pre><pre>void drawXAxis(int xAxisMaximum, float xScale)<br>{<br>  int yPos = height - MARGIN;<br>  float xAxisTick = 279.0 / 7.0;<br>  int axisYear = 1959;<br>  <br>  while(xAxisTick &lt;= xAxisMaximum)<br>  {<br>    int tickLength = 5;<br>    int xPos = MARGIN + int(xAxisTick * xScale);<br>    <br>    if(axisYear % 5 == 0)<br>    {<br>      text(axisYear, xPos, yPos + 22);<br>      tickLength = 10;<br>    }<br>        <br>    line(xPos, yPos, xPos, yPos + tickLength);<br>    <br>    xAxisTick += 365.25 / 7.0;<br>    axisYear++;<br>  }<br>}</pre><pre>void loadData()<br>{<br>  String[] lines = loadStrings(&quot;weekly_in_situ_co2_mlo.csv&quot;);<br>  <br>  for (String line : lines) <br>  {<br>    if( line.startsWith(&quot;\&quot;&quot;) ) continue;<br>    <br>    String[] values = split(line, &#39;,&#39;);<br>    String date = values[0];<br>    float co2 = parseFloat(values[1]);<br>    _data.set(date, co2);<br>  }<br>}</pre><p>Next week I’ll show you how to record videos and generate gifs along with hosting them on the internet.</p><figure><a href="http://bit.ly/HackernoonFB"><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0hqOaABQ7XGPT-OYNgiUBg.png" /></a></figure><figure><a href="https://goo.gl/k7XYbx"><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Vgw1jkA6hgnvwzTsfMlnpg.png" /></a></figure><figure><a href="https://goo.gl/4ofytp"><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gKBpq1ruUi0FVK2UM_I4tQ.png" /></a></figure><blockquote><a href="http://bit.ly/Hackernoon">Hacker Noon</a> is how hackers start their afternoons. We’re a part of the <a href="http://bit.ly/atAMIatAMI">@AMI</a> family. We are now <a href="http://bit.ly/hackernoonsubmission">accepting submissions</a> and happy to <a href="/service/mailto:partners@amipublications.com">discuss advertising &amp; sponsorship</a> opportunities.</blockquote><blockquote>If you enjoyed this story, we recommend reading our <a href="http://bit.ly/hackernoonlatestt">latest tech stories</a> and <a href="https://hackernoon.com/trending">trending tech stories</a>. Until next time, don’t take the realities of the world for granted!</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*35tCjoPcvq6LbB3I6Wegqw.jpeg" /></figure><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fupscri.be%2Fdde502%3Fas_embed%3Dtrue&amp;dntp=1&amp;url=https%3A%2F%2Fupscri.be%2Fhackernoon%2F&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=upscri" width="800" height="400" frameborder="0" scrolling="no"><a href="https://medium.com/media/3c851dac986ab6dbb2d1aaa91205a8eb/href">https://medium.com/media/3c851dac986ab6dbb2d1aaa91205a8eb/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=59b1a13480a0" width="1" height="1" alt=""><hr><p><a href="https://medium.com/hackernoon/more-polish-59b1a13480a0">More polish</a> was originally published in <a href="https://medium.com/hackernoon">HackerNoon.com</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Some polish]]></title>
            <link>https://medium.com/hackernoon/some-polish-62200a044d27?source=rss-a70fd7c25dd8------2</link>
            <guid isPermaLink="false">https://medium.com/p/62200a044d27</guid>
            <category><![CDATA[climate-change]]></category>
            <category><![CDATA[tutorial]]></category>
            <category><![CDATA[animation]]></category>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[global-warming]]></category>
            <dc:creator><![CDATA[Kevin Pluck]]></dc:creator>
            <pubDate>Mon, 15 May 2017 23:04:48 GMT</pubDate>
            <atom:updated>2019-04-30T09:46:50.602Z</atom:updated>
            <content:encoded><![CDATA[<p>This is part 9 of an N part series detailing how I make <a href="https://medium.com/@kevpluck/spirals-and-barrels-364d1dd78175">my animations</a>.</p><p><a href="https://hackernoon.com/rates-of-change-ca904f010a19">Prev</a> <a href="https://medium.com/@kevpluck/more-polish-59b1a13480a0">Next</a></p><p>I have discovered that there is a <a href="https://hackernoon.com/tagged/command">command</a> in <a href="https://hackernoon.com/tagged/processing">Processing</a> to plot a point instead of drawing a small ellipse like I have done! I had looked to see how Processing sets pixels which it does with the <a href="https://processing.org/reference/set_.html">set()</a> command but the pixels were too small. Processing also has a <a href="https://processing.org/reference/point_.html">point()</a> command which I disregarded as it looked the same. The difference is that point() plots a circle using strokeWeight() as the size!</p><pre>strokeWeight(10);<br>stroke(255,255,0);<br>point(50,50);</pre><p>Produces:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/132/1*1YyGGulYa3fUTkG2CgscLA.png" /></figure><p>Woohoo, now back to our regularly scheduled program…</p><p>My maths teacher drummed into my head “always label your axes” so let’s do that along with the actual axes themselves.</p><p>Oh, but our scale is continuously changing. This makes it trickier but not impossible…</p><p>First we had better make some room for them, currently our plot is shown hard up against the edges of the window.</p><p>I was going to just create a margin variable and then simply add it to the x and y values but that turned out to be trickier than I thought as I then had to modify the scale algorithms in a non intuitive manner (what actually happened is that I failed to scale it correctly at all and gave up in disgust).</p><p>So a far simpler way is to pull out the code into a function and pass in the new position and the width and height. Sounds like a lot of work but it isn’t. First rename the draw() function to drawGraph(). Create a new draw() function and put in the body drawGraph(); like so:</p><pre>void draw()<br>{<br>   drawGraph();<br>}</pre><pre>void drawGraph()<br>{<br>   ...<br>}</pre><p>Now add xPos, yPos, graphWidth, graphHeight integers to drawGraph().</p><pre>void drawGraph(int xPos, int yPos, int graphWidth, int graphHeight)</pre><p>I think that a margin of 60 pixels is a good starting point. So let’s make a global constant with that value. Add this line just before draw():</p><pre>final int MARGIN = 60;</pre><p>Marking something as final means it cannot be modified and by convention these variables are named using all caps.</p><p>Now all we do is to call drawGraph() from draw() with the following:</p><pre>drawGraph(MARGIN, height - MARGIN, width - MARGIN, height - MARGIN);</pre><p>So let’s start using those values in drawGraph():</p><p>First replace all uses of width and height with graphWidth and graphHeight respectively.</p><p>Then replace the ellipse() function with the newly discovered point() function:</p><pre>point(xPos + x * xScale, yPos - (y * yScale));</pre><p>And you’re done. Drawing with point() also seems a lot faster! Oh yeah, add strokeWeight(3) to setup() otherwise your points will be 1 pixel in size.</p><p>Ok, let’s draw some axes. Simple lines from top to bottom using MARGIN so in the draw() function:</p><pre>line(MARGIN, 0, MARGIN, height);<br>line(0, height - MARGIN, width, height - MARGIN);</pre><p>Hmm, that didn’t work first time for me. This was due to leaving background(0) in the drawGraph() function which was clearing the drawn lines. Move that to the first line of draw() and it will all work.</p><p>Result should look like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/502/1*K97u1nqdo4L7brr9XvEicw.png" /></figure><p>The line thickness is inherited from the strokeWeight command in setup(). So let’s move that to the first line of drawGraph() and add strokeWeight(1) to the first line of draw(). That will make a thin line for the axes and heavy points for the graph.</p><p>So now we need to add tick marks. As the tick marks need to be scaled to the same as the graph then they’ll need to be drawn from inside the drawGraph() function. Let’s do the x axis first by marking each year.</p><p>Yikes. This isn’t as easy as I thought it would be. Because each data point is recorded on a Saturday and we are simply multiplying our dataIndex by 7 to get the number of days from the start date to find the date to get the data it makes working out the tick marks non trivial.</p><p>After a fair bit of experimentation I was able to reduce the complexity to the following function:</p><pre>void drawXAxis(int xAxisMaximum, float xScale)<br>{<br>  int yPos = height - MARGIN;<br>  float xAxisTick = 279.0 / 7.0;<br>  int axisYear = 1958;<br>  <br>  while(xAxisTick &lt;= xAxisMaximum)<br>  {<br>    int tickLength = 5;<br>    int xPos = MARGIN + int(xAxisTick * xScale);<br>    <br>    if(axisYear % 5 == 0)<br>    {<br>      text(axisYear, xPos, yPos + 22);<br>      tickLength = 10;<br>    }<br>        <br>    line(xPos, yPos, xPos, yPos + tickLength);<br>    <br>    xAxisTick += 365.25 / 7.0;<br>    axisYear++;<br>  }<br>}</pre><p>Where the xAxisMaximum starts off as the width of the plot and then as the scale increases the scaled max x value. The initial value of the xAxisTick is the number of days left in the year after 29/3/1958 (279) converted to weeks by dividing by 7.</p><p>Here I am introducing a while loop. This type of loop keeps executing while the contents of the parenthesis is true. In this case while the current position of the tick is less than the maximum x value on the axis.</p><p>The line if(axisYear % 5 == 0) makes use of the modulus operator % to make a longer tick mark and display the year every fifth year. We then simply draw a line for the tick followed by incrementing the tick position by the average number of weeks in a year.</p><p>This results in:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fi.imgur.com%2FwdlZtU0.mp4&amp;src_secure=1&amp;url=https%3A%2F%2Fi.imgur.com%2FwdlZtU0.gifv&amp;image=https%3A%2F%2Fi.imgur.com%2FwdlZtU0h.jpg&amp;key=d04bfffea46d4aeda930ec88cc64b87c&amp;type=video%2Fmp4&amp;schema=imgur" width="500" height="500" frameborder="0" scrolling="no"><a href="https://medium.com/media/d4c4a64193597473ac6e203e8a8f3519/href">https://medium.com/media/d4c4a64193597473ac6e203e8a8f3519/href</a></iframe><p>Which I’m rather pleased with. Here’s the complete code:</p><pre>import java.time.*;<br>import de.looksgood.ani.*;<br>Ani _ani;</pre><pre>FloatDict _data = new FloatDict();<br>LocalDate _startDate = LocalDate.of(1958, 3, 29);</pre><pre>void setup()<br>{<br>  loadData();<br>  size(500,500);<br>  background(0);<br>  stroke(255,255,0);<br>  textAlign(CENTER);<br>  <br>  Ani.init(this);<br>  Ani.setDefaultTimeMode(Ani.FRAMES);<br>  _ani = new Ani(this, 1032, &quot;_change&quot;, 3, Ani.EXPO_IN);<br>}</pre><pre>float yScale = 20.0;</pre><pre>boolean _coda = false;<br>int _codaCount = 0;</pre><pre>float _change = 1.0;<br>final int MARGIN = 60;</pre><pre>void draw()<br>{<br>  background(0);<br>  strokeWeight(1);<br>  <br>  line(MARGIN, 0, MARGIN, height);<br>  line(0, height - MARGIN, width, height - MARGIN);<br>  <br>  drawGraph(MARGIN, height - MARGIN, width - MARGIN, height - MARGIN);<br>}</pre><pre>void drawGraph(int xPos, int yPos, int graphWidth, int graphHeight)<br>{<br>  int deltaX = int(frameCount * _change);<br>    <br>  float xScale = 1.0;<br>  <br>  int xAxisMaximum = graphWidth;<br>  <br>  if(deltaX &gt; graphWidth)<br>  {<br>    xAxisMaximum = deltaX;<br>    xScale = float(graphWidth) / float(deltaX);<br>  }<br>  <br>  drawXAxis(xAxisMaximum, xScale);<br>  <br>  strokeWeight(3);<br>  <br>  for(int dataIndex = 1; dataIndex &lt;= deltaX; dataIndex++)<br>  {<br>    <br>    int daysFromStart = (dataIndex - 1) * 7;<br>    LocalDate frameDate = _startDate.plusDays(daysFromStart);<br>       <br>    <br>    if(_data.hasKey(frameDate.toString()))<br>    {<br>      float co2 = _data.get(frameDate.toString());<br>            <br>      float x = dataIndex;<br>      float y = (co2 - 313.04) ;<br>      <br>      if(y * yScale &gt; graphHeight) <br>      {<br>        yScale = float(graphHeight)/y;<br>      }<br>      <br>      point(xPos + x * xScale, yPos - (y * yScale));<br>    }<br>  }    <br>}</pre><pre>void drawXAxis(int xAxisMaximum, float xScale)<br>{<br>  int yPos = height - MARGIN;<br>  float xAxisTick = 279.0 / 7.0;<br>  int axisYear = 1958;<br>  <br>  while(xAxisTick &lt;= xAxisMaximum)<br>  {<br>    int tickLength = 5;<br>    int xPos = MARGIN + int(xAxisTick * xScale);<br>    <br>    if(axisYear % 5 == 0)<br>    {<br>      text(axisYear, xPos, yPos + 22);<br>      tickLength = 10;<br>    }<br>        <br>    line(xPos, yPos, xPos, yPos + tickLength);<br>    <br>    xAxisTick += 365.25 / 7.0;<br>    axisYear++;<br>  }<br>}</pre><pre>void loadData()<br>{<br>  String[] lines = loadStrings(&quot;weekly_in_situ_co2_mlo.csv&quot;);<br>  <br>  for (String line : lines) <br>  {<br>    if( line.startsWith(&quot;\&quot;&quot;) ) continue;<br>    <br>    String[] values = split(line, &#39;,&#39;);<br>    String date = values[0];<br>    float co2 = parseFloat(values[1]);<br>    _data.set(date, co2);<br>  }<br>}</pre><p>That was a whole lot more complicated than I was expecting. I intended this post to cover both the x and y axis along with titles etc! I guess that’ll be next week then…</p><figure><a href="http://bit.ly/HackernoonFB"><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0hqOaABQ7XGPT-OYNgiUBg.png" /></a></figure><figure><a href="https://goo.gl/k7XYbx"><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Vgw1jkA6hgnvwzTsfMlnpg.png" /></a></figure><figure><a href="https://goo.gl/4ofytp"><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gKBpq1ruUi0FVK2UM_I4tQ.png" /></a></figure><blockquote><a href="http://bit.ly/Hackernoon">Hacker Noon</a> is how hackers start their afternoons. We’re a part of the <a href="http://bit.ly/atAMIatAMI">@AMI</a> family. We are now <a href="http://bit.ly/hackernoonsubmission">accepting submissions</a> and happy to <a href="/service/mailto:partners@amipublications.com">discuss advertising &amp; sponsorship</a> opportunities.</blockquote><blockquote>If you enjoyed this story, we recommend reading our <a href="http://bit.ly/hackernoonlatestt">latest tech stories</a> and <a href="https://hackernoon.com/trending">trending tech stories</a>. Until next time, don’t take the realities of the world for granted!</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*35tCjoPcvq6LbB3I6Wegqw.jpeg" /></figure><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fupscri.be%2Fdde502%3Fas_embed%3Dtrue&amp;dntp=1&amp;url=https%3A%2F%2Fupscri.be%2Fhackernoon%2F&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=upscri" width="800" height="400" frameborder="0" scrolling="no"><a href="https://medium.com/media/3c851dac986ab6dbb2d1aaa91205a8eb/href">https://medium.com/media/3c851dac986ab6dbb2d1aaa91205a8eb/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=62200a044d27" width="1" height="1" alt=""><hr><p><a href="https://medium.com/hackernoon/some-polish-62200a044d27">Some polish</a> was originally published in <a href="https://medium.com/hackernoon">HackerNoon.com</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Rates of change]]></title>
            <link>https://medium.com/hackernoon/this-is-part-8-of-an-n-part-series-detailing-how-i-make-my-animations-ca904f010a19?source=rss-a70fd7c25dd8------2</link>
            <guid isPermaLink="false">https://medium.com/p/ca904f010a19</guid>
            <category><![CDATA[tutorial]]></category>
            <category><![CDATA[climate-change]]></category>
            <category><![CDATA[global-warming]]></category>
            <category><![CDATA[animation]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Kevin Pluck]]></dc:creator>
            <pubDate>Sun, 07 May 2017 00:11:59 GMT</pubDate>
            <atom:updated>2019-04-30T09:47:38.035Z</atom:updated>
            <content:encoded><![CDATA[<p>This is part 8 of an N part series detailing how I make <a href="https://medium.com/@kevpluck/spirals-and-barrels-364d1dd78175">my animations</a>.</p><p><a href="https://hackernoon.com/change-your-scales-97e4ee37c4d4">Prev</a> <a href="https://medium.com/@kevpluck/some-polish-62200a044d27">Next</a></p><p>We don’t want to bore our audience with a long slow <a href="https://hackernoon.com/tagged/animation">animation</a> so let’s speed things up a bit. Currently we are drawing one dot per frame so the simplest way to go is multiple dots per frame by creating a variable called, say, deltaX and setting it to five times the frame count:</p><pre>int deltaX = frameCount * 5;</pre><p>Then replace all references of frameCount to deltaX resulting in:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fi.imgur.com%2FMOvkxZo.mp4&amp;src_secure=1&amp;url=https%3A%2F%2Fi.imgur.com%2FMOvkxZo.gifv&amp;image=https%3A%2F%2Fi.imgur.com%2FMOvkxZoh.jpg&amp;key=d04bfffea46d4aeda930ec88cc64b87c&amp;type=video%2Fmp4&amp;schema=imgur" width="500" height="500" frameborder="0" scrolling="no"><a href="https://medium.com/media/5cc947aff545f1a6e218866d64009e4d/href">https://medium.com/media/5cc947aff545f1a6e218866d64009e4d/href</a></iframe><p>Just like that. Here’s the whole code:</p><pre>import java.time.*;</pre><pre>FloatDict _data = new FloatDict();<br>LocalDate _startDate = LocalDate.of(1958, 3, 29);</pre><pre>void setup()<br>{<br>  loadData();<br>  size(500,500);<br>  background(0);<br>  stroke(255,255,0);<br>}</pre><pre>float yScale = 20.0;</pre><pre>void draw()<br>{<br>  background(0);<br>  float xScale = 1.0;<br>  <br>  int deltaX = frameCount * 5;<br>  <br>  if(deltaX &gt; width)<br>  {<br>    xScale = float(width) / float(deltaX);<br>  }</pre><pre>  for(int dataIndex = 1; dataIndex &lt;= deltaX; dataIndex++)<br>  {<br>    int daysFromStart = (dataIndex - 1) * 7;<br>    LocalDate frameDate = _startDate.plusDays(daysFromStart);<br>    <br>    if(_data.hasKey(frameDate.toString()))<br>    {<br>      float co2 = _data.get(frameDate.toString());<br>            <br>      float x = dataIndex;<br>      float y = (co2 - 313.04) ;<br>      <br>      if(y * yScale &gt; height) <br>      {<br>        yScale = float(height)/y;<br>      }<br>      <br>      ellipse(x * xScale, height - (y * yScale), 1, 1);<br>    }<br>  }<br>}</pre><pre>void loadData()<br>{<br>  String[] lines = loadStrings(&quot;weekly_in_situ_co2_mlo.csv&quot;);<br>  <br>  for (String line : lines) <br>  {<br>    if( line.startsWith(&quot;\&quot;&quot;) ) continue;<br>    <br>    String[] values = split(line, &#39;,&#39;);<br>    String date = values[0];<br>    float co2 = parseFloat(values[1]);<br>    _data.set(date, co2);<br>  }<br>}</pre><p>But, I feel it’s too fast at the start yet not fast enough at the end. This means we should vary the <a href="https://hackernoon.com/tagged/rate">rate</a> during the animation. One approach would be to use the frameCount to vary the rate but this gets complicated very quickly.</p><p>This sort of thing is very common in animations and conveniently clever types have made libraries that simplify this. These libraries are called “easing libraries” and we will be using one called Ani written by Benedikt Groß.</p><p>To install Ani go to Processing’s Tools menu and select Add Tool... In the Libraries tab search for Ani and click install.</p><p>What this library does is to change the value of a particular variable over the course of an animation using a chosen algorithm. We would like to change the speed of the animation from slow to fast in a steady fashion. To do this we replace the constant value 5 in the code above with a variable. I’m calling this variable _change and setting its initial value to 1.0.</p><pre>float _change = 1.0;</pre><pre>void draw()<br>{<br>  int deltaX = int(frameCount * _change);</pre><p>Clearly this doesn’t do much by itself.</p><p>Now let’s activate Ani. First we import it:</p><pre>import de.looksgood.ani.*;<br>Ani _ani;</pre><p>_ani is the variable that will be containing the Ani object.</p><p>Then we set it up in setup():</p><pre>  Ani.init(this);<br>  Ani.setDefaultTimeMode(Ani.FRAMES);<br>  _ani = new Ani(this, 1032, &quot;_change&quot;, 3.0, Ani.EXPO_IN);</pre><p>this is a reference to our Keeling animation letting the Ani library know where to find things. By default the TimeMode is based on seconds but I find it to be more accurate to use frames as we can be dealing with many thousands of data points and each frame can take a while to render.</p><p>So the meat and potatoes of the Ani library is contained in the line:</p><pre>_ani = new Ani(this, 1032, &quot;_change&quot;, 3.0, Ani.EXPO_IN);</pre><p>What this is saying is over 1,032 frames¹ modify the value in the _change variable to end up with the value 3 exponentially. Remember we defined _change to start with the value 1.0 above so this slowly increases this value until it reaches 3.0 after 1,032 frames.</p><p>The full documentation is available <a href="http://benedikt-gross.de/libraries/Ani/">here</a>.</p><p>Running this we get:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fi.imgur.com%2FAfpXMAp.mp4&amp;src_secure=1&amp;url=https%3A%2F%2Fi.imgur.com%2FAfpXMAp.gifv&amp;image=https%3A%2F%2Fi.imgur.com%2FAfpXMAph.jpg&amp;key=d04bfffea46d4aeda930ec88cc64b87c&amp;type=video%2Fmp4&amp;schema=imgur" width="500" height="500" frameborder="0" scrolling="no"><a href="https://medium.com/media/04c51ea9b2cbfa4220778f89d5a06c6e/href">https://medium.com/media/04c51ea9b2cbfa4220778f89d5a06c6e/href</a></iframe><p>As you can see it starts of nice, slow and easy then rapidly increases in speed.</p><p>Here is the full code:</p><pre>import java.time.*;<br>import de.looksgood.ani.*;<br>Ani _ani;</pre><pre>FloatDict _data = new FloatDict();<br>LocalDate _startDate = LocalDate.of(1958, 3, 29);</pre><pre>void setup()<br>{<br>  loadData();<br>  size(500,500);<br>  background(0);<br>  stroke(255,255,0);<br>  frameRate(30);<br>  <br>  Ani.init(this);<br>  Ani.setDefaultTimeMode(Ani.FRAMES);<br>  _ani = new Ani(this, 1032, &quot;_change&quot;, 3.0, Ani.EXPO_IN);<br>}</pre><pre>float yScale = 20.0;<br>float _change = 1.0;</pre><pre>void draw()<br>{<br>  int deltaX = int(frameCount * _change);<br>   <br>  background(0);<br>  float xScale = 1.0;<br>   <br>  if(deltaX &gt; width)<br>  {<br>    xScale = float(width) / float(deltaX);<br>  }<br>  <br>  for(int dataIndex = 1; dataIndex &lt;= deltaX; dataIndex++)<br>  {<br>    <br>    int daysFromStart = (dataIndex - 1) * 7;<br>    LocalDate frameDate = _startDate.plusDays(daysFromStart);<br>       <br>    if(_data.hasKey(frameDate.toString()))<br>    {<br>      float co2 = _data.get(frameDate.toString());<br>            <br>      float x = dataIndex;<br>      float y = (co2 - 313.04) ;<br>      <br>      if(y * yScale &gt; height) <br>      {<br>        yScale = float(height)/y;<br>      }<br>      <br>      ellipse(x * xScale, height - (y * yScale), 1, 1);<br>    }<br>  }<br>}</pre><pre>void loadData()<br>{<br>  String[] lines = loadStrings(&quot;weekly_in_situ_co2_mlo.csv&quot;);<br>  <br>  for (String line : lines) <br>  {<br>    if( line.startsWith(&quot;\&quot;&quot;) ) continue;<br>    <br>    String[] values = split(line, &#39;,&#39;);<br>    String date = values[0];<br>    float co2 = parseFloat(values[1]);<br>    _data.set(date, co2);<br>  }<br>}</pre><p>Next week we’ll be adding text and a bit of polish!</p><p>¹ I came up with 1,032 through trial and error. I’m sure there was a way of calculating it but life’s too short.</p><blockquote><a href="http://bit.ly/Hackernoon">Hacker Noon</a> is how hackers start their afternoons. We’re a part of the <a href="http://bit.ly/atAMIatAMI">@AMI</a>family. We are now <a href="http://bit.ly/hackernoonsubmission">accepting submissions</a> and happy to <a href="/service/mailto:partners@amipublications.com">discuss advertising &amp; sponsorship</a> opportunities.</blockquote><blockquote>To learn more, <a href="https://goo.gl/4ofytp">read our about page</a>, <a href="http://bit.ly/HackernoonFB">like/message us on Facebook</a>, or simply, <a href="https://goo.gl/k7XYbx">tweet/DM @HackerNoon.</a></blockquote><blockquote>If you enjoyed this story, we recommend reading our <a href="http://bit.ly/hackernoonlatestt">latest tech stories</a> and <a href="https://hackernoon.com/trending">trending tech stories</a>. Until next time, don’t take the realities of the world for granted!</blockquote><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fupscri.be%2Fdde502%3Fas_embed%3Dtrue&amp;dntp=1&amp;url=https%3A%2F%2Fupscri.be%2Fhackernoon%2F&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=upscri" width="800" height="400" frameborder="0" scrolling="no"><a href="https://medium.com/media/3c851dac986ab6dbb2d1aaa91205a8eb/href">https://medium.com/media/3c851dac986ab6dbb2d1aaa91205a8eb/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ca904f010a19" width="1" height="1" alt=""><hr><p><a href="https://medium.com/hackernoon/this-is-part-8-of-an-n-part-series-detailing-how-i-make-my-animations-ca904f010a19">Rates of change</a> was originally published in <a href="https://medium.com/hackernoon">HackerNoon.com</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Change your scales]]></title>
            <link>https://medium.com/hackernoon/change-your-scales-97e4ee37c4d4?source=rss-a70fd7c25dd8------2</link>
            <guid isPermaLink="false">https://medium.com/p/97e4ee37c4d4</guid>
            <category><![CDATA[global-warming]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[animation]]></category>
            <category><![CDATA[climate-change]]></category>
            <category><![CDATA[tutorial]]></category>
            <dc:creator><![CDATA[Kevin Pluck]]></dc:creator>
            <pubDate>Sat, 29 Apr 2017 00:07:52 GMT</pubDate>
            <atom:updated>2019-04-30T09:42:54.922Z</atom:updated>
            <content:encoded><![CDATA[<p>This is part 7 of an N part series detailing how I make <a href="https://medium.com/@kevpluck/spirals-and-barrels-364d1dd78175">my animations</a>.</p><p><a href="https://medium.com/@kevpluck/learn-your-scales-f78da9bea959">Prev</a> <a href="https://medium.com/@kevpluck/rates%20of%20change-ca904f010a19">Next</a></p><p>Now we have reached the limits of what we can display in our 500x500 <a href="https://hackernoon.com/tagged/animation">animation</a> so we will need to change the <a href="https://hackernoon.com/tagged/scale">scale</a> to fit the plot for the full 58 years of data.</p><p>So once x is greater than 500 we can start reducing the x scale along with the y. Let’s do that:</p><pre>    float xScale = 1.0;<br>    float yScale = 40.0;<br>    <br>    if(frameCount &gt; width)<br>    {<br>      xScale = float(width) / float(frameCount);<br>    }<br>    <br>    float x = frameCount * xScale;<br>    float y = (co2 - 313.04) * yScale;<br>    <br>    ellipse(x, height - y, 1, 1);</pre><p>What’s with the float()? Because width and framcCount are integers when you divide them the result is an integer. Unfortunately 500/501 as an integer results in 0, not 0.998 so we have to convert the integer values into floats. You’ll also note that when I am using a constant value, like 40.0 I include the decimal point, this causes the computer to do floating point arithmetic and not integer arithmetic. For example the result of 1/10 would equal 0 while the result of 1.0/10.0 would be 0.1 as expected. Be aware that 1.0/10 would also result in 0!</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fi.imgur.com%2Fykqfifn.mp4&amp;src_secure=1&amp;url=https%3A%2F%2Fi.imgur.com%2Fykqfifn.gifv&amp;image=https%3A%2F%2Fi.imgur.com%2Fykqfifn.gif%3Fnoredirect&amp;key=d04bfffea46d4aeda930ec88cc64b87c&amp;type=video%2Fmp4&amp;schema=imgur" width="502" height="500" frameborder="0" scrolling="no"><a href="https://medium.com/media/ba88519e15f8683eada0f8d9e1fc7416/href">https://medium.com/media/ba88519e15f8683eada0f8d9e1fc7416/href</a></iframe><p>Oh, well that’s not what we want! The image doesn’t scale and the last few dots are all squashed up against the right side of the image.</p><p>Our code is plotting one dot per frame and keeping all the other dots previously drawn visible. This means that when we change the scale we are only changing the scale for the next dot drawn, not for all the dots previously drawn.</p><p>So to overcome this we have to redraw all previous dots and the next one each time. This is done with a for() loop of a different format to the one in loadData()</p><pre>for(int dataIndex = 1; dataIndex &lt;= frameCount; dataIndex++)<br>{}</pre><p>This loop defines a variable called dataIndex which starts at 1 and increases by 1 until it reaches the current frameCount. The dataIndex++ notation is a shorthand way of saying dataIndex = dataIndex + 1</p><p>Here it is in context:</p><pre>void draw()<br>{<br>  float xScale = 1.0;<br>  float yScale = 40.0;<br>  <br>  if(frameCount &gt; width)<br>  {<br>    xScale = float(width) / float(frameCount);<br>  }</pre><pre>  for(int dataIndex = 1; dataIndex &lt;= frameCount; dataIndex++)<br>  {<br>    int daysFromStart = (dataIndex - 1) * 7;<br>    LocalDate frameDate = _startDate.plusDays(daysFromStart);<br>    <br>    if(_data.hasKey(frameDate.toString()))<br>    {<br>      float co2 = _data.get(frameDate.toString());<br>            <br>      float x = dataIndex * xScale;<br>      float y = (co2 - 313.04) * yScale;<br>      <br>      ellipse(x, height - y, 1, 1);<br>    }<br>  }<br>}</pre><p>So this basically draws all previous points for every frame and then increases that count by one each time. Once the count gets above 500 then we start adjusting the xScale to</p><p>And here we have it:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/502/1*-CjWsciwdRqP8knn2MJyBw.png" /></figure><p>Huh? Oh, I forgot to clear the frame for each draw!</p><p>Simply add background(0); to the first line of draw(). Now we get:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fi.imgur.com%2FAgzeonR.mp4&amp;src_secure=1&amp;url=https%3A%2F%2Fi.imgur.com%2FAgzeonR.gifv&amp;image=https%3A%2F%2Fi.imgur.com%2FAgzeonRh.jpg&amp;key=d04bfffea46d4aeda930ec88cc64b87c&amp;type=video%2Fmp4&amp;schema=imgur" width="500" height="500" frameborder="0" scrolling="no"><a href="https://medium.com/media/8ea66e44721c4860d0e07aac0ef244fc/href">https://medium.com/media/8ea66e44721c4860d0e07aac0ef244fc/href</a></iframe><p>Lovely. Now let’s do the same to the y axis.</p><p>This’ll be a tad different as the y value doesn’t increase linearly so we’ll keep a running maximum value and scale according to that. As we already multiply our y-axis by 40 already it naturally leads to us setting:</p><pre>float yScale = 40.0;</pre><p>Place that line just before the void draw() definition so that yScale is remembered between draw() executions.</p><p>Now each time we calculate a new y value we need to check if this exceeds the previous height value, if so then calculate the new yScale:</p><pre>if(y * yScale &gt; height) <br>{<br>  yScale = float(height)/y;<br>}</pre><p>Resulting in:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fi.imgur.com%2FMmzzJis.mp4&amp;src_secure=1&amp;url=https%3A%2F%2Fi.imgur.com%2FMmzzJis.gifv&amp;image=https%3A%2F%2Fi.imgur.com%2FMmzzJish.jpg&amp;key=d04bfffea46d4aeda930ec88cc64b87c&amp;type=video%2Fmp4&amp;schema=imgur" width="500" height="500" frameborder="0" scrolling="no"><a href="https://medium.com/media/9492a746ca198c5c15c175d30d962b41/href">https://medium.com/media/9492a746ca198c5c15c175d30d962b41/href</a></iframe><p>Here’s the entire code:</p><pre>import java.time.*;</pre><pre>FloatDict _data = new FloatDict();<br>LocalDate _startDate = LocalDate.of(1958, 3, 29);</pre><pre>void setup()<br>{<br>  loadData();<br>  size(500,500);<br>  background(0);<br>  stroke(255,255,0);<br>}</pre><pre>float yScale = 40.0;</pre><pre>void draw()<br>{<br>  background(0);<br>  float xScale = 1.0;<br>  <br>  if(frameCount &gt; width)<br>  {<br>    xScale = float(width) / float(frameCount);<br>  }</pre><pre>for(int dataIndex = 1; dataIndex &lt;= frameCount; dataIndex++)<br>  {<br>    int daysFromStart = (dataIndex - 1) * 7;<br>    LocalDate frameDate = _startDate.plusDays(daysFromStart);<br>    <br>    if(_data.hasKey(frameDate.toString()))<br>    {<br>      float co2 = _data.get(frameDate.toString());<br>            <br>      float x = dataIndex;<br>      float y = (co2 - 313.04) ;<br>      <br>      if(y * yScale &gt; height) <br>      {<br>        yScale = float(height)/y;<br>      }<br>      <br>      ellipse(x * xScale, height - (y * yScale), 1, 1);<br>    }<br>  }<br>}</pre><pre>void loadData()<br>{<br>  String[] lines = loadStrings(&quot;weekly_in_situ_co2_mlo.csv&quot;);<br>  <br>  for (String line : lines) <br>  {<br>    if( line.startsWith(&quot;\&quot;&quot;) ) continue;<br>    <br>    String[] values = split(line, &#39;,&#39;);<br>    String date = values[0];<br>    float co2 = parseFloat(values[1]);<br>    _data.set(date, co2);<br>  }<br>}</pre><p>Join me next week when we play with speed and add some text and axis and stuff…</p><p>Edit: When I first published this I had a more complicated way of calculating yScale, this is now simpler.</p><figure><a href="http://bit.ly/HackernoonFB"><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0hqOaABQ7XGPT-OYNgiUBg.png" /></a></figure><figure><a href="https://goo.gl/k7XYbx"><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Vgw1jkA6hgnvwzTsfMlnpg.png" /></a></figure><figure><a href="https://goo.gl/4ofytp"><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gKBpq1ruUi0FVK2UM_I4tQ.png" /></a></figure><blockquote><a href="http://bit.ly/Hackernoon">Hacker Noon</a> is how hackers start their afternoons. We’re a part of the <a href="http://bit.ly/atAMIatAMI">@AMI</a> family. We are now <a href="http://bit.ly/hackernoonsubmission">accepting submissions</a> and happy to <a href="/service/mailto:partners@amipublications.com">discuss advertising &amp; sponsorship</a> opportunities.</blockquote><blockquote>If you enjoyed this story, we recommend reading our <a href="http://bit.ly/hackernoonlatestt">latest tech stories</a> and <a href="https://hackernoon.com/trending">trending tech stories</a>. Until next time, don’t take the realities of the world for granted!</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*35tCjoPcvq6LbB3I6Wegqw.jpeg" /></figure><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fupscri.be%2Fdde502%3Fas_embed%3Dtrue&amp;dntp=1&amp;url=https%3A%2F%2Fupscri.be%2Fhackernoon%2F&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=upscri" width="800" height="400" frameborder="0" scrolling="no"><a href="https://medium.com/media/3c851dac986ab6dbb2d1aaa91205a8eb/href">https://medium.com/media/3c851dac986ab6dbb2d1aaa91205a8eb/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=97e4ee37c4d4" width="1" height="1" alt=""><hr><p><a href="https://medium.com/hackernoon/change-your-scales-97e4ee37c4d4">Change your scales</a> was originally published in <a href="https://medium.com/hackernoon">HackerNoon.com</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Learn your scales]]></title>
            <link>https://medium.com/@kevpluck/learn-your-scales-f78da9bea959?source=rss-a70fd7c25dd8------2</link>
            <guid isPermaLink="false">https://medium.com/p/f78da9bea959</guid>
            <category><![CDATA[tutorial]]></category>
            <category><![CDATA[climate-change]]></category>
            <category><![CDATA[animation]]></category>
            <category><![CDATA[global-warming]]></category>
            <dc:creator><![CDATA[Kevin Pluck]]></dc:creator>
            <pubDate>Sun, 23 Apr 2017 11:25:02 GMT</pubDate>
            <atom:updated>2017-04-29T00:08:36.942Z</atom:updated>
            <content:encoded><![CDATA[<p>This is part 6 of an N part series detailing how I make <a href="https://medium.com/@kevpluck/spirals-and-barrels-364d1dd78175">my animations</a>.</p><p><a href="https://medium.com/@kevpluck/flipping-plots-and-making-dates-cf605a0152e1">Prev</a> <a href="https://medium.com/@kevpluck/change-your-scales-97e4ee37c4d4">Next</a></p><p>In part 5 I left you with an animation that was spatially and chronologically correct yet dreadfully dull. Time to change that.</p><p>First thing’s first let’s set the colour to something other than grey and black. Colours on computers have three components: red, green and blue. You create a colour by mixing different amounts of these primary colours together. So, for example, to get purple you mix 100% red, 0% green and 100% blue. Yellow is 100% red, 100% green and 0% blue. White is all 100%, black is all 0% and grey is all, say, 50%.</p><p>If only it was that simple. Unfortunately the primary colour levels don’t use percentages, instead they use a range from 0 to 255.</p><p>Huh?</p><p>This is because colours are stored in binary and each of the primary colours use one byte of 8 bits each. So 0 is represented as 00000000 and 255 is represented as 11111111. This is the decimal equivalent of, for example, 000 to 999 maximising the use of three available digits.</p><p>So to actually get purple we set the red to 255, green to 0 and blue to 255. Yellow is red at 255, green at 255 and blue at 0. White is all 255, black is all 0 and grey is, for example, all 127.</p><p>Let’s say we want to set the background to black and the dots to yellow, in the setup() function add the following lines:</p><pre>background(0);<br>stroke(255, 255, 0);</pre><p>In Processing if you want to set all the primary colours to the same value, basically setting a shade of grey, you to just use one parameter as I have done to set the background to black.</p><p>Now let’s show some detail by zooming in on the plot. This is done by multiplying the x and y values by some factor¹. Problem with that is even multiplying the CO2 value by 2 instantly plots off screen as it creates a value of &gt; 600. Simple to fix; we just subtract the lowest data value² (313.04) from all the data values before multiplying (I found the best y scale factor to be 40.0).</p><p>So change the ellipse command like so:</p><pre>    float x = frameCount;<br>    float y = (co2 - 313.04) * 40.0;<br>    <br>    ellipse(x, height - y, 1, 1);</pre><p>I separated the x and y values into variables of their own merely to aid reading, the computer doesn’t care but you will when you return to code like this in six months time and struggle to understand what you were thinking at the time!</p><p>The code in its entirety:</p><pre>import java.time.*;</pre><pre>FloatDict _data = new FloatDict();<br>LocalDate _startDate = LocalDate.of(1958, 3, 29);</pre><pre>void setup()<br>{<br>  loadData();<br>  size(500,500);<br>  background(0);<br>  stroke(255,255,0);<br>}</pre><pre>void draw()<br>{<br>  int daysFromStart = (frameCount - 1) * 7;<br>  LocalDate frameDate = _startDate.plusDays(daysFromStart);<br>  <br>  if(_data.hasKey(frameDate.toString()))<br>  {<br>    float co2 = _data.get(frameDate.toString());<br>    <br>    float x = frameCount;<br>    float y = (co2 - 313.04) * 40.0;<br>    <br>    ellipse(x, height - y, 1, 1);<br>  }<br>}</pre><pre>void loadData()<br>{<br>  String[] lines = loadStrings(&quot;weekly_in_situ_co2_mlo.csv&quot;);<br>  <br>  for (String line : lines) <br>  {<br>    if( line.startsWith(&quot;\&quot;&quot;) ) continue;<br>    <br>    String[] values = split(line, &#39;,&#39;);<br>    String date = values[0];<br>    float co2 = parseFloat(values[1]);<br>    _data.set(date, co2);<br>  }<br>}</pre><p>Resulting in:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fi.imgur.com%2FAZ86BTf.mp4&amp;src_secure=1&amp;url=https%3A%2F%2Fi.imgur.com%2FAZ86BTf.gifv&amp;image=https%3A%2F%2Fi.imgur.com%2FAZ86BTf.gif%3Fnoredirect&amp;key=d04bfffea46d4aeda930ec88cc64b87c&amp;type=video%2Fmp4&amp;schema=imgur" width="500" height="500" frameborder="0" scrolling="no"><a href="https://medium.com/media/b84391c5216c9938d4a11ca8ad3f215c/href">https://medium.com/media/b84391c5216c9938d4a11ca8ad3f215c/href</a></iframe><p>My next post will show how to dynamically change the scale so the graph zooms out as the data plots!</p><p>¹ Processing has a function called scale() which does all this for you but unfortunately it also scales the dot you are drawing so you end up plotting stretched dots especially so when we are multiplying the y scale by 40!</p><p>² As an aside the value of 313.04 ppm was measured on 3/10/1954. We will never get back to this value or lower naturally for centuries.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f78da9bea959" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Flipping plots and making dates]]></title>
            <link>https://medium.com/@kevpluck/flipping-plots-and-making-dates-cf605a0152e1?source=rss-a70fd7c25dd8------2</link>
            <guid isPermaLink="false">https://medium.com/p/cf605a0152e1</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[tutorial]]></category>
            <category><![CDATA[climate-change]]></category>
            <category><![CDATA[animation]]></category>
            <category><![CDATA[global-warming]]></category>
            <dc:creator><![CDATA[Kevin Pluck]]></dc:creator>
            <pubDate>Mon, 17 Apr 2017 17:54:09 GMT</pubDate>
            <atom:updated>2017-07-11T16:08:15.781Z</atom:updated>
            <content:encoded><![CDATA[<p>This is part 5 of an N part series detailing how I make <a href="https://medium.com/@kevpluck/spirals-and-barrels-364d1dd78175">my animations</a>.</p><p><a href="https://medium.com/@kevpluck/plotting-data-on-screen-1b0dbfd1d2c4">Prev</a> <a href="https://medium.com/@kevpluck/learn-your-scales-f78da9bea959">Next</a></p><p>In the previous post I mentioned there were two major problems with the animation produced.</p><p>The first one is quite subtle. Notice how the first CO2 concentration value was 316.19 and the height of the output was 500 which means the plot should’ve been well above half way?</p><p>Something fishy is going on and not only that I thought the CO2 concentration was rising? If you squint you can make out that it is dropping! That’s a relief I guess.</p><p>Well the reason is because the origin is located in the top left corner of the output and that positive Y values go down. Basically the output is flipped upside down. The reason this is so is a legacy from how old computers displayed their output on old cathode ray tube screens. In those a beam of electrons was scanned from left to right across the screen starting from the top left corner and ending at the bottom right. It seemed logical at the time that if you wanted to plot a point on the screen then you just counted how many rows had been scanned down from the top and then plot your point accordingly. This turned into measuring the Y value from the top of the screen.</p><p>Unfortunately it’s too late to change computers to be sensible so we have to deal with it everytime we plot points. Simply by subtracting our value from the height of the output. Processing has provided us with a special variable called height to do this. So replace the ellipse command with:</p><pre>ellipse(frameCount, height - co2, 1, 1);</pre><p>Running the sketch should look something like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/502/1*VMyTuamNYAYG2ZMkWc0VTA.png" /></figure><p>Much better.</p><p>Now the second problem is to do with the data itself. Recall I mentioned that due to powercuts and hurricanes there are gaps in the data? In fact you may be able to spot discontinuities in the image above.</p><p>This is going to be quite a challenge to overcome. One way would be to read the date along with the CO2 value, work out how many days since the last CO2 value and shift the X coordinate accordingly.</p><p>This may well work and the gaps will appear correctly in the output but there will be jumps in time creating a chronological discontinuity.</p><p>After much experimentation I have settled on an approach that reduces the amount of logic required and hence the amount of code. Basically we have a starting date and a time interval. For the Keeling curve our start date is 29/03/1958 and our time interval is 7 days. For every frame we simply multiply the frameCount by the time interval, add that to the starting date and see if data exists for that point in time. If so plot a point otherwise go to the next frame.</p><p>The key part to that algorithm is finding out if the data exists for that point in time. We could remember the next point in the array and check if the date at that point matches the expected one. It’ll work, but there’s a much better way.</p><p>Instead of using an array we use something called a <em>dictionary</em>. I personally think this is a stupid name as it instantly invokes images of looking up definitions of words. What’s it got to do with dates and CO2 concentrations?</p><p>In computing a dictionary is a storage mechanism that has keys and values. In traditional dictionaries a key would be the word and the value would be the definition. For our purposes the key will be the date and the value would be the CO2 concentration.</p><p>We simply check to see if the date exists in the dictionary, plot the point if it does or skip this frame if it doesn’t. It’s slightly more complicated to populate the dictionary but now we can filter out things like the informational text and not have to remember where the data starts in the array.</p><p>So change the definition of _data near the top of the code to:</p><pre>FloatDict _data = new FloatDict();</pre><p>A FloatDict is a dictionary who’s values are floating point real numbers. Replace the contents of loadData() with:</p><pre>  String[] lines = loadStrings(&quot;weekly_in_situ_co2_mlo.csv&quot;);<br>  <br>  for (String line : lines) <br>  {<br>    if( line.startsWith(&quot;\&quot;&quot;) ) continue;<br>    <br>    String[] values = split(line, &#39;,&#39;);<br>    String date = values[0];<br>    float co2 = parseFloat(values[1]);<br>    _data.set(date, co2);<br>  }</pre><p>The code for (String line : lines) says <em>for each </em>line <em>in the array </em>lines<em> do this.</em></p><p>The code if( line.startsWith(&quot;\&quot;&quot;) ) continue; skips any line that begins with the character &quot;. Unfortunately the very character we are checking to skip is the same character that is used to denote a string in the code! So to get round this we use a special character \ to “escape” the &quot; from the string. This character is called the “escape character”. This’ll probably make more sense in the following example:</p><pre>&quot;Fred said \&quot;Oh dear, I must escape from here.\&quot; but it was all for nought&quot; </pre><p>The code _data.set(date, co2) adds a the CO2 value to the dictionary with the date as the key. This now means we can check to see if the date exists by executing _data.hasKey(&lt;some date&gt;) and getting the value with _data.get(&lt;some date&gt;).</p><p>Now we need to be able to manipulate dates. Unfortunately Processing has very poor date handling functionality, this isn’t a problem though, we simply import a date handling library, so at the top of the code add:</p><pre>import java.time.*;</pre><p>That’s right, you’ve been coding in Java all this time!</p><p>In the same area of the code where FloatDict _data is defined add:</p><pre>LocalDate _startDate = LocalDate.of(1958, 3, 29);</pre><p>Now for each frame all we need to do is work out the number of days from the start date, add them to the start date, check to see if data exists for that date and plot it!</p><p>To work out the number of days since the start:</p><pre>int daysFromStart = (frameCount - 1) * 7;</pre><p>For frame #1 the days from the start will be 0. For frame #2 the days from the start will be 7 etc.</p><p>To add that number of days to the start date:</p><pre>LocalDate frameDate = _startDate.plusDays(daysFromStart);</pre><p>To check if any data exists for the frameDate :</p><pre>if(_data.hasKey( frameDate.toString() ))</pre><p>toString() outputs the date in the format yyyy-mm-dd which is luckily the same format as the data!</p><p>To get the data:</p><pre>float co2 = _data.get(frameDate.toString());</pre><p>In total:</p><pre>import java.time.*;</pre><pre>FloatDict _data = new FloatDict();<br>LocalDate _startDate = LocalDate.of(1958, 3, 29);</pre><pre>void setup()<br>{<br>  loadData();<br>  size(500,500);<br>}</pre><pre>void draw()<br>{<br>  int daysFromStart = (frameCount - 1) * 7;<br>  LocalDate frameDate = _startDate.plusDays(daysFromStart);<br>  <br>  if(_data.hasKey( frameDate.toString() ))<br>  {<br>    float co2 = _data.get(frameDate.toString());<br>    ellipse(frameCount, height - co2, 1, 1);<br>  }<br>}</pre><pre>void loadData()<br>{<br>  String[] lines = loadStrings(&quot;weekly_in_situ_co2_mlo.csv&quot;);<br>  <br>  for (String line : lines) <br>  {<br>    if( line.startsWith(&quot;\&quot;&quot;) ) continue;<br>    <br>    String[] values = split(line, &#39;,&#39;);<br>    String date = values[0];<br>    float co2 = parseFloat(values[1]);<br>    _data.set(date, co2);<br>  }<br>}</pre><p>Which produces:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fi.imgur.com%2F4WoUoWH.mp4&amp;src_secure=1&amp;url=https%3A%2F%2Fi.imgur.com%2F4WoUoWH.gifv&amp;image=https%3A%2F%2Fi.imgur.com%2F4WoUoWH.gif%3Fnoredirect&amp;key=d04bfffea46d4aeda930ec88cc64b87c&amp;type=video%2Fmp4&amp;schema=imgur" width="500" height="500" frameborder="0" scrolling="no"><a href="https://medium.com/media/78abab392a847af698b51c583af8d591/href">https://medium.com/media/78abab392a847af698b51c583af8d591/href</a></iframe><p>Hurray! Our gaps are there and the animation doesn’t have chronological discontinuities!</p><p><a href="https://medium.com/@kevpluck/learn-your-scales-f78da9bea959">Now let’s make this more compelling…</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=cf605a0152e1" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Patience now.]]></title>
            <link>https://medium.com/@kevpluck/patience-now-25d7e59a8c0a?source=rss-a70fd7c25dd8------2</link>
            <guid isPermaLink="false">https://medium.com/p/25d7e59a8c0a</guid>
            <dc:creator><![CDATA[Kevin Pluck]]></dc:creator>
            <pubDate>Sun, 16 Apr 2017 22:33:12 GMT</pubDate>
            <atom:updated>2017-04-16T22:33:12.955Z</atom:updated>
            <content:encoded><![CDATA[<p>These things take time :-)</p><p>You can prod me into action on <a href="https://twitter.com/kevpluck">twitter</a> or follow me here.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=25d7e59a8c0a" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Plotting data on screen]]></title>
            <link>https://medium.com/@kevpluck/plotting-data-on-screen-1b0dbfd1d2c4?source=rss-a70fd7c25dd8------2</link>
            <guid isPermaLink="false">https://medium.com/p/1b0dbfd1d2c4</guid>
            <category><![CDATA[animation]]></category>
            <category><![CDATA[climate-change]]></category>
            <category><![CDATA[code]]></category>
            <category><![CDATA[tutorial]]></category>
            <category><![CDATA[global-warming]]></category>
            <dc:creator><![CDATA[Kevin Pluck]]></dc:creator>
            <pubDate>Sun, 16 Apr 2017 22:32:49 GMT</pubDate>
            <atom:updated>2017-04-17T17:57:28.989Z</atom:updated>
            <content:encoded><![CDATA[<p>This is part 4 of an N part series detailing how I make <a href="https://medium.com/@kevpluck/spirals-and-barrels-364d1dd78175">my animations</a>.</p><p><a href="https://medium.com/@kevpluck/data-from-files-7e1e202f366b">Prev</a> <a href="https://medium.com/@kevpluck/flipping-plots-and-making-dates-cf605a0152e1">Next</a></p><p><a href="https://medium.com/@kevpluck/data-from-files-7e1e202f366b">Now that we have the data loaded</a> into a string array we can start doing something with it.</p><p>Delete the println command from setup() and in the draw() function add println(frameCount); Running that should result in incrementing numbers whizzing by in the console. frameCount is a special variable created for us by Processing which starts at 1 and increments by 1 everytime draw() is executed.</p><p>Replace the contents of draw() with</p><pre>int dataIndex = frameCount + 43;<br>String frameData = _data[dataIndex];<br>println(frameData);</pre><p>You should now see all the data whizzing by in the console. Because frameCount starts at 1 and _data is zero based then adding 43 to frameCount starts at line 45 of the original file¹.</p><p>Now we want to be able to access the date and CO2 concentrations separately, to do that we use the split() function. The split function takes as parameters a string you wish to split and a character to split with. It returns an array of strings where each string was separated by the character.</p><p>So for example</p><pre>String[] values = split(&quot;apples,oranges,pears&quot;, &#39;,&#39;);</pre><p>will result in values containing three strings: apples, oranges and pears. Notice that , is wrapped by &#39; and not &quot;. This is because an individual character is a special data type itself and is denoted by single quotes².</p><p>Replace the println command with</p><pre>String[] values = split(frameData, &#39;,&#39;);<br>println(values[0]);<br>println(values[1]);</pre><p>You can now see that the date is in values[0] and the CO2 concentration is in values[1].</p><p>We have finally got to accessing the individual values but unfortunately we can’t do much other than displaying the value. To be able to graph them we have to convert the string representation of the CO2 concentration into a number the computer can understand. As the concentration is not an integer but a real number we logically need to convert it into a float. Real numbers are technically known as “floating point numbers” as the decimal point can float around depending on the significant digits of the number.</p><p>So on the line after the split command add</p><pre>float co2 = parseFloat(values[1]);</pre><p>parseFloat() converts the string representation of the CO2 concentration to a floating point real number. This now means that we can plot some points! But first set the size of the animation by adding size(500,500); to the setup() function.</p><p>Add</p><pre>ellipse(frameCount, co2, 1, 1);</pre><p>The ellipse function takes four float values³ X, Y, Width, Height. The width and height are set to 1 pixel so we simply draw a dot. Having frameCount as X means that the dot is drawn one pixel further to the right for each frame and co2 is the value for Y.</p><p>Your code should look something like this</p><pre>String[] _data;</pre><pre>void setup()<br>{<br>  loadData();<br>  size(500,500);<br>}</pre><pre>void draw()<br>{<br>  int dataIndex = frameCount + 43;<br>  String frameData = _data[dataIndex];<br>  String[] values = split(frameData, &#39;,&#39;);<br>  <br>  float co2 = parseFloat(values[1]);<br>  <br>  ellipse(frameCount, co2, 1, 1);<br>}</pre><pre>void loadData()<br>{<br>  _data = loadStrings(&quot;weekly_in_situ_co2_mlo.csv&quot;);<br>}</pre><p>Running that should produce this:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fi.imgur.com%2FYMIcuoI.mp4&amp;src_secure=1&amp;url=https%3A%2F%2Fi.imgur.com%2FYMIcuoI.gifv&amp;image=https%3A%2F%2Fi.imgur.com%2FYMIcuoI.gif%3Fnoredirect&amp;key=d04bfffea46d4aeda930ec88cc64b87c&amp;type=video%2Fmp4&amp;schema=imgur" width="500" height="500" frameborder="0" scrolling="no"><a href="https://medium.com/media/e8bfcf55f79f4ab2701d985b839a5f0b/href">https://medium.com/media/e8bfcf55f79f4ab2701d985b839a5f0b/href</a></iframe><p>There are two major problems with this output (other than it being the most dull looking thing imaginable). See if you can figure them out before heading to the <a href="https://medium.com/@kevpluck/flipping-plots-and-making-dates-cf605a0152e1">next post</a>.</p><p>¹ You can see why even seasoned developers trip up! There are four hard problems in computing; naming things, cache invalidation and off-by-one errors.</p><p>² Can be confusing as you can also have strings with just an individual character. You can consider a String as an array of character data types, in fact in some languages (not Processing unfortunately) you can access a particular character in a string like so print(&quot;Hello&quot;[1]); will print “e”.</p><p>³ Integer variables, such as frameCount are automatically converted to float variables by Processing.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1b0dbfd1d2c4" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>