Created
May 16, 2016 20:27
-
-
Save nataliefreed/b3abc6add31427e6c85fcac8e9abc0b2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Classes and objects with arrays example | |
Natalie Freed for Art and Science of Computing May 16, 2016 | |
Whale function by Courtney Peterson, March 9, 2016 | |
From: Whales in Love https://gist.github.com/anonymous/5d5b9bdef5243e8c2973 | |
*/ | |
Whale orangeWhale; | |
Whale greenWhale; | |
//normal array syntax | |
Whale[] pod = new Whale[100]; | |
//ArrayList version | |
ArrayList<Whale> otherPod = new ArrayList<Whale>(); | |
void setup() { | |
size(800, 800); | |
orangeWhale = new Whale(250, 250, 0.5, color(#F08716)); | |
greenWhale = new Whale(250, 250, 1, color(#3DFA5A)); | |
//normal array syntax | |
for(int i=0;i<pod.length;i++) { | |
pod[i] = new Whale(random(0, width), random(0, height), random(-5, 5), color(random(0, 255), random(0, 255), random(0, 255))); | |
} | |
//ArrayList version | |
for(int i=0;i<15;i++) { | |
otherPod.add(new Whale(i*60, height/2, 0, color(random(0, 255)))); | |
} | |
} | |
void draw() { | |
background(#1CCBE8); | |
//render | |
orangeWhale.render(); | |
greenWhale.render(); | |
//normal array syntax | |
for(int i=0;i<pod.length;i++) { | |
//render | |
pod[i].render(); | |
//update | |
pod[i].move(); | |
} | |
//ArrayList version | |
for(Whale w:otherPod) { | |
//render | |
w.render(); | |
//update | |
w.move(); | |
} | |
//easy to add and remove elements from an ArrayList! It can be resized. | |
if(otherPod.size() > 0 && random(0, 1) > 0.9) { | |
otherPod.remove(int(random(0, otherPod.size()))); | |
} | |
//update | |
orangeWhale.move(); | |
greenWhale.move(); | |
} | |
class Whale { | |
float x; | |
float y; | |
float speed; | |
color c; | |
Whale(float x, float y, float speed, color c) { | |
this.x = x; | |
this.y = y; | |
this.speed = speed; | |
this.c = c; | |
} | |
void move() { | |
x = x + speed; | |
} | |
//Whale function by Courtney Peterson, March 9, 2016 | |
//From: Whales in Love https://gist.github.com/anonymous/5d5b9bdef5243e8c2973 | |
void render() { | |
pushMatrix(); | |
translate(x-86, y-70); | |
scale(0.5); | |
fill(0); | |
beginShape();//head curve | |
fill(c); | |
stroke(0); | |
strokeWeight(5); | |
vertex(86, 167);//starting point | |
bezierVertex(52, -3, 255, 32, 268, 147); //back to tail curve | |
bezierVertex(269, 194, 303, 176, 309, 168); //top of left tail curve | |
bezierVertex(308, 133, 342, 132, 346, 130); //bottom of left tail curve | |
bezierVertex(342, 132, 357, 161, 330, 172); //top of right tail curve | |
bezierVertex(347, 166, 364, 168, 371, 183); //bottom of left tail curve | |
bezierVertex(363, 192, 342, 206, 321, 190); //bottom right half of whale curve | |
bezierVertex(312, 206, 253, 224, 218, 218); //fin curve | |
bezierVertex(226, 254, 201, 241, 179, 216); //bottom left half of whale curve | |
bezierVertex(182, 218, 127, 220, 94, 179); //bottom part of smile curve | |
bezierVertex(115, 177, 130, 170, 139, 153); //top part of smile curve | |
bezierVertex(126, 162, 110, 169, 86, 167); | |
endShape(); | |
fill(255, 255, 255); | |
ellipse(146, 119, 15, 22);//outer eye | |
fill(0); | |
ellipse(143, 123, 6, 6);//inner eye | |
noFill(); | |
strokeWeight(2.75); | |
ellipse(156, 61, 20, 10);//blow hole | |
beginShape();//left water spray--base | |
vertex(157, 60); | |
bezierVertex(160, 3, 129, 6, 123, 23); //left water spray--first curve | |
bezierVertex(119, 38, 138, 33, 138, 27); //left water spray--second curve | |
bezierVertex(144, 23, 126, 22, 131, 27); //left water spray--third curve | |
endShape(); | |
beginShape();//right water spray--base | |
vertex(156, 35); | |
bezierVertex(157, 10, 195, 20, 175, 39); //right water spray--first curve | |
bezierVertex(151, 38, 172, 13, 172, 32); //right water spray--second curve | |
endShape(); | |
popMatrix(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment