Recitation 10

Workshop

I attended the workshop on Object Oriented Programming organised by Tristan. This workshop went over the class category in Processing and when to use them. 

We made an example in order to create an emoji: 

float x;

ArrayList<Emoji> emojiList;

void setup() {
size(600,600);

emojiList = new ArrayList<Emoji>();
for(int i=0; i<100; i++) {
emojiList.add( new Emoji(random(width), random(height), color(random(255), random(255), random(255)) ));
}}

void draw() {
background(255);
for(int i=0; i<emojiList.size(); i++) {
Emoji temp= emojiList.get(i);
temp.display();
}}

void mousePressed() {
emojiList.add( new Emoji(mouseX, mouseY, color(random(255), random(255), random(255))) );
float x = map(mouseX,0,width,width/4,3*width/4);
}

And in another class file called Emoji: 

class Emoji {
float x,y;
float size;
color clr;

Emoji(float startingX, float startingY, color startingColor) {
x= startingX;
y= startingY;
size= random(50,200);
clr = startingColor;
}

void display() {
fill(clr);
ellipse(x,y, size,size);
fill(255);
ellipse(x-size/4, y-size/6, size/4, size/2);
ellipse(x+size/4, y-size/6, size/4, size/2); }
}

The void mouse pressed was added in order to display a new emoji each time I click on the Canva, a new emoji appears:

We made an in class example using the map () function, in order to display the emojis only in a limited space on the canvas, no matter where we pressed on the canvas. 

For the exercise, I made a project which I will use in my final project of a fuse, so a line which would decrease in size from one side under a time constraint of one minute. I added an ‘if’ statement in order to make the line stop shrinking once its reached the other point (the position 0): 

Fuse f;

void setup() {
size(1000,800);
background(0,0,50);
f = new Fuse(width-290, height-250); //new Fuse
}

void draw() {
background(0,0,50);
f.display();
f.shrink();
}

and the Fuse class: 

class Fuse {
float x, y, fuseWidth, speed;
long iLength, itime;

Fuse(float tempX, float tempY) {
x= tempX;
y= height-250;

iLength = 600;
itime = millis() ;
speed = – iLength/6000.0;}

void display () {
stroke(255);
strokeWeight(10);
line(x-fuseWidth, y, x, y);}

void shrink () {
fuseWidth = speed * (millis()- itime) +iLength ;
if(fuseWidth<0) {
fuseWidth=0;
}} }

The result is as follows: 

Leave a Reply