Create a sketch that starts with characters randomly scattered (and rotated). Have them slowly move back toward their “home” location. Use an object-oriented approach as seen in Example 17-6.
// Learning Processing
// Daniel Shiffman
// http://www.learningprocessing.com
// Exercise 17-10: Create a sketch that starts with characters randomly
// scattered (and rotated). Have them slowly move back toward their "home"
// location. Use an object-oriented approach as seen in Example 17-6.
PFont f;
String message = "random characters flying home!";
// An array of Letter objects
Letter[] letters;
void setup() {
size(400, 200);
// Load the font
f = createFont("Georgia",20,true);
textFont(f);
// Create the array the same size as the String
letters = new Letter[message.length()];
// Initialize Letters at the correct x location
int x = 50;
for (int i = 0; i < message.length(); i++) {
letters[i] = new Letter(x,height/2,message.charAt(i));
x += textWidth(message.charAt(i));
}
}
void draw() {
background(255);
for (int i = 0; i < letters.length; i++) {
// Display all letters
letters[i].display();
// If the mouse is pressed the letters shake
// If not, they return to their original location
if (mousePressed) {
letters[i].shake();
} else {
letters[i].home();
}
}
}
// Learning Processing
// Daniel Shiffman
// http://www.learningprocessing.com
// A class to describe a single Letter
class Letter {
char letter;
// The object knows its original "home" location
float homex,homey;
// As well as its current location
float x,y;
// And an angle of rotation
float theta;
Letter (float x_, float y_, char letter_) {
homex = x = x_;
homey = y = y_;
x = random(width);
y = random(height);
theta = random(TWO_PI);
letter = letter_;
}
// Display the letter
void display() {
fill(0);
textAlign(LEFT);
// User translate and rotate to draw the letter
pushMatrix();
translate(x,y);
rotate(theta);
text(letter,0,0);
popMatrix();
}
// Move the letter randomly
void shake() {
x += random(-2,2);
y += random(-2,2);
theta += random(-0.5,0.5);
}
// Return the letter home using lerp!
void home() {
x = lerp(x,homex,0.05);
y = lerp(y,homey,0.05);
theta = lerp(theta,0,0.05);
}
}








