Saturday, January 24, 2015

Loop Assignment 2

For my last assignment, I had to create a program that will produce a checkerboard pattern using loops. I had to do research on how to create loops that alternate colors in order to do this assignment and I found out about modulo(%) and I modulo in order to create my checkerboard. Modulo basically calculates what the remainder of a number is when two numbers are divided. In order to create this checkerboard I used i & j as two of my int's and made it so that when i+j divided by thirty was more than 0 one part of the screen would be black and the other part of the screen would be red. I used the number thirty because i and j were constantly being increased by 75 and 30 was the only number I  could find that when i and j were divided were divided by it caused a pattern. For example, 75/30 has a remainder, 150/30 does not, 225/30 had a remainder, but 300/30 does not and so on. This is what made me able to create a certain pattern. My finished code is down below.

int  square  = 75;
void setup() {
 size(600, 600);

}


void draw() {
  
 // i starts at 0, i increases by 75, i will stop when it's less than or equal to (width)
 // j starts at 0, j increases by 75, j will stop when it's less than or equal to (height)
 // 600/75= 8 , this board will be 8 across and 8  down(8x8)
 for ( int i = 0; i <= width; i += square) {
   for ( int j = 0; j <= height; j += square) {
     //When i+j divided by 30 has a remainder of 0 make the block black
     //When i + j divided by 30 has a remainder higher than 0 make the block red
     if ( (i+j) % 30 == 0) { //== (means equality)


       fill(#080707);//Black
     } else {
       fill(#FF0000);//Red
     }

     rect (i, j, square, square);
     //(x,y,width,height)
     
  
   }
 }
}


“A program is never less than 90% complete, and never more than 95% complete.”
– Terry Baker

Thursday, January 15, 2015

CSE Loops

Recently I had a new assignment in Computer Science Engineering. We had to modify a code given to us by adding a  keyPressed() function (Links to an external site.) that updated the background color. The code we were given to modify is below.

/* global variables to keep track of the amount of red, green and blue (respectively) in the background */

int r, g, b;


/* setup the window with an initial background color of black */

void setup(){

 size(300, 300);

 r = 0;

 g = 0;

 b = 0;

 background(r, g, b);

}


/* update the background color */

void draw(){

 background(r, g, b);

}


/* DO NOT MODIFY ANY CODE ABOVE THIS LINE */


/* allows the user to increase and decrease the values of r, g, and b */



In order to update the background color I added these codes:

void keyPressed() {

 if (key == 'r') { // change fill color

   r+= 20;

 } else if (key == 'g') { // change fill color

   g += 20;

 } else if (key == 'b') { // change fill color

   b += 20;

 } else if (key == 't') { // change fill color

   r -= 20;

 } else if (key == 'h') { // change fill color

   g -= 20;

 } else if (key == 'n') { // change fill color

   b -= 20;

 }

}



//Key

//r=red +20

//g=green+20

//b=blue+20



//t=red-20

//h=green-20

//n=blue-20

Basically, these codes say if the key r is pressed increase red by 20, if the key g is pressed increase green by green and if the key b is pressed increase blue by 20. I then used the keys t, h, and n to decrease red, green and blue by 20 depending on which button was pressed. I also made a key at the end so my teacher would know which keys to press to increase and decrease certain colors. This code was very easy and didn't take long at all. The full code is down below.

/* global variables to keep track of the amount of red, green and blue (respectively) in the background */

int r, g, b;



/* setup the window with an initial background color of black */

void setup() {

 size(300, 300);

 r = 0;

 g = 0;

 b = 0;

 background(r, g, b);
}



/* update the background color */

void draw() {

 background(r, g, b);
}
void keyPressed() {
 if (key == 'r') { // change fill color
   r+= 20;
 } else if (key == 'g') { // change fill color
   g += 20;
 } else if (key == 'b') { // change fill color
   b += 20;
 } else if (key == 't') { // change fill color
   r -= 20;
 } else if (key == 'h') { // change fill color
   g -= 20;
 } else if (key == 'n') { // change fill color
   b -= 20;
 }
}

//Key
//r=red +20
//g=green+20
//b=blue+20

//t=red-20
//h=green-20

//n=blue-20

“If the code and the comments do not match, possibly both are incorrect.”
– Norm Schryer





Saturday, January 10, 2015

Update Background Color

Recently I had a new assignment in Computer Science Engineering. We had to modify a code given to us by adding a  keyPressed() function (Links to an external site.) that updated the background color. The code we were given to modify is below.


/* global variables to keep track of the amount of red, green and blue (respectively) in the background */


int r, g, b;

/* setup the window with an initial background color of black */


void setup(){


 size(300, 300);


 r = 0;


 g = 0;


 b = 0;


 background(r, g, b);


}

/* update the background color */


void draw(){


 background(r, g, b);


}

/* DO NOT MODIFY ANY CODE ABOVE THIS LINE */

/* allows the user to increase and decrease the values of r, g, and b */


In order to update the background color I added these codes:


void keyPressed() {


 if (key == 'r') { // change fill color


   r+= 20;


 } else if (key == 'g') { // change fill color


   g += 20;


 } else if (key == 'b') { // change fill color


   b += 20;


 } else if (key == 't') { // change fill color


   r -= 20;


 } else if (key == 'h') { // change fill color


   g -= 20;


 } else if (key == 'n') { // change fill color


   b -= 20;


 }


}


//Key


//r=red +20


//g=green+20


//b=blue+20


//t=red-20


//h=green-20


//n=blue-20


Basically, these codes say if the key r is pressed increase red by 20, if the key g is pressed increase green by green and if the key b is pressed increase blue by 20. I then used the keys t, h, and n to decrease red, green and blue by 20 depending on which button was pressed. I also made a key at the end so my teacher would know which keys to press to increase and decrease certain colors. This code was very easy and didn't take long at all. The full code is down below.


/* global variables to keep track of the amount of red, green and blue (respectively) in the background */


int r, g, b;


/* setup the window with an initial background color of black */


void setup() {


 size(300, 300);


 r = 0;


 g = 0;


 b = 0;


 background(r, g, b);
}


/* update the background color */


void draw() {


 background(r, g, b);
}
void keyPressed() {
 if (key == 'r') { // change fill color
   r+= 20;
 } else if (key == 'g') { // change fill color
   g += 20;
 } else if (key == 'b') { // change fill color
   b += 20;
 } else if (key == 't') { // change fill color
   r -= 20;
 } else if (key == 'h') { // change fill color
   g -= 20;
 } else if (key == 'n') { // change fill color
   b -= 20;
 }
}


//Key
//r=red +20
//g=green+20
//b=blue+20


//t=red-20
//h=green-20


//n=blue-20


“If the code and the comments do not match, possibly both are incorrect.”
– Norm Schryer