Difference between revisions of "CSC103 Processing Lab: Solution Sketches"

From dftwiki3
Jump to: navigation, search
(Created page with "=Solutions= ==Orange Rectangles== <br /> <code><pre> void setup() { size(480, 480 ); smooth(); } void draw() { // ellipse(mouseX, mouseY, 80, 80); fill( 255, 200, 8 ); ...")
 
Line 3: Line 3:
 
<br />
 
<br />
 
<code><pre>
 
<code><pre>
void setup() {
+
void&nbsp;setup()&nbsp;{
  size(480, 480 );
+
&nbsp;&nbsp;size(480,&nbsp;480&nbsp;);
  smooth();
+
&nbsp;&nbsp;smooth();
}
+
}
   
+
  &nbsp;
void draw() {
+
void&nbsp;draw()&nbsp;{
  // ellipse(mouseX, mouseY, 80, 80);
+
&nbsp;&nbsp;//&nbsp;ellipse(mouseX,&nbsp;mouseY,&nbsp;80,&nbsp;80);
  fill( 255, 200, 8 );
+
&nbsp;&nbsp;fill(&nbsp;255,&nbsp;200,&nbsp;8&nbsp;);
  rect( mouseX, mouseY, 80, 80 );
+
&nbsp;&nbsp;rect(&nbsp;mouseX,&nbsp;mouseY,&nbsp;80,&nbsp;80&nbsp;);
}
+
}
 +
 
 
</pre></code>
 
</pre></code>
 
<br />
 
<br />
Line 19: Line 20:
 
<br />
 
<br />
 
<code><pre>
 
<code><pre>
void setup() {
+
void&nbsp;setup()&nbsp;{
  size(480, 480 );
+
&nbsp;&nbsp;size(480,&nbsp;480&nbsp;);
  smooth();
+
&nbsp;&nbsp;smooth();
}
+
}
   
+
  &nbsp;
void draw() {
+
void&nbsp;draw()&nbsp;{
  // ellipse(mouseX, mouseY, 80, 80);
+
&nbsp;&nbsp;//&nbsp;ellipse(mouseX,&nbsp;mouseY,&nbsp;80,&nbsp;80);
  fill( 255, 200, 8 );
+
&nbsp;&nbsp;fill(&nbsp;255,&nbsp;200,&nbsp;8&nbsp;);
  rect( mouseY, mouseX, 80, 80 );
+
&nbsp;&nbsp;rect(&nbsp;mouseY,&nbsp;mouseX,&nbsp;80,&nbsp;80&nbsp;);
}
+
}
 +
 
 
</pre></code>
 
</pre></code>
 
<br />
 
<br />
Line 34: Line 36:
 
<br />
 
<br />
 
<code><pre>
 
<code><pre>
void setup() {
+
void&nbsp;setup()&nbsp;{
  size(480, 480 );
+
&nbsp;&nbsp;size(480,&nbsp;480&nbsp;);
  smooth();
+
&nbsp;&nbsp;smooth();
}
+
}
   
+
  &nbsp;
void draw() {
+
void&nbsp;draw()&nbsp;{
  //background( 200 );
+
&nbsp;&nbsp;//background(&nbsp;200&nbsp;);
   
+
  &nbsp;
  if ( mousePressed == true ) {
+
&nbsp;&nbsp;if&nbsp;(&nbsp;mousePressed&nbsp;==&nbsp;true&nbsp;)&nbsp;{
      fill( 231, 47, 39 ); // red
+
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fill(&nbsp;231,&nbsp;47,&nbsp;39&nbsp;);&nbsp;//&nbsp;red
      ellipse(mouseX, mouseY, 80, 80);
+
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ellipse(mouseX,&nbsp;mouseY,&nbsp;80,&nbsp;80);
  }
+
&nbsp;&nbsp;}
  else {
+
&nbsp;&nbsp;else&nbsp;{
      fill( 255, 200, 8 ); // orange
+
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fill(&nbsp;255,&nbsp;200,&nbsp;8&nbsp;);&nbsp;//&nbsp;orange
      rect(mouseX, mouseY, 80, 80);
+
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rect(mouseX,&nbsp;mouseY,&nbsp;80,&nbsp;80);
  }
+
&nbsp;&nbsp;}
   
+
  &nbsp;
}
+
}
 
 
 
</pre></code>
 
</pre></code>
 
