Sine Fun
PVector p1,p2;
void setup()
{
size(500,500);
p1 = new PVector(20,20);
p2 = new PVector(width-20, height-20);
smooth();
}
void sineTo(PVector p1, PVector p2, float freq, float amp, float phase)
{
float d = PVector.dist(p1,p2);
float a = atan2(p2.y-p1.y,p2.x-p1.x);
noFill();
pushMatrix();
translate(p1.x,p1.y);
rotate(a);
beginShape();
for (float i = 0; i <= d; i += 1) {
float y = sin(i*TWO_PI*freq/d+phase)*amp;
if (i < d/10)
y = lerp(0,y,i/(d/10));
else if (i > d-d/10)
y = lerp(y,0,(i-(d-d/10))/(d/10));
vertex(i,y);
}
endShape();
popMatrix();
}
void draw()
{
float tm = millis()*.001;
float freq = 8 + sin(tm)*6;
float amp = 30 + cos(tm/2)*20;
float phase = tm*10;
background(255);
sineTo(p1,p2,freq,amp,phase);
fill(255,255,0);
ellipse(p1.x,p1.y,20,20);
ellipse(p2.x,p2.y,20,20);
if (mousePressed) {
if (dist(p1.x,p1.y,pmouseX,pmouseY) < 10) {
p1.x += mouseX - pmouseX;
p1.y += mouseY - pmouseY;
} else if (dist(p2.x,p2.y,pmouseX,pmouseY) < 10) {
p2.x += mouseX - pmouseX;
p2.y += mouseY - pmouseY;
}
}
}