Vectors
Chapter 1 introduces vectors. I’ve looked into these at various points in my life, but always appreciate a refresher. I don’t usually need them for my work or hobbies. There have been execptions to this and it’s nice to imagine that doing the course will raise the amount of maths I use daily in the future.
Exercise 1.1
Find something you’ve previously made in Processing using separate x and y variables and use PVectors instead.
The course assumes the learner has done some work, or even a course, with Processing before. I looked into the tool years ago, but now only have the course to lean on. I picked the “Gaussian paint splatter” exercise from the previous chapter.
The code only uses one set of x and y co-ordinates, and immediately passes them on to the ellipse()
function. Adding vectors to this is overkill, but the point of the exercise is to practice using the PVector
class, not to write sensible code.
import java.util.Random;
Random generator;
int red, green, blue;
void ellipseVector( PVector location, float x_rad, float y_rad ) {
ellipse( location.x, location.y, x_rad, y_rad );
}
void setup() {
size(640,360);
noStroke();
generator = new Random();
}
float gaussian( float mu, float sigma ) {
float num = (float) generator.nextGaussian();
float ret = sigma * num + mu;
return ret;
}
void draw() {
if( !mousePressed ) return;
PVector location = new PVector( gaussian( mouseX, 10 ), gaussian( mouseY, 10 ) );
ellipseVector( location, 8, 8 );
}
void mousePressed() {
red = (int) gaussian( 125, 60 );
green = (int) gaussian( 125, 60 );
blue = (int) gaussian( 125, 60 );
fill( red, green, blue, 80 );
}
The course text pointed out that ellipse()
wont take a vector as a location parameter, so I wrote a wrapper function which would.