Lecture 08
Arrays.
A case where the arrays are set with specific values at the start:
float xPoints[] = {
280.0, 289.0, 134.0, 64.0, 151.0
};
float yPoints[] = {
74.0, 205.0, 260.0, 194.0, 61.0
};
void setup() {
size (400, 400);
smooth();
}
void draw() {
background(126);
beginShape();
for (int i=0; i<5; i++) {
float px = xPoints[i] + 30*noise(i + millis()/200.0);
float py = yPoints[i] + 30*noise(i + millis()/313.0);
vertex(px,py);
}
endShape(CLOSE);
}
“Flocking” simulation — actually, 100 drunks:
float pxArray[]; // declaration
float[] pyArray;
int nDrunks = 100;
void setup() {
size(400, 400);
pxArray = new float[nDrunks]; // ALLOCATION
pyArray = new float[nDrunks];
for (int i=0; i < ndrunks ; i++) {
pxArray[i] = width/2; // assignment
pyArray[i] = height/2;
}
}
void updateFlockingSimulation() {
for (int i=0; i < nDrunks; i++) {
pxArray[i] += random(-4, 4);
pyArray[i] += random(-4, 4);
}
}
void renderFlockingSimulation() {
for (int i=0; i < nDrunks; i++) {
ellipse(pxArray[i], pyArray[i], 20, 20);
}
}
void draw() {
background(128);
updateFlockingSimulation();
renderFlockingSimulation();
}
A Point Recorder:
ArrayList xPoints;
ArrayList yPoints;
void setup() {
size(400, 400);
xPoints = new ArrayList();
yPoints = new ArrayList();
}
void draw() {
background(200);
noFill();
int n = xPoints.size();
beginShape();
for (int i=0; i < n ; i++) {
float px = ((Float)(xPoints.get(i)));
float py = ((Float)(yPoints.get(i)));
vertex(px, py);
}
endShape();
}
void mousePressed() {
xPoints.add((float)mouseX);
yPoints.add((float)mouseY);
}
void keyPressed() {
print("float xPoints[] = {");
int nx = xPoints.size();
for (int i=0; i < nx; i++) {
float px = ((Float)(xPoints.get(i)));
if (i != (nx-1)) {
print (px + ", ");
} else {
print (px);
}
}
println("};");
print("float yPoints[] = {");
int ny = yPoints.size();
for (int i=0; i < ny; i++) {
float py = ((Float)(yPoints.get(i)));
if (i != (ny-1)) {
print (py + ", ");
} else {
print (py);
}
}
println("};");
}
More on Transforms.
A Rotating Square: Two ways, different advantages.
int drawState = 0;
void setup() {
size (300, 300);
smooth();
}
void keyPressed() {
drawState = (drawState+1)%3;
}
//=========================================================
void draw() {
fill(255);
text("Draw State = " + drawState, 15, 20);
switch (drawState) {
case 0:
draw0();
break;
case 1:
draw1();
break;
case 2:
draw2();
break;
}
fill(100, 64);
rect(0, 0, width, height);
}
//=========================================================
void draw0() {
// Use rotate and translate to compute the vertices for us.
pushMatrix();
translate(width/2, height/2);
rotate (millis()/400.0);
rect (-50, -50, 100, 100);
popMatrix();
}
//=========================================================
void draw1() {
// Compute the vertices mathematically, ourselves.
float cx = width/2;
float cy = height/2;
float T = millis()/400.0 - QUARTER_PI;
float r = 50.0 * sqrt(2.0);
beginShape();
vertex(cx + r*cos(T ), cy + r*sin(T ));
vertex(cx + r*cos(T+HALF_PI*1), cy + r*sin(T+HALF_PI*1));
vertex(cx + r*cos(T+HALF_PI*2), cy + r*sin(T+HALF_PI*2));
vertex(cx + r*cos(T+HALF_PI*3), cy + r*sin(T+HALF_PI*3));
endShape(CLOSE);
}
//=========================================================
void draw2() {
// Compute the vertices mathematically, ourselves.
float cx = width/2;
float cy = height/2;
float T = millis()/400.0 - QUARTER_PI;
float r = 50.0 * sqrt(2.0);
float dx = 15*sin(millis()/50.0);
float dy = 15*cos(millis()/50.0);
float dT = 0.4 * sin(millis()/80.0);
beginShape();
vertex(cx + r*cos(T+dT ), cy + r*sin(T+dT ));
vertex(cx + r*cos(T+HALF_PI*1), cy + r*sin(T+HALF_PI*1));
vertex(cx + r*cos(T+HALF_PI*2) + dx, cy + r*sin(T+HALF_PI*2) + dy);
vertex(cx + r*cos(T+HALF_PI*3), cy + r*sin(T+HALF_PI*3));
endShape(CLOSE);
}
Limbs: Here’s an Arm
void setup() {
size(500, 500);
}
void draw() {
background(100);
fill(255,255,255, 128);
smooth();
pushMatrix();
translate(150, 150);
ellipse(0,0, 10,10);
rect(0,0, 100, 100);
translate(75,75);
ellipse(0,0, 10,10);
rect(0,0, 50,150);
translate(25,125);
ellipse(0,0, 10,10);
rect(0,0, 50,50);
popMatrix();
}
Limbs: Here’s an Arm, Moving
void setup() {
size(500, 500);
}
void draw() {
background(100);
fill(255,255,255, 128);
smooth();
float ang1 = 0.5 * (sin(millis()/2000.0));
float ang2 = 0.7 * (sin(millis()/1111.0));
float ang3 = 1.2 * (sin(millis()/ 313.0));
pushMatrix();
translate(150, 150);
rotate( ang1 );
ellipse(0,0, 10,10);
rect(0,0, 100, 100);
translate(75,75);
rotate( ang2 );
ellipse(0,0, 10,10);
rect(0,0, 50,150);
translate(25,125);
rotate( ang3 );
ellipse(0,0, 10,10);
rect(0,0, 50,50);
popMatrix();
}
Noisy Limb:
void setup() {
size(500, 500);
}
void draw() {
background(100);
fill(255,255,255, 128);
smooth();
float ang1 = 0.5 * (noise(millis()/2000.0) - 0.5);
float ang2 = 0.7 * (noise(millis()/1111.0) - 0.5);
float ang3 = 1.2 * (noise(millis()/ 313.0) - 0.5);
pushMatrix();
translate(150, 150);
rotate( ang1 );
ellipse(0,0, 10,10);
rect(0,0, 100, 100);
translate(75,75);
rotate( ang2 );
ellipse(0,0, 10,10);
rect(0,0, 50,150);
translate(25,125);
rotate( ang3 );
ellipse(0,0, 10,10);
rect(0,0, 50,50);
popMatrix();
}