For this project, I created several different arrays that duplicated the number of instances my smiley face appeared on the screen. I then assigned each face a random position on the screen by randomizing the X and Y coordinates of each face. Finally, I made the faces “rain” down the screen using a double if() loop.
Code:
int numberOfInstances = 100; float [] xPositions = new float[numberOfInstances]; float [] x = new float [100]; float [] y = new float [100]; float [] speed = new float[100]; void setup () { size(600, 600); background(0); for(int index = 0; index < numberOfInstances; index++){ x[index] = random(width); y[index] = random(height); speed[index]=random(5); } } void draw () { background (100, 150, 200); for(int index = 0; index < numberOfInstances; index++){ face(x[index],y[index]); y[index]+=speed[index]; if(y[index] > height){ face(x[index], y[index]); y[index] *= -1; } } } void face(float x, float y){ circle(x-30,y-80,15); circle(x+30,y-80,15); arc(x,y-50,80,80,0, PI); }
Arrays are extremely useful to store a set of values to be recalled later. This was especially useful in this project to duplicate the number of objects that appeared on the screen. Although I don’t really see a necessity for them in our final project, they may possibly be used to store the different values of sounds played while someone draws.
For this homework, I created a grid of squares that slowly change in hue. Then, when one mouses over the squares, they decrease in size in a circle around the mouse, with smaller circles being closer to the cursor. This was done by storing the positions of the squares and the colors in arrays so they could be called upon later.
int col=25, row= 25; int number= col*row; int padding= 140; float defaultSize=20; float [] xPositions= new float[number]; float [] yPositions= new float[number]; color [] colors= new color[number]; void setup() { size(800, 800); background(0); colorMode(HSB, 360, 100, 100); rectMode(CENTER); for (int j=0; j<row; j++){ for (int i=0;i<col;i++){ int index=i+j*col; xPositions[index]=map(i,0, col-1,padding, width-padding); yPositions[index]=map(j,0, row-1,padding, height-padding); colors[index]=color(random(360), 50, 80); } } } void draw() { background(0); for (int i=0;i<number;i++){ float offset=dist(mouseX, mouseY, xPositions[i], yPositions[i]); offset=max(offset, 0.001); float hue=hue(colors[i]); colors[i]=color(hue+1, saturation(colors[i]), brightness(colors[i])); float scale= constrain(offset*0.01, 0, 1); float angle=0; drawShape(xPositions[i], yPositions[i], colors[i], scale, angle); } } void drawShape(float x, float y, color c, float scale, float angle){ pushMatrix(); pushStyle(); translate(x,y); fill(c); noStroke(); scale(scale); rotate(angle); rect(0, 0, defaultSize, defaultSize); popMatrix(); popStyle(); }