nfg_Day9

Day 9
<< main | course director notes

code int numCircles = 100; float[] posX = new float[numCircles]; float[] posY = new float[numCircles]; float[] sizes = new float[numCircles]; color[] circleColor = new color[numCircles]; float drift = 0; float driftDelta = 0.001; boolean hover = false;
 * 4. Interactivity and arrays**

void setup{ size(640,480); smooth; ellipseMode(CENTER); for(int i=0;i < numCircles; i++){ posX[i] = random(width); posY[i] = random(height); sizes[i] = random(10,20); circleColor[i] = color(random(255),random(255),random(255)); } }

void draw{ background(128); for(int i=0; i< numCircles; i++){ if(hitDetect(posX[i],posY[i],sizes[i],sizes[i],mouseX,mouseY,5,5)){ hover=true; }else{ hover = false; }     posX[i] += random(drift) - random(drift); posY[i] += random(drift) - random(drift);

if(hover){ fill(255); ellipse(posX[i],posY[i],sizes[i]+10,sizes[i]+10); }else{ fill(circleColor[i]); ellipse(posX[i],posY[i],sizes[i],sizes[i]); }

if(mousePressed){ drift += driftDelta; }else{ drift -= driftDelta; if(drift<0){ drift=0; }     }      println(drift); } }

boolean hitDetect(float x1, float y1, float w1, float h1, float x2, float y2, float w2, float h2) { w1 /= 2; h1 /= 2; w2 /= 2; h2 /= 2; if(x1 + w1 >= x2 - w2 && x1 - w1 <= x2 + w2 && y1 + h1 >= y2 - h2 && y1 - h1 <= y2 + h2) { return true; }  else { return false; } } code

code int numCircles = 100; float[] posX = new float[numCircles]; float[] posY = new float[numCircles]; float[] sizes = new float[numCircles]; float drift = 0; float driftDelta = 0.001;
 * 3. Creating a large array with loops**

void setup{ size(640,480); smooth; ellipseMode(CENTER); for(int i=0;i < numCircles; i++){ posX[i] = random(width); posY[i] = random(height); sizes[i] = random(10,20); } }

void draw{ background(128); for(int i=0; i< numCircles; i++){ posX[i] += random(drift) - random(drift); posY[i] += random(drift) - random(drift); ellipse(posX[i],posY[i],sizes[i],sizes[i]); if(mousePressed){ drift += driftDelta; }else{ drift -= driftDelta; if(drift<0){ drift=0; }     }      println(drift); } }

code

code float[] posX = { 320, 100, 200, 40, 50 }; float[] posY = { 240, 230, 200, 30, 40 };
 * 2. Making a small array by hand**

void setup{ size(640,480); ellipseMode(CENTER); }

void draw{ background(128); for(int i=0; i< posX.length; i++){ ellipse(posX[i],posY[i],10,10); posX[i]++; posY[i]--; } }

code

code float posX = 320; float posY = 240;
 * 1. Array example--before we add arrays**

void setup{ size(640,480); }

void draw{ background(128); ellipseMode(CENTER); ellipse(posX,posY,10,10); posX++; posY--; }

code