More polish

Kevin Pluck
HackerNoon.com
6 min readMay 21, 2017

--

This is part 10 of an N part series detailing how I make my animations.

Prev Next

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.

The y-axis is the concentration of CO2 measured in PPM (parts per million) with a range of about 310–400 PPM.

As an aside; to comprehend those figures here’s an image of what 400 PPM looks like:

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.

First of all we need a variable to record the maximum CO2 value:

Pop that just before the for loop in drawGraph().

Just after where the CO2 value is extracted in the for loop add:

After the for loop add:

That’ll get red squigglies underneath it as we haven’t created drawYAxis() yet. So let’s do that just after drawGraph():

I think a tick mark starting at 320PPM showing every 10PPM should suffice so let’s set the start value:

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:

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:

That’s better:

The line co2Tick += 10.0 is a shorthand way of incrementing a variable by 10. Equivalent to co2Tick = co2Tick + 10.0.

Let’s give that a whirl:

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():

Resulting in:

Much better.

Now we need to label the axes. The x axis is straight forward:

But the y axis needs to be rotated. Brace yourself as this involves some matrix manipulation. 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().

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.

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 stack is? Here we go.) 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().

So just before drawYAxis() add:

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.

After titling the main graph we get:

And the now rather long and complete code:

Next week I’ll show you how to record videos and generate gifs along with hosting them on the internet.

Hacker Noon is how hackers start their afternoons. We’re a part of the @AMI family. We are now accepting submissions and happy to discuss advertising & sponsorship opportunities.

If you enjoyed this story, we recommend reading our latest tech stories and trending tech stories. Until next time, don’t take the realities of the world for granted!

--

--