xxxxxxxxxx
// Mouse click to animate.
// boolean, class, noLoop, sin, cos, PVector
// beginshape, endShape, pow, array, vertex
Heart h;
boolean go;
void setup() {
background(0);
size(800, 400);
go = false;
h = new Heart(plotHeart(width/4, height/2-30, 10),
plotHeart(width- 200, height/2-30, 10));
noLoop();
}
void draw() {
if (go) loop();
background(#83a79a);
strokeWeight(2);
stroke(255);
line(width/2, 0, width/2, height/2-15);
line(width/2, height/2+15, width/2, height);
noFill();
rect(1, 1, width-2, height-2);
h.update();
h.display();
}
// http://mathworld.wolfram.com/HeartCurve.html
PVector[] plotHeart(float posX, float posY, float s) {
PVector[] verts = new PVector[252];
int index = 0;
beginShape();
for (float a = 0; a < TWO_PI; a += 0.025) {
float x = posX + (s * 16 * pow(sin(a), 3));
float y = posY + (- s * (13 * cos(a)
- 5 * cos(2 * a)
- 2 * cos(3 * a)
- cos(4 * a)));
verts[index] = new PVector(x, y);
index++;
}
endShape();
return verts;
}
void mousePressed() {
go = true;
redraw();
}
class Heart {
PVector from[];
PVector to[];
PVector dest;
Heart(PVector[] f, PVector[] t) {
dest = new PVector();
from = f;
to = t;
}
void update() {
for (int i = 0; i<252; i++) {
if (from[i].x <= width/2) dest.set(width/2, height/2);
else if (from[i].x > width/2) dest.set(to[i].x, to[i].y);
PVector dir = PVector.sub(dest, from[i]);
dir.normalize();
dir.mult(0.5);
from[i].add(dir);
}
}
void display() {
stroke(255);
strokeWeight(2);
fill(#adb395);
beginShape();
for (int i = 0; i< 252; i++) {
vertex(from[i].x, from[i].y);
}
endShape(CLOSE);
}
}