Comparing vvvv and Processing, part 4.
Hello guys!
I’ve seen that my previous articles have had some kind of success as you’ve been liking and re-posting them. I’m glad you like the work I’m doing here, and it makes me want to go further in this article series.
In the last article, we made a brief but exhaustive walkthrough to the key points you need to take into account deciding which of these 2 great tools should you use in your project (and even consider some other tool like openFrameworks, pure Data, maxNSP, etc.). So this has been enough theory and blah blah for this article series. From now on, we’re going into the practical articles and stuff.
In this article, I’m going to start by proposing a pretty straight-forward interactive application with mouse input and simple animations. The purpose of this simple “hello world” kind of application is to show you how things move in both Processing and vvvv, and having in mind the other key points I’ve discussed in the previous articles, this will allow you to decide which tool is your favorite or the most fit to your own needs.
So, without further ado, let’s start with processing!
Foreword: what we’re going to do!
The super-awesome-and-innovative interactive application we’re going to create is just a simple 5x5 grid of squares with red and green colors. These squares are reactive to the mouse position, and they change their dimensions according to the mouse’s distance from them. The higher the distance, the greater the dimensions are and vice versa. Check out the online example here so you can see by yourself the effect. Is very simple, but that’s the way everyone starts with any kind of tool, and is also simpler to compare our tools with simple examples.
Part 4: let’s start sketching with processing.
Step 1: We have to think before we even write a semicolon!
This is a very simple application that can be entirely done without frying our brains. So the first thing I’m going to consider here is that I’m not going to use any objects or classes as all the functionality can be achieved with pretty easy and straight-forward algorithms.
Having made that clear, let’s start by making some abstract thinking, and in this part of the process, it’s pretty handy to grab your pencil and paper and make some sketching. It would seem very dumb or simple but this step is crucial and you have to do it before writing any code. Just imagine that you thought things up quickly and you wrote your code and then you realize your algorithm was wrong! Now in this case that is not much of a problem, but if you have 2000 lines of code with complicated relations, loops, conditionals, etc. it won’t be that funny right?
So, after I finished my speech, let’s do some abstract thinking: we have 25 squares that have the same size and are equally apart from each other, and they are arranged in a 5x5 grid. When it comes to these problems, we start by a simple question: what is variable in the program? What is going to change? In our case, it’s the squares’ dimensions primarily, and the second variable is the squares’ color, and they appear in a pattern: green, red, green, red, etc.
Now to our question number 2: how to store our data so it is logically related to what is seen? As we won’t use OOP, let’s find a programming structure that allows us to handle nxn grid intuitively and that stores our data accordingly. In our case, we are going to use an array2D, and this array will store our squares’ dimensions, and not the position as you might think, ‘cause they won’t change and we’ll calculate them as we draw our shapes.
Finally the question number 3: how are we going to achieve the effect? What is the logic and procedures to accomplish it? So the first thing is to point out we’ll do it with the mouse movement. Notice that as we get close to a specific square, it gets smaller and as we move away it gets bigger until it reaches the original or maximum dimension. Now every square has an (x, y) coordinate in the window as well as the mouse (refer to the processing documentation for more info if you’re a newbie in this), so how are we going to calculate the distance between a pair of two-dimensional points? Basic algebra guys: the distance formula!!!

the distance formula for the two-dimensional space. Extracted from about.com
Now for the second part of our analysis: we have our distance, but we can’t just assign it directly to our square, as it would be enormous! So the solution to this problem is to have a length interval. We set a specific and finite interval from the minimum to the maximum distance, so we exclude the data we don’t care. Next, we have to map that data to another desired interval, which will be the change factor for the squares’ dimensions.
This factor will be a decimal number between 0 and 1, cause with those the scaling can be made in pretty-straight-forward fashion. Remember that the scale transformation is made by taking a point in the Cartesian plane and multiply it by a constant k, so if we multiply by 1, we return the square to its former dimension, and if we multiply by 0, it’ll disappear. See the image below to make things clearer.