<br />
 
<br />
Line 58: Line 59:
 
<br />
 
<br />
 
<code><pre>
 
<code><pre>
void setup() {
+
void&nbsp;setup()&nbsp;{
  size(480, 480 );
+
&nbsp;&nbsp;size(480,&nbsp;480&nbsp;);
  smooth();
+
&nbsp;&nbsp;smooth();
}
+
}
   
+
  &nbsp;
void draw() {
+
void&nbsp;draw()&nbsp;{
  //background( 200 );
+
&nbsp;&nbsp;//background(&nbsp;200&nbsp;);
   
+
  &nbsp;
  if ( mousePressed == true ) {
+
&nbsp;&nbsp;if&nbsp;(&nbsp;mousePressed&nbsp;==&nbsp;true&nbsp;)&nbsp;{
      fill( 231, 47, 39 ); // red
+
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fill(&nbsp;231,&nbsp;47,&nbsp;39&nbsp;);&nbsp;//&nbsp;red
      rect(mouseX, mouseY, 200, 20);
+
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rect(mouseX,&nbsp;mouseY,&nbsp;200,&nbsp;20);
  }
+
&nbsp;&nbsp;}
  else {
+
&nbsp;&nbsp;else&nbsp;{
      fill( 255, 200, 8 ); // orange
+
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fill(&nbsp;255,&nbsp;200,&nbsp;8&nbsp;);&nbsp;//&nbsp;orange
      rect(mouseX, mouseY, 20, 100);
+
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rect(mouseX,&nbsp;mouseY,&nbsp;20,&nbsp;100);
  }
+
&nbsp;&nbsp;}
   
+
  &nbsp;
}
+
}
 
 
 
</pre></code>
 
</pre></code>
 
<br />
 
<br />
Line 82: Line 82:
 
<br />
 
<br />
 
<code><pre>
 
<code><pre>
void setup() {
+
void&nbsp;setup()&nbsp;{
  size(480, 480 );
+
&nbsp;&nbsp;size(480,&nbsp;480&nbsp;);
  smooth();
+
&nbsp;&nbsp;smooth();
}
+
}
 +
&nbsp;
 +
void&nbsp;draw()&nbsp;{
 +
&nbsp;&nbsp;//background(&nbsp;200&nbsp;);
 +
&nbsp;&nbsp;if&nbsp;(&nbsp;mousePressed&nbsp;)&nbsp;{
 +
&nbsp;&nbsp;&nbsp;&nbsp;line(&nbsp;mouseX,&nbsp;mouseY,&nbsp;pmouseX,&nbsp;pmouseY&nbsp;);
 +
&nbsp;&nbsp;}
 +
&nbsp;&nbsp;
 +
}
 
   
 
   
void draw() {
 
  //background( 200 );
 
  if ( mousePressed ) {
 
    line( mouseX, mouseY, pmouseX, pmouseY );
 
  }
 
 
 
}
 
 
</pre></code>
 
</pre></code>
 
<br />
 
<br />
Line 99: Line 100:
 
<br />
 
<br />
 
<code><pre>
 
<code><pre>
void setup() {
+
void&nbsp;setup()&nbsp;{
  size( 400, 400 );
+
&nbsp;&nbsp;size(&nbsp;400,&nbsp;400&nbsp;);
  smooth();
+
&nbsp;&nbsp;smooth();
}
+
}
 +
&nbsp;
 +
void&nbsp;draw()&nbsp;{
 +
&nbsp;
 +
&nbsp;&nbsp;background(200);
 +
&nbsp;
 +
&nbsp;&nbsp;//&nbsp;count&nbsp;from&nbsp;20&nbsp;to&nbsp;380&nbsp;jumping&nbsp;20&nbsp;each&nbsp;time.
 +
&nbsp;&nbsp;for(&nbsp;int&nbsp;x&nbsp;=&nbsp;20;&nbsp;x&nbsp;<&nbsp;400;&nbsp;x&nbsp;=&nbsp;x&nbsp;+&nbsp;20&nbsp;)&nbsp;{
 +
&nbsp;
 +
&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;and&nbsp;draw&nbsp;a&nbsp;circle&nbsp;in&nbsp;that&nbsp;position,&nbsp;on&nbsp;a&nbsp;horizontal&nbsp;line.
 +
&nbsp;&nbsp;&nbsp;&nbsp;fill(&nbsp;250,&nbsp;200,&nbsp;8&nbsp;);
 +
&nbsp;&nbsp;&nbsp;&nbsp;ellipse(&nbsp;200,&nbsp;x,&nbsp;15,15&nbsp;);&nbsp;
 +
&nbsp;
 +
&nbsp;&nbsp;}
 +
}
 
   
 
   
void draw() {
 
 
  background(200);
 
 
  // count from 20 to 380 jumping 20 each time.
 
  for( int x = 20; x < 400; x = x + 20 ) {
 
 
    // and draw a circle in that position, on a horizontal line.
 
    fill( 250, 200, 8 );
 
    ellipse( 200, x, 15,15 );
 
 
  }
 
}
 
 
</pre></code>
 
