# ICM_3_RainMan & Triangles

Time flies and here I am sweet ICM homework #3. It’s a collaboration work by Karam and I, and basically we just used each other’s class and try to make some interaction! I got the triangle class from Karam and background idea from Gladys, and this work was inspired by reading The Nature of Code of Daniel Shiffman. Just finished chapter 1. Super good/tough stuff!!  Here’s my new born baby(click me click me)! It’s about a person who loves being rained, just enjoys the feeling of being miserable. When rain stops, the smile stops and background went blue. You can click your mouse and make this guy happy again.

For this, I wrote 5 classes… just couldn’t stop, and hit into wall sooo many time. I was like a small rooted tree in the corner of ITP floor on Monday. And here’s are some problems(all about using class in class) and answers(except the last one) from try-and-error process of myself.

• Where to put the moving code?
• make sure you can control the position in the { } in which the leading moving codes(e.g. x++) settle
• If things moves in different way?
• put moving codes separately in its own class
• Wants rains start from cloud
• don’t need to declare Cloud class in Rain class!! can use it directly(tearing)
• How to make smile affected by rain????

Still can’t solve the last problem. I guess there must be something wrong about my rain drops. I use distance between each rain drop and the man’s head to write some condition codes. The rain drops may seem to be affected well by the man, but I bet it’s actually not what we think they are, and that’s why although I could affect rain drops with the man, I failed to affect the smile with the rain drops? Too complicated. So in the end, I compromised and made the man smile when it’s raining and sad when it’s not.

Maybe it’s time to book an office hour with Dan.

And as usual, below are my baby’s bloody organs(codes).

Main

```MainRain[] rains;
Cloud cloud;
Tri[] t;

int totalDrops = 0;
int totalTris = 0;

float ln = 30.0; //length of triangle sides
float dis = 50.0; //distance between triangles

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

cloud = new Cloud();
rains = new Rain;
t = new Tri;

}

void draw() {

background(100);

if(totalTris < t.length) {
t[totalTris] = new Tri(ln, dis);
totalTris ++;
}

for (int i=0; i < totalTris; i++) {
t[i].pattern();
t[i].show();
}

if(totalDrops < rains.length) {
rains[totalDrops] = new Rain();
totalDrops ++;
pp.happy = true;
} else {
pp.happy = false;
}

for (int i=0; i < totalDrops; i++) {
rains[i].update();
rains[i].updateCloud();
rains[i].show();

}

cloud.drawCloud();
cloud.move();

pp.update(mouseX, mouseY);
pp.show();
pp.facial();
}

void mousePressed() {
totalDrops = 0;
}```

Cloud

```class Cloud{

float x, y; // position
float speed;
float scaleC; // size # to scale
color c; // color

Cloud(float _x, float _y, float _speed, float _scaleC, color _c) {
x = _x;
y = _y;
speed = _speed;
c = _c;
scaleC = random((_scaleC-0.05), (_scaleC+0.05));
}

Cloud() {
x = width/2;
y = 50;
speed = 1;
scaleC = 0.6;
c = color(200);
}

// draw a cloud shape
void drawCloud() {

pushMatrix();
pushStyle();

translate(x,y);
noStroke();
fill(c);
scale(scaleC);

float xA=30, yA=-40;
bezier(30+xA,20+yA,90+xA,5+yA,110+xA,100+yA,30+xA,75+yA);
bezier(30+xA,75+yA,50+xA,110+yA,-10+xA,120+yA,-5+xA,80+yA);
bezier(-5+xA,80+yA,-10+xA,120+yA,-80+xA,110+yA,-60+xA,80+yA);
bezier(-60+xA,80+yA,-70+xA,110+yA,-100+xA,90+yA,-80+xA,80+yA);
bezier(-80+xA,80+yA,-140+xA,100+yA,-160+xA,20+yA,-110+xA,30+yA);
bezier(-110+xA,30+yA,-120+xA,10+yA,-100+xA,0+yA,-90+xA,20+yA);
bezier(-90+xA,20+yA,-95+xA,0+yA,-60+xA,-30+yA,-40+xA,5+yA);
bezier(-40+xA,5+yA,-35+xA,-30+yA,60+xA,-10+yA,30+xA,20+yA);

//fill cloud with color
beginShape();
vertex(31+xA,19+yA);
vertex(31+xA,76+yA);
vertex(-5+xA,81+yA);
vertex(-60+xA,81+yA);
vertex(-80+xA,81+yA);
vertex(-111+xA,29+yA);
vertex(-91+xA,19+yA);
vertex(-40+xA,4+yA);
endShape(CLOSE);

popStyle();
popMatrix();
}

// make cloud moves right
void move() {
x += speed;
if(x < 90 || x > width - 90) {
speed *= -1;
}
}

// make cloud moves up and down
void drifting() {
y += random(-0.5,0.5);
}

}```

