Recitation Functions and Arrays – Stephanie Anderson – 006

Introduction:

In this lab we continued the basics of Processing and began to better understand the use of arrays and other functions.

Objectives:

  1. Learn about how to incorporate arrays into code
  2. Understanding the differences between loops and setup()

Materials (as listed on the IMA website):

Computer

Processing

Process:

Step 1:  Create a graphic.

I was attempting to make a bear face, but I ended up with a head, ears, and half of his nose and mouth. I realized that I could create an arc but I did not understand how to flip the arc in order to create the other half of his mouth. I then realized that I was wasting too much time creating his face and not spending enough time on the code, so I left his face as it was. 

Step 2: Make his face 100 times

After I finished his face, I moved on to creating his face 100 times over.  This is where I learn about the difference between the setup() and draw() functions that create drastic differences in code (I will explain the differences below).

Step 3: Arrays . . . l o l

The last part was the tricky part: arrays. I especially struggled with arrays when I first started programming, and I still struggle with them. I think now I have a much firmer understanding of their capabilities.  I still am not as strong at creating arrays, so I used the basis of my code from Eric’ in-class example and modified it from there.  I incorporated my own design, adjusted the parameters, and renamed the variables. We used the array to create values that would allow the graphic to appear in different spots around the screen.

Step 4: Moving the Images

Step four mainly included adding three additional lines of code in a for loop that allowed for the movement of the objects dependent on where they originally started in the screen. 

/* notes: Unfortunately, my screen recording would not record both the processed images and the code at the same time so I recorded them separately. */

CODE:

//x = 200
// y = 160
float x;
float y;
int numInst = 100;
float[] xpositions = new float[numInst];
float[] ypositions = new float[numInst];

void setup(){
size(500, 500);
background(255);

for( int i = 0; i < xpositions.length; i++);{
xpositions[i] = random(width);
ypositions[i] = random(height);
}
printArray(xpositions);
println();
printArray(ypositions);

/*
for(int i = 0; i<100 ; i++){
graphic(random(width), random(height), color(20, 30, random(100), 20));
}*/

}

void draw(){

background(255);
for( int i = 0; i<100; i++){
smiley(random(800), random(600));
}

for(int i=0; i<xpositions.length; i++) {
smiley(xpositions[i], ypositions[i]);
xpositions[i] += random(-15, 15);
ypositions[i] += random(-5, 25);

}
}

void graphic(float x, float y){

//noFill();

fill(20, 100, 180);
circle(x-40, y-20, 50);
circle(x+25, y-20, 50);
fill(200, 0, 25);
circle(x, y+10, 100 );
rect(x-10, y+23, 10, 10);
arc(x+5, y+27, 20, 30, HALF_PI, PI);

//circle(200, 200, 100 );
//circle(160, 160, 50);
//circle(225, 160, 50);
//rect(190, 195, 10, 10);
//arc(205, 200, 20, 30, HALF_PI, PI);
//arc(205, 200, 20, 30, PI, PI);

}

Reflection:

This recitation was fun, as always, but also a pain. I really dislike arrays. I understand that they are really useful, but I still feel as though I am not in full command of their capabilities which I hope to get better about. 

Questions:

1. In your own words, please explain the difference between having your for loop from Step 2 in setup() as opposed to in draw().

The key difference between setup() and draw() is that setup() produces just one instance of the code and then terminates, while draw() will loop the code infinitely. Unless you call a for() or some other infinite loop in setup(), then the code will only run one. When there is code in draw() the code will continue to run unless there is a case in place that would allow the code to terminate after a certain number of iterations. 

2. What is the benefit of using arrays?  How might you use arrays in a potential project?

Arrays are useful because they can create data without you having to manually input values that you want. For example, in the case where we wanted our graphic to appear at random places around the screen, then the use of a array allows us to utilize the computer to generate numbers for us. This is invaluable because it saves an infinite amount of time. I am planning to use arrays in my final project to store data of the user and then make my device do a certain action based on certain stored values that it calls upon.

Leave a Reply