Tut 5: Drawing with Conditionals and Loops
- Show Sketch
// Uses the logical OR operator to combine the values of tests into each if statement
/** @peep sketchcode */
size(200, 200);
int a = 20;
int b = 20;
if ((a > 5) || (b < 30)) {
line(40, 100, 160, 100);
}
if ((a > 15) || (b < 30)) {
ellipse(100, 100, 72, 72);
}
/* The difference between a WHILE and a DO...WHILE loop is that the test is performed after the code inside bracket is run. This means that the code will always run AT LEAST ONCE
*/
int x = 10;
do {
println(x);
x--;
} while (x > 1)
Drawing Repeating Patterns with Loops: If you change the value next to += to a higher value, the squares will become farther spaced apart because the x-axis is incremented by a larger number and vice versa. To change the size of the squares themselves, simply edit the width and height attributes of the rect() function
- Show Sketch
/** @peep sketchcode */
size(200, 200);
int y = 20;
for (int x = 20; x < width; x += 10) {
rect(x-5, y-5, 5, 5);
}
Tut 5: combinbed Custom Code Exercise
- Show Code
/**@peep sketch */
void setup() {
size (500, 500);
background (random(255), random(255), random(255));
textSize(15);
text("click around in different parts of the canvas for fun surprises!", 10, 430);
fill(0, 102, 153);
text("press 'enter' to start a new drawing!", 10, 450);
fill (random(255), random(255), random(255));
} // end setup function
void draw() {
strokeWeight(random(10));
stroke(random(255), random(255), random(255));
if (mousePressed == true ) {
line(mouseX, mouseY, pmouseX, pmouseY);
} // end if statement
} // end draw function
// this function draws either a circle or a tirangle, depending on the position of your mouse in reference to x coordinates
// the if statements are used to decide what type of shape would be drawn, depending on the location of where each mouse click happens
void mouseClicked () {
if (mouseX <=250) {
strokeWeight(random(10));
fill(random(255), random(255), random(255));
triangle(30, 76, 58, 20, 86, 75);
} else if (mouseX >=250) {
strokeWeight(random(10));
fill(random(255), random(255), random(255));
ellipse (375,125, 150, 150);
}
} // end of mouseClicked
// this function allows you to press 'enter' to clear and create a new masterpiece!
void keyPressed () {
if (key == ENTER) {
size (500, 500);
background (random(255), random(255), random(255));
textSize(15);
text("click around in different parts of the canvas for fun surprises!", 10, 430);
fill(0, 102, 153);
text("press 'enter' to start a new drawing!", 10, 450);
fill (random(255), random(255), random(255));
}
}
Nothing in the gallery yet!
Comments
Nobody has said anything yet.