</pre></code>
 
<br />
 
<br />
Line 122: Line 124:
 
<br />
 
<br />
 
<code><pre>
 
<code><pre>
void setup() {
+
void&nbsp;setup()&nbsp;{
  size( 400, 400 );
+
&nbsp;&nbsp;size(&nbsp;400,&nbsp;400&nbsp;);
  smooth();
+
&nbsp;&nbsp;smooth();
}
+
}
   
+
  &nbsp;
void draw() {
+
void&nbsp;draw()&nbsp;{
   
+
  &nbsp;
  background(200);
+
&nbsp;&nbsp;background(200);
   
+
  &nbsp;
  // count from 20 to 380 jumping 20 each time.
+
&nbsp;&nbsp;//&nbsp;count&nbsp;from&nbsp;20&nbsp;to&nbsp;380&nbsp;jumping&nbsp;20&nbsp;each&nbsp;time.
  for( int x = 20; x < 400; x = x + 20 ) {
+
&nbsp;&nbsp;for(&nbsp;int&nbsp;x&nbsp;=&nbsp;20;&nbsp;x&nbsp;<&nbsp;400;&nbsp;x&nbsp;=&nbsp;x&nbsp;+&nbsp;20&nbsp;)&nbsp;{
   
+
  &nbsp;
    // and draw a circle in that position, on a horizontal line.
+
&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;and&nbsp;draw&nbsp;a&nbsp;circle&nbsp;in&nbsp;that&nbsp;position,&nbsp;on&nbsp;a&nbsp;horizontal&nbsp;line.
    fill( 250, 200, 8 );
+
&nbsp;&nbsp;&nbsp;&nbsp;fill(&nbsp;250,&nbsp;200,&nbsp;8&nbsp;);
    ellipse( 200, x, 15,15 );  
+
&nbsp;&nbsp;&nbsp;&nbsp;ellipse(&nbsp;200,&nbsp;x,&nbsp;15,15&nbsp;);&nbsp;
    ellipse( x, 200, 15,15 );  
+
&nbsp;&nbsp;&nbsp;&nbsp;ellipse(&nbsp;x,&nbsp;200,&nbsp;15,15&nbsp;);&nbsp;
   
+
  &nbsp;
  }
+
&nbsp;&nbsp;}
}
+
}
 
</pre></code>
 
</pre></code>
 
<br />
 
<br />
Line 146: Line 148:
 
<br />
 
<br />
 
<code><pre>
 
<code><pre>
void setup() {
+
void&nbsp;setup()&nbsp;{
  size( 400, 400 );
+
&nbsp;&nbsp;size(&nbsp;400,&nbsp;400&nbsp;);
  smooth();
+
&nbsp;&nbsp;smooth();
}
+
}
   
+
  &nbsp;
void draw() {
+
void&nbsp;draw()&nbsp;{
 +
&nbsp;
 +
&nbsp;&nbsp;background(200);
 +
&nbsp;&nbsp;
 +
&nbsp;&nbsp;//&nbsp;count&nbsp;from&nbsp;20&nbsp;to&nbsp;380&nbsp;jumping&nbsp;20&nbsp;each&nbsp;time.
 +
&nbsp;&nbsp;for(&nbsp;int&nbsp;x&nbsp;=&nbsp;20;&nbsp;x&nbsp;<&nbsp;400;&nbsp;x&nbsp;=&nbsp;x&nbsp;+&nbsp;20&nbsp;)&nbsp;{
 +
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fill(&nbsp;255,&nbsp;200,&nbsp;8&nbsp;);
 +
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ellipse(&nbsp;x,&nbsp;x,&nbsp;15,&nbsp;15&nbsp;);&nbsp;
 +
&nbsp;&nbsp;&nbsp;&nbsp;}
 +
}
 
   
 
   
  background(200);
 
 
 
  // count from 20 to 380 jumping 20 each time.
 
  for( int x = 20; x < 400; x = x + 20 ) {
 
      fill( 255, 200, 8 );
 
      ellipse( x, x, 15, 15 );
 
    }
 
}
 
 
</pre></code>
 
