# Assignment 2: Processing Chapters 4-6 by Harjot Bal

Hi, it’s Harjot again. This post will cover Lesson 2: Chapters 4, 5, and 6.

Before I jump in, I think the material is going to be harder to digest than last week. Anyway, here’s to another week of useful knowledge.

NOTE: Again, I have not included all book exercises (sparing some room on the blog). I can, however, provide my answers in full should anyone be curious to know how I got through the chapters.

Exercise 4-3

Code: (growing circle)

//Declare and initialize variables
int circleSize = 0;
int circleX = 100;
int circleY = 100;
//Setup window
void setup() {
size(200,200);
smooth();
frameRate(30);
}
//Draw circle with conditions
void draw() {
background(0);
stroke(255);
fill(175);
ellipse(circleX,circleY,circleSize,circleSize);
circleSize = circleSize + 1;
}

Code: (growing circle with mouse)

//Declare and initialize variables
int circleSize = 0;
//Setup window
void setup() {
size(200,200);
smooth();
frameRate(30);
}
//Draw circle with conditions
void draw() {
background(0);
stroke(255);
fill(175);
ellipse(mouseX,mouseY,circleSize,circleSize);
circleSize = circleSize + 1;
}

To change the speed at which the circle grows just increase the number being added to circleSize variable.

So far this seems easy…

Exercise 4-6

This is the code I wrote to keep Zoog centered and within a limited range for the left-to-right shaking movement.

Code:

zoogX = width/2 + random(-50,50);

Exercise 4-7

I thought the random function was very easy to implement so I played around with colours and conditionals in this exercise as well.

Meet Fraag (that’s right, FRAAG):

Click on image to view a demo.

Exercise 5-2

Problem#1:

• Output: 75 is greater than 50!
• Output: 75 is greater than 25!

Problem#2:

• Output: x is now: 5, x is now: 5
• Output: x is now: 5, x is now: 6

Note: The order of conditionals in code is important! The println() function should be used after conditionals which compute a result you are trying to display.

Exercise 5-3

// Rectangle starts at location x
float x = 0;
void setup() {
size(200,200);
}
void draw() {
background(255);
// Display object
fill(0);
rect(x,100,20,20);
// Increment x
x = x + 1;
//Constrain x
x = constrain(x,0,100);
}

Logical operators are sure to come in handy.

&& = AND, || = OR, ! = NOT

Exercise 5-4

Regarding the last statement, it is not possible for “x” to be both greater than 10 and less than 5.

I’m liking the introduction of buttons into the mix…this could make for some really interactive pieces.

Exercise 5-10: Here comes the SUN!

This is what I was able to come up with using random, conditionals, and constrain.

Exercise 6-1

Code:

size(200,200);
background(255);
int y = 0;
while (y<=height) {
stroke(0);
line(0,y,width,y);
y = y + 10; //or y = y + (height/20)
}

Code:

size(200,200);
background(255);
float w = width;
while (w>10) {
stroke(0);
fill(w);
ellipse(width/2,height/2,w,w);
w = w – 20;
}

For loops are very interesting and further reduce the amount of code needed.

Exercise 6-2

Code:

size(200,200);
background(255);
for (int y = 0; y <= height; y+=10) {
stroke(0);
line(0,y,width,y);
}

Code:

size(200,200);
background(255);
for (float w=width; w>=10; w-=20) {
stroke(0);
fill(w);
ellipse(width/2,height/2,w,w);

Lesson 2: Final Project

I have a lot more confidence in my work now. I decided to enhance my FRAAG sketch by adding a Boolean state (day and night feature) and making use of while loops (clouds). The entire sketch is resizable and all algorithms readjust to the new size parameters.

Click the mouse to turn the Sun into the Moon and respectively, day into night.

### 2 comments to Assignment 2: Processing Chapters 4-6 by Harjot Bal

• Maayan Cohen

I love this frog! sweet effects with the sun and the shade!

• Maayan Cohen

it gave me an idea to make my snowman melt… 🙂