CSC103 Homework 5 Solution 2012

From dftwiki3
Revision as of 17:10, 7 March 2012 by Thiebaut (talk | contribs)
Jump to: navigation, search

--D. Thiebaut 17:02, 7 March 2012 (EST)


// solution program for Homework #5
// setup()
void setup() {
  size( 500, 500 );
  smooth(); 
}

// draw(): called 30 times/sec
//    draws horizontal rectangles with varying colors in background
//    draws circles that fit exactly in rectangles on the left and right sides
//    circles on the left appear only if they're below the mouse, while
//    circles on the right appear only if they're above the mouse.

void draw() {
   // erase the window
   background( 200 );
   
   // draw stacked rectangles from 0 to 500, in increment of 20
   for ( int x=0; x < 500; x = x+20 ) {
     // pick a color whose green contents vary with x
     fill( x/2, 255-x, 0 );
     // position the rectangle
     rect( 0, x, 500, 20 ); 

     // pick an orange color
     fill( 250, 200, 8 );
 
     // if left-circle center is above vertical position of mouse, draw it
     if ( mouseY < x ) {
       ellipse( 20, x+10, 20, 20 );
     }
     // otherwise if right-circle center is below vertical position of the mouse, draw it
     else {
       ellipse( 500-20, x+10, 20, 20 );
     }
   }
}