Monday 9 May 2011

If/Else Examples 2

The next sketch I found on openproccesing.org is called if else. It is also extremely simple, but a good example of a basic if/else statement. The code looks like this:

void draw() {
  if(mousePressed == true) {
        background(mouseX,mouseY,0);
    }
  else {
    // line(25, 150, mouseX, mouseY);
      int a; a =mouseX;        
      int b =  mouseY;  
      int c = a + b;
      for (int i=60; i<540;i=i+60 )
      triangle(i,0 , a-i, b, a, b);
   }
}

The code allows the user to change the background color based on where the mouse is on the screen. The if part of the statement says that if the mouse is pressed the background color will change based on the mouse's coordinates. Otherwise, when the mouse is not pressed, the background color will stay the same, and the triangle figure will change based on the coordinates of the mouse on the screen.  This is a great example of an if/else statement because it shows that when something is true one funtion occurs, and when that something is false, something else happens.

No comments:

Post a Comment