Skip to content

Instantly share code, notes, and snippets.

@sabotai
Created March 30, 2016 20:36
Show Gist options
  • Save sabotai/a6f1e1e22a7c446c2533e53561dcdafa to your computer and use it in GitHub Desktop.
Save sabotai/a6f1e1e22a7c446c2533e53561dcdafa to your computer and use it in GitHub Desktop.
For Loop/Array Flip Switch Example for GD105
int howMany = 45; //variable to keep track of how many
int[] x = new int[howMany]; //lets make a new array to keep track of x values
int[] y = new int[howMany]; //lets make a new array to keep track of y values
boolean[] beenClicked = new boolean[howMany]; //lets make a new array to keep track of which ones have been clicked
int dia = 100; //diameter of our ellipses
void setup(){
size(800,600);
for(int i = 0; i < howMany; i++){ //assign each of our x and y positions
x[i] = int(random(0,width)); //random x position up to the width of the window
y[i] = int(random(0,height));//random y position up to the height of the window
beenClicked[i] = false; //by default, none of them have been clicked
}
}
void draw(){
background(0); //draw our background each frame
for(int i = 0; i < howMany; i++){ //lets use a for loop to draw each one of our circles, because ... lazy
if (mousePressed && (dist(mouseX,mouseY,x[i],y[i]) < dia/2)){ //if the mouse is hovering i.e. the distance from the center is less than the radius
beenClicked[i] = !beenClicked[i];
}
if (beenClicked[i]){
fill(random(50)); //those that are hovered over should change to a dark color
} else {
fill(255); //those not being hovered over should be white
}
ellipse(x[i],y[i],dia,dia); //draw each ellipse with its x and y positions, with a diameter = to dia
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment