Assignment 3: Learning Processing Chapters 7 & 9

Another week of Processing and I feel like I’m learning A LOT. Last week was intense but I got through it well I think. This week I’m hoping my ‘FRAAG the Frog’ sketch will really enhance in proportion to the material learned. You’ll also notice the code for my project is much more organized.

Exercise 7-1

I would definitely like a function for drawing Fraag…especially if he wants a family.

drawFrog();

The sun and moon could also use functions…then again, hell everything could use a function.

drawSun(); and drawMoon();

Exercise 7-2

float positionX;
float positionY;
void setup() {
size(200,200);
}
void draw() {
background(0);
drawFrog();
}
void drawFrog() {
strokeWeight(5);
stroke(0);
fill(100,100,0);
//Feet
ellipse(positionX-25,positionY+45,45,17);
ellipse(positionX+25,positionY+45,45,17);
//Body
ellipse(positionX,positionY,100,100);
//Eyes (left then right)
fill(255,255,255);
ellipse(positionX-15,positionY-40,30,30);
ellipse(positionX+15,positionY-40,30,30);
//Eyebrows (left then right)
strokeWeight(3);
fill(0);
rectMode(CENTER);
rect(positionX-15,positionY-65,20,5);
rect(positionX+15,positionY-65,20,5);
//Mouth
strokeWeight(2);
fill(255,0,0,200);
ellipse(positionX,positionY+20,40,40);
//Pupils
float pupilX = constrain(mouseX,positionX-20,positionX-10);
float pupilY = constrain(mouseY,positionY-50,positionY-30);
strokeWeight(2);
fill(r,g,b,200);
ellipse(pupilX,pupilY,8,8);
ellipse(pupilX+30,pupilY,8,8);
}

Functions make code less intimidating to rework since everything is more organized. I like the option of commenting out a function when tinkering around.

Arguments for functions are looking really good right now.

Exercise 7-4

sum(12,34,56);

Exercise 7-5

void multiply(float a, float b) {
float product = a*b;
println(product);
}

Exercise 7-6

drawCar(globalX,globalY,50,color(200,150,100));

I created two new functions which I called into void draw(). The fun part (lame, I know) was passing the parameters from function calls to the function definitions. This made making new additions, as well as changes, a breeze.

I was able to further enhance my sketch after learning this new material; I created functions for multiple instances of sketch elements and passed parameters to edit each unique instance.

Note: There might not be an apparent difference in what is displayed on-screen, but the code (behind the scenes) is structured MUCH more efficiently than before.

Exercise 7-10

Success! One thing I was particularly happy about with using functions and passing parameters was the fact that I found a solid way (after A LOT of code trial and error) to make each Frog’s eyes and shadow move independently, as you would expect, under the influence of the moving sun/moon.

Okay, on to arrays…

Having read just the intro, it sounds like arrays are somewhat like functions in that they lessen code writing for multiple instances. I’m curious to know exactly how they work differently than functions.

Exercise 9-2

Answer: 0 through 999

Exercise 9-3

int [] integers = new int [30];
float [] numbers = new float [100];
Zoog [] zoogs = new Zoog [56];

Exercise 9-6

Part 1

for(int i = 0; i < nums.length; i++) {
nums[i] = nums[i]*nums[i];
}

Part 2

for(int i = 0; i < nums.length; i++) {
nums[i] += int(random(10));
}

Part 3

for(int i = 0; i < nums.length-1; i++) {
nums[i] += nums[i+1]
}

Part 4

int sum = 0;
for(int i = 0; i < nums.length; i++) {
sum += nums[i];
}

This seems to make sense so far…atleast the coding for arrays isn’t extraordinarily complex.

For my final iteration, I used a snake trailing effect for both the Sun and Moon elements of the sketch. It worked out quite nicely.

Afterthoughts:

Coming to the end of this chapter, I’m starting to find myself a bit overwhelmed and confused with arrays…I probably should go over chapter 8. I wanted to add more to my sketch but I guess this will do for now.

Final Project:

Check it out: Click the mouse to toggle Day(Sun) and Night(Moon).

1 comment to Assignment 3: Learning Processing Chapters 7 & 9

  • Jessica Knox

    Really like this sketch, Harjot. I think you’ve done a really good job of incorporating many different tools into one coherent and seemingly simple concept. And it looks fantastic!