## Answer Key to Test 1

Dear Students,
Thanks to our Course Assistant David, we now have an answer key to Test #1:

 /* Test1: Duplicate the image located at http://www.golancourses.net/ blog/wp-content/uploads/2008/09/string-art.jpg.   The image has dimensions 300px x 300px. The gap between each line is 25 px, and the border around the line drawing is also 25 px. */   /* - Answer Key, code only ------------------------------------ */ size(300, 300); background(#e9e9e9); smooth(); for (int i=gap; i <=width-gap; i+=gap) { int topsideX = i; int topsideY = gap; int rightsideX = width-gap; int rightsideY = i; line(topsideX, topsideY, rightsideX, rightsideY); }     /* - Answer Key, with details -------------------------------- */ size(300, 300); //Set the dimensions of this copy to //match the original image   background(#e9e9e9); //Set the correct background color //(this part was not required)   smooth(); //Technically necessary to really duplicate //the image, but not required   int gap = 25; //All spacing in this drawing are 25 px wide.   //Begin Drawing loop   for (int i = gap; //i will indicate the x-location of the top of //the line; have to consider border in //choosing a start position   i <= width-gap; //the last line we want to draw has a x-location //on the top of width-gap, right on the edge of //the border. Using <= because we want to still //draw a line at this point. Without the = it //would be missing the last line   i += gap) //Each line is separated by a gap of size gap. //i represents the location of the line, so we //should increment by this gap { int topsideX = i; //the x value of the top of the line   int topsideY = gap; //the y value of the top of the line; this is the //same for every line   //the x value of the right of the line; this is //the same for every line int rightsideX = width - gap;   int rightsideY = i; //the y value of the right of the line; follows //the same pattern as the x value on the top of the line   //Draw a line based on the variables we calculate just above line(topsideX, topsideY, rightsideX, rightsideY); }

## Examples from Wednesday, 9/10/2008

Testing the location of the mouse on screen:

 void setup(){ size(300,300); } void draw(){   float firstThird = width * 0.333; float secondThird = width * 0.666;   if ((mouseX < firstThird) || (mouseX > secondThird)){ background(255,0,0); } else { background(255); } }

Using the mousePressed variable to govern the color of the screen, with an if{}:

 void setup(){ size(300,300); }   void draw(){ if (mousePressed){ background(0); } else { background(255); } }

Here’s our drinking bar:

 int ageOfIncomingPerson = 23; final int allowableAdmissionAge = 18; final int legalDrinkingAge = 21; boolean bCanGetIn = (ageOfIncomingPerson >= allowableAdmissionAge); boolean bCanDrink = (ageOfIncomingPerson >= legalDrinkingAge);   if (bCanGetIn){ println("Come on in"); if (bCanDrink){ println("Have a drink"); } else { println("too bad you cant drink"); } } else { println("Scram kid"); }

Simple animation: a line which travels across the screen, and then restarts.
Requires persistent data, the float x:

 float x = 0;   void setup(){ size(300,300); }   void draw(){ background(255); line(x,0, 0,height);   x = x + 1; // increment x if (x > width){ x = 0; } }

Using the mousePressed() function to increment the number of clicks, as stored in a piece of persistent data, which is then also used to govern the position of a line:

 float x = 0; void setup(){ size(300,300); }   void draw(){ background(255); line(x,0, 0,height); if (x > width){ x = 0; } }   void mousePressed(){ x = x + 1; // increment x println(x); }

A persistent boolean, which is used to govern the color of the canvas:

 boolean bBlack = false;   void setup(){ size(300,300); }   void draw(){ if (bBlack){ background(0); } else { background(255); } }   void mousePressed(){ bBlack = !bBlack; }