Wrapping things up: First, we have to calculate the Euclidean distance between the squares’ origin and the mouse. Second, we constrain and place that distance in our allowed distances interval. Third, we take that place or location from the distances’ interval to the dimensions’ change factor interval (that will be between 0 and 1) and finally we take the original dimensions from the square and multiply them for that change factor.
It’s a very easy algorithm, and the same effect can be achieved with even a simpler one, but the main point here is to emphasize the design and analysis process, as it is very important when it comes to develop any kind of application, no matter if you’re on processing or vvvv.
So let’s write some code! If you’re an experimented programmer, feel free to jump the steps and see the final code at the end of the article.
Step 2: write some code!
Let’s start by placing our very basic skeleton for our sketch. As we’ll be using animation and changing in time, we have to use the setup() and draw() methods. Explaining briefly, the first one is executed only once in the program and there you’ll initialize your values, create objects, define your window’s size, and so on; and the second one will be executed in every frame of the program, so here you draw your shapes, your objects, you do the logical changes so the animations can happen, etc. Processing runs at 60 frames per second by default.
void setup(){
}
void draw(){
}
//executed every time you move the mouse in the window.
void mouseMoved(){
}
Additionally, we’ll place the mouseMoved() method, that handles the mouse move event: every time you move the mouse inside the application’s window, this method will be executed, so here is where we’ll make our logical processes for the interaction.
Step 3: defining our variables!
static final color RED_COLOR = #FF0000; static final color GREEN_COLOR = #00FF00; static final int GRID_SIZE = 5; static final float INIT_WIDTH = 50.0f; float iniX, iniY; float[][] widths;
These variable are pretty self-explanatory. The first 4 are constants, as they won’t change during the program and are reference values. The iniX and iniY variables simply store the initial location for the grid to start drawing, and they’re important as we do some calculation with them. Finally, the array2D variable we spoke of at step 1.
Step 4: setting it up!
Let’s fill our setup method now.
void setup()
{
size(500, 500);
widths = new float[GRID_SIZE][GRID_SIZE];
iniX = iniY = 100;
fillGridWithWidths();
rectMode(CENTER);
smooth();
}
This chunk of code is pretty straight forward as we just initialize our variables, we create our array2D with the specified dimensions (5x5) and blah blah. The only weird thing here is the fillGridWithWiths() method. This method fills the entire array 2D with our INIT_WIDTH value. Copy and paste the following code to the end of your sketch:
void fillGridWithWidths()
{
for (int i = 0; i < GRID_SIZE; i++) {
Arrays.fill(widths[i], INIT_WIDTH);
}
}
Here I just used the Arrays.fill(int[] target, int value) method that fill every row of the array2D with the value specified in the second parameter.
Step 5: drawing our squares!
So let’s get to me drawing. Copy and paste this code:
void draw()
{
background(0);
color currentCol = RED_COLOR;
for (int i = 0; i < GRID_SIZE; i++) {
for (int j = 0; j < GRID_SIZE; j++) {
currentCol = (currentCol == GREEN_COLOR) ? RED_COLOR : GREEN_COLOR;
float x = iniX + i*70;
float y = iniY + j*70;
fill(currentCol);
noStroke();
rect(x, y, widths[i][j], widths[i][j]);
}
}
}
First, I refresh the background every frame (or else you’ll see some ghostly square when you animate). Next, I define a local variable called currentCol, and with it we’ll decide what color will correspond to each square, and we set it to the red color first. Now we use a nested loop to get every single cell from our array 2D and we do the following: first get the current color (by a sinple line conditional), second we calculate the corresponding position for the cell, and finally we draw our square with the rect() method with the right positions and the particular size that corresponds to each cell.
By this point, if you hit the “run” button, you should see something like this:

Step 6: the logic and fun part from this article!
Copy and paste the following code into the mouseMoved() method:
void mouseMoved()
{
for (int i = 0; i < GRID_SIZE; i++) {
for (int j = 0; j < GRID_SIZE; j++) {
float x = iniX + i*70;
float y = iniY + j*70;
float d = dist(x, y, mouseX, mouseY);
d = constrain(d, 0, INIT_WIDTH * 3.0f);
float dfactor = map(d, 0, INIT_WIDTH * 3.0f, 0, 1);
widths[i][j] = INIT_WIDTH * dfactor;
}
}
}
This is the code that does the exact same thing described in step 1. We loop through our array2D, recalculate the squares’ positions and we get the distance with the dist() method using the mouse positions. Then we constrain that distance between 0 and 3 times the initial width (as if we get any further, we don’t want make a huge square that fill the entire window) and then we calculate the change factor by mapping this distance from the previous interval from 0 to 1. Finally, assign the current position of the array2D to the multiplication of the initial width by the factor.
Step 7: voilà! Enjoy you super-entertaining application!
And this is the final code:
static final color RED_COLOR = #FF0000;
static final color GREEN_COLOR = #00FF00;
static final int GRID_SIZE = 5;
static final float INIT_WIDTH = 50.0f;
float iniX, iniY;
float[][] widths;
void setup()
{
size(500, 500);
widths = new float[GRID_SIZE][GRID_SIZE];
iniX = iniY = 100;
fillGridWithWidths();
rectMode(CENTER);
smooth();
}
void draw()
{
background(0);
color currentCol = RED_COLOR;
for (int i = 0; i < GRID_SIZE; i++) {
for (int j = 0; j < GRID_SIZE; j++) {
currentCol = (currentCol == GREEN_COLOR) ? RED_COLOR : GREEN_COLOR;
float x = iniX + i*70;
float y = iniY + j*70;
fill(currentCol);
noStroke();
rect(x, y, widths[i][j], widths[i][j]);
}
}
}
//..................................................
void mouseMoved()
{
for (int i = 0; i < GRID_SIZE; i++) {
for (int j = 0; j < GRID_SIZE; j++) {
float x = iniX + i*70;
float y = iniY + j*70;
float d = dist(x, y, mouseX, mouseY);
d = constrain(d, 0, INIT_WIDTH * 3.0f);
float dfactor = map(d, 0, INIT_WIDTH * 3.0f, 0, 1);
widths[i][j] = INIT_WIDTH * dfactor;
}
}
}
void fillGridWithWidths()
{
for (int i = 0; i < GRID_SIZE; i++) {
Arrays.fill(widths[i], INIT_WIDTH);
}
}
Final words
Pretty easy huh? Now you see why processing is so popular! It is very easy to create something like this just a few lines of code, and with only 10 minutes you’re ready to go!
In the next, article I’m going to replicate this application using vvvv, and I promise that one won’t be as long as this one :P
See ya later ‘alligators!!!









extracted from: the illustrated guide for vvvv for newbies in computer arts.