</pre></code>
 
<br />
 
<br />
Line 166: Line 169:
 
<br />
 
<br />
 
<code><pre>
 
<code><pre>
void setup() {
+
void&nbsp;setup()&nbsp;{
  size( 400, 400 );
+
&nbsp;&nbsp;size(&nbsp;400,&nbsp;400&nbsp;);
  smooth();
+
&nbsp;&nbsp;smooth();
}
+
}
 +
&nbsp;
 +
void&nbsp;draw()&nbsp;{
 +
&nbsp;
 +
&nbsp;&nbsp;background(200);
 +
&nbsp;&nbsp;
 +
&nbsp;&nbsp;//&nbsp;count&nbsp;from&nbsp;20&nbsp;to&nbsp;380&nbsp;jumping&nbsp;20&nbsp;each&nbsp;time.
 +
&nbsp;&nbsp;for(&nbsp;int&nbsp;x&nbsp;=&nbsp;20;&nbsp;x&nbsp;<&nbsp;400;&nbsp;x&nbsp;=&nbsp;x&nbsp;+&nbsp;20&nbsp;)&nbsp;{
 +
&nbsp;&nbsp;&nbsp;&nbsp;fill(&nbsp;250,&nbsp;200,&nbsp;8&nbsp;);
 
   
 
   
void draw() {
+
&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(&nbsp;x&nbsp;<&nbsp;mouseX&nbsp;)&nbsp;{&nbsp;&nbsp;&nbsp;
 +
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ellipse(&nbsp;x,&nbsp;x,&nbsp;15,15&nbsp;);&nbsp;
 +
&nbsp;&nbsp;&nbsp;&nbsp;}
 +
&nbsp;&nbsp;}&nbsp;
 +
}
 
   
 
   
  background(200);
 
 
 
  // count from 20 to 380 jumping 20 each time.
 
  for( int x = 20; x < 400; x = x + 20 ) {
 
    fill( 250, 200, 8 );
 
 
    if ( x < mouseX ) { 
 
      ellipse( x, x, 15,15 );
 
    }
 
  }
 
}
 
 
</pre></code>
 
</pre></code>
 
<br />
 
<br />
Line 189: Line 193:
 
<br />
 
<br />
 
<code><pre>
 
<code><pre>
void setup() {
+
void&nbsp;setup()&nbsp;{
  size( 400, 400 );
+
&nbsp;&nbsp;size(&nbsp;400,&nbsp;400&nbsp;);
  smooth();
+
&nbsp;&nbsp;smooth();
}
+
}
 +
&nbsp;
 +
void&nbsp;draw()&nbsp;{
 +
&nbsp;
 +
&nbsp;&nbsp;background(200);
 +
&nbsp;&nbsp;
 +
&nbsp;&nbsp;//&nbsp;count&nbsp;from&nbsp;20&nbsp;to&nbsp;380&nbsp;jumping&nbsp;20&nbsp;each&nbsp;time.
 +
&nbsp;&nbsp;for(&nbsp;int&nbsp;x&nbsp;=&nbsp;20;&nbsp;x&nbsp;<&nbsp;400;&nbsp;x&nbsp;=&nbsp;x&nbsp;+&nbsp;20&nbsp;)&nbsp;{
 +
&nbsp;&nbsp;&nbsp;&nbsp;fill(&nbsp;250,&nbsp;x/2,&nbsp;255-x/2&nbsp;);
 
   
 
   
void draw() {
+
  &nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(&nbsp;x&nbsp;<&nbsp;mouseX&nbsp;)&nbsp;{&nbsp;&nbsp;&nbsp;
   
+
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ellipse(&nbsp;x,&nbsp;x,&nbsp;x/2,x/2&nbsp;);&nbsp;
  background(200);
+
&nbsp;&nbsp;&nbsp;&nbsp;}
 
+
&nbsp;&nbsp;}&nbsp;
  // count from 20 to 380 jumping 20 each time.
+
}
  for( int x = 20; x < 400; x = x + 20 ) {
 
    fill( 250, x/2, 255-x/2 );
 
 
 
    if ( x < mouseX ) {  
 
      ellipse( x, x, x/2,x/2 );  
 
    }
 
  }  
 
}
 
 
</pre></code>
 
