Difference between revisions of "CSC103 Homework 5 Solution 2012"

From dftwiki3
Jump to: navigation, search
(Created page with "--~~~~ ---- <code><pre> void setup() { size( 500, 500 ); smooth(); } void draw() { background( 200 ); for ( int x=0; x < 500; x = x+20 ) { fill( x/2, 255-x, ...")
 
Line 2: Line 2:
 
----
 
----
 
<code><pre>
 
<code><pre>
 +
// solution program for Homework #5
 +
// setup()
 
void setup() {
 
void setup() {
 
   size( 500, 500 );
 
   size( 500, 500 );
 
   smooth();  
 
   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() {
 
void draw() {
 +
  // erase the window
 
   background( 200 );
 
   background( 200 );
 
    
 
    
 +
  // draw stacked rectangles from 0 to 500, in increment of 20
 
   for ( int x=0; x < 500; x = x+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 );
 
     fill( x/2, 255-x, 0 );
 +
    // position the rectangle
 
     rect( 0, x, 500, 20 );  
 
     rect( 0, x, 500, 20 );  
 +
 +
    // pick an orange color
 
     fill( 250, 200, 8 );
 
     fill( 250, 200, 8 );
 +
 +
    // if left-circle center is above vertical position of mouse, draw it
 
     if ( mouseY < x ) {
 
     if ( mouseY < x ) {
 
       ellipse( 20, x+10, 20, 20 );
 
       ellipse( 20, x+10, 20, 20 );
 
     }
 
     }
 +
    // otherwise if right-circle center is below vertical position of the mouse, draw it
 
     else {
 
     else {
 
       ellipse( 500-20, x+10, 20, 20 );
 
       ellipse( 500-20, x+10, 20, 20 );

Revision as of 18:10, 7 March 2012

--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 );
     }
   }
}