Drop

```class Drop{

float x, y, w, h;

Drop(float _x, float _y, float _w, float _h) {
x = _x;
y = _y;
w = _w;
h = _h;
}

void show() {

pushStyle();
smooth();
noStroke();
triangle(x, y-h, x+w/2, y, x-w/2, y);
ellipse(x, y, w, w);
popStyle();
}

}```

Rain

```class Rain{

PVector location;
PVector velocity;
PVector acceleration;
PVector distance;

Drop drop;

float w, h;
float topSpeed;
color c;

Rain() {

location = new PVector(cloud.x + random(-40, 40), cloud.y + 20);
velocity = new PVector(random(0.01), random(0.1));
distance = new PVector(20, 20);

w = 8;
h = 10;
topSpeed = 4;
c = color(128, 226, 234);
}

void show() {

smooth();
pushStyle();

noStroke();
fill(c);
drop = new Drop(location.x, location.y, w, h);
drop.show();

popStyle();
}

void update() {

PVector mouse = new PVector(mouseX, mouseY-60);
PVector dir = PVector.sub(mouse, location);

acceleration = new PVector(0, 0.03);

if (dir.mag() < distance.mag()) {

dir.normalize();
dir.x *= random(-0.4);
dir.y = 0;
acceleration = dir;
}

velocity.limit(topSpeed);
}

void updateCloud() {

PVector cloudPosition = new PVector(cloud.x, cloud.y);
PVector dir = PVector.sub(cloudPosition, location);

acceleration = new PVector(0, 0);

dir.normalize();
dir.y = 0;
dir.x *= random(-0.01, 0.01);
acceleration = dir;

}

void checkFloor() {

if (location.y > height) {
location.y = 0;
} else if (location.y < 0) {
location.y = height;
}

if (location.x > width) {
location.x = 0;
} else if (location.x < 0) {
location.x = width;
}
}

}```

```class SadPP{

float x, y;
Drop tear;
boolean happy;

x = 0;
y = 0;
tear = new Drop(x+5, y-10, 10, 5);
happy = false;
}

void update(float _x, float _y) {
x = _x;
y = _y;
}

void show() {
smooth();

pushStyle();
noStroke();
fill(255);
ellipse(x, y-40, 30, 30);
rectMode(CENTER);
rect(x, y, 25, 40);

stroke(255);
strokeWeight(8);
line(x-8.8, y+18, pmouseX-8.8, pmouseY+48);
line(x+8.4, y+18, pmouseX+8.4, pmouseY+48);
popStyle();
}

void facial() {

if (happy == true) {
pushStyle();
noFill();
strokeWeight(1.5);
stroke(255,0,0);
beginShape();
curveVertex(x-8.5, y-40);
curveVertex(x-8.5, y-40);
curveVertex(x-6, y-33);
curveVertex(x, y-30);
curveVertex(x+6, y-33);
curveVertex(x+8.5, y-40);
curveVertex(x+8.5, y-40);
endShape();
popStyle();
} else if (happy == false) {
pushStyle();
stroke(0);
beginShape();
curveVertex(x-8.5, y-30);
curveVertex(x-8.5, y-30);
curveVertex(x-6, y-37);
curveVertex(x, y-40);
curveVertex(x+6, y-37);
curveVertex(x+8.5, y-30);
curveVertex(x+8.5, y-30);
endShape();
popStyle();
}

}
}```

Triangle

```class Tri {
float x, y; //pattern starting point
float ln; //length of triangle sides
float dis; //distance between triangles

Tri(float _ln, float _dis) {
//set starting point of pattern
x = 0;
y = 0;
ln = _ln;
dis = _dis;
}

void show() {

pushStyle();
noStroke();

if (pp.happy == true) {
fill(x+100, 0, y, 80);
} else if (pp.happy == false) {
fill(0, y, x+100, 80);
}

if (random(1) > 0.52) {
triangle(x, y+ln, x+ln/2, y, x+ln, y+ln);
}
else {
triangle(x, y, x+ln/2, y+ln, x+ln, y);
}
popStyle();

}

void pattern() {

pushStyle();
if (pp.happy == true) {
x += dis;
if (x > width) {
x = 0;
y += ln;
}
if (y >= height) {
x = 0;
y = 0;
}
} else {
x -= dis;
if (x < 0) {
x = width;
y -= ln;
}
if (y <= 0-ln) {
x = width;
y = height;
}
}
popStyle();
}

}```