7. Various bits

Constants

Constants look almost exactly like variables, but you can’t modify their content. With the keyword final a variable will turn into a constant, after which you can’t reassing new values to it. It is perfectly ok to use the value, though:

final int MYNUMBER=10;
int a;

a=MYNUMBER; // We can do this
MYNUMBER=11; // But not this

It’s a matter of taste and style, but writing constant names in ALL CAPS makes it immediately evident that they are not variables. But why do we need these constants? Some examples are fixed values that should never change, like PI. Another typical use is to replace numbers, such as screen coordinates or array sizes with a descriptively named constant that can be reassigned as needed in one go, instead of touching the code everywhere.

Building a little toolbox

Let’s put functions into good use again and build a couple of useful functions that will probably come in handy for your assignment, too. toolbox.pde

Circular and oscillating motion

Time to go back to your high school math and use sin/cos for drawing circles, ellipses and spirals. cos(angle) gives the x coordinate for a circle and sin(angle) the y coordinate. A couple of things to note: a full circle is 2*PI – Processing uses radians instead of angles. Another quirk is that, unlike with mathematical coordinates, the y axis points down, not up. circularmotion.pde

This entry was posted in Processing. Bookmark the permalink.