</pre></code>
 
<br />
 
<br />

Revision as of 09:12, 7 October 2013

Solutions

Orange Rectangles


 void setup() {
   size(480, 480 );
   smooth();
 }
  
 void draw() {
   // ellipse(mouseX, mouseY, 80, 80);
   fill( 255, 200, 8 );
   rect( mouseX, mouseY, 80, 80 );
 }


Swapping mouseX and mouseY


 void setup() {
   size(480, 480 );
   smooth();
 }
  
 void draw() {
   // ellipse(mouseX, mouseY, 80, 80);
   fill( 255, 200, 8 );
   rect( mouseY, mouseX, 80, 80 );
 }


Switching between rectangles and circles on mousePressed


 void setup() {
   size(480, 480 );
   smooth();
 }
  
 void draw() {
   //background( 200 );
  
   if ( mousePressed == true ) {
       fill( 231, 47, 39 ); // red
       ellipse(mouseX, mouseY, 80, 80);
   }
   else {
       fill( 255, 200, 8 ); // orange
       rect(mouseX, mouseY, 80, 80);
   }
  
 }


Tall-skinny, vs. short-wide Rectangles


 void setup() {
   size(480, 480 );
   smooth();
 }
  
 void draw() {
   //background( 200 );
  
   if ( mousePressed == true ) {
       fill( 231, 47, 39 ); // red
       rect(mouseX, mouseY, 200, 20);
   }
   else {
       fill( 255, 200, 8 ); // orange
       rect(mouseX, mouseY, 20, 100);
   }
  
 }


Drawing lines on mousePressed


 void setup() {
   size(480, 480 );
   smooth();
 }
  
 void draw() {
   //background( 200 );
   if ( mousePressed ) {
     line( mouseX, mouseY, pmouseX, pmouseY );
   }
   
 }
 


Vertical Circles


 void setup() {
   size( 400, 400 );
   smooth();
 }
  
 void draw() {
  
   background(200);
  
   // count from 20 to 380 jumping 20 each time.
   for( int x = 20; x < 400; x = x + 20 ) {
  
     // and draw a circle in that position, on a horizontal line.
     fill( 250, 200, 8 );
     ellipse( 200, x, 15,15 ); 
  
   }
 }
 


Cross of Orange Circles


 void setup() {
   size( 400, 400 );
   smooth();
 }
  
 void draw() {
  
   background(200);
  
   // count from 20 to 380 jumping 20 each time.
   for( int x = 20; x < 400; x = x + 20 ) {
  
     // and draw a circle in that position, on a horizontal line.
     fill( 250, 200, 8 );
     ellipse( 200, x, 15,15 ); 
     ellipse( x, 200, 15,15 ); 
  
   }
 }


Diagonal Orange Circles


 void setup() {
   size( 400, 400 );
   smooth();
 }
  
 void draw() {
  
   background(200);
   
   // count from 20 to 380 jumping 20 each time.
   for( int x = 20; x < 400; x = x + 20 ) {
        fill( 255, 200, 8 );
        ellipse( x, x, 15, 15 ); 
     }
 }
 


Orange Circles Left of the Mouse


 void setup() {
   size( 400, 400 );
   smooth();
 }
  
 void draw() {
  
   background(200);
   
   // count from 20 to 380 jumping 20 each time.
   for( int x = 20; x < 400; x = x + 20 ) {
     fill( 250, 200, 8 );
 
     if ( x < mouseX ) {   
        ellipse( x, x, 15,15 ); 
     }
   } 
 }
 


Growing Multicolored Circles


 void setup() {
   size( 400, 400 );
   smooth();
 }
  
 void draw() {
  
   background(200);
   
   // count from 20 to 380 jumping 20 each time.
   for( int x = 20; x < 400; x = x + 20 ) {
     fill( 250, x/2, 255-x/2 );
 
     if ( x < mouseX ) {   
        ellipse( x, x, x/2,x/2 ); 
     }
   } 
 }