We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › continously updating curve between points
Page Index Toggle Pages: 1
continously updating curve between points (Read 468 times)
continously updating curve between points
Nov 26th, 2008, 4:58pm
 
This seems fairly simple, but I can't find an example that covers it.  I want to draw a curve (trig, quadratic, whatever- only bezier curves have this functionality built-in) between two points.  A little more complicated, I'd also like to be able to make it continuously cycle through what segment of the curves it uses between the points (i.e. an oscillating sine curve).  

Thanks,
Zach
Re: continously updating curve between points
Reply #1 - Nov 27th, 2008, 1:29am
 
An example:
Code:
float x1, x2, ym;
float p1x, p2x, p3x;
float p1y, p2y, p3y;
float flatness = 50;

float angle;

void setup()
{
 size(800, 400);
 x1 = 20; x2 = width - 20;
 ym = height/2;
 
 // Just keep fixed x pos for now
 // we might do variations later
 p1x = lerp(x1, x2, 0.25);
 p2x = lerp(x1, x2, 0.50);
 p3x = lerp(x1, x2, 0.75);
}

void draw()
{
 background(#CCFFEE);
 fill(#FF9955);
 ellipse(x1, ym, 10, 10);
 ellipse(x2, ym, 10, 10);
 stroke(#8899FF);
//  line(x1, ym, x2, ym);

 angle += 0.1;
 p1y = height/2 + (height/2 - 11) * sin(angle);
 // Central point don't move much
 p2y = height/2 * (1.0 - 0.33 * cos(angle * 0.11));
 p3y = height/2 + (height/2 - 17) * cos(angle);

 noFill();
 beginShape();
 vertex(x1, ym);
 bezierVertex(x1 + flatness, ym,
     p1x - flatness, p1y,
     p1x, p1y);
 bezierVertex(p1x + flatness, p1y,
     p2x - flatness, p2y,
     p2x, p2y);
 bezierVertex(p2x + flatness, p2y,
     p3x - flatness, p3y,
     p3x, p3y);
 bezierVertex(p3x + flatness, p3y,
     x2 - flatness, ym,
     x2, ym);
 endShape();
}

You can randomize some parameters, like flatness, variations on angles of anchors, speed of angle changing, separate angle for each point,  etc.
Re: continously updating curve between points
Reply #2 - Dec 1st, 2008, 9:49pm
 
Thanks.  I still can't figure out how to draw multiple curves by looping through the same shape.  I'm also not sure how to modify this to use different y values for the beginning and end-y1 and y2, instead of ym.  Ultimately, I want something like this, but with continuously updating curves instead of lines:

int startX = 0;
int startY = 0;

void setup() {
 size (1000, 800);
 background(0);
 smooth();
}

void draw() {
 if(mousePressed) {
   color randColor = color( random (0, 255), random (0, 255), random (0, 255));
   stroke(randColor, 50);
   float speed = abs(startX-mouseX) + abs (startY-mouseY);
   strokeWeight(speed/5);
   strokeCap(SQUARE);
   line(startX, startY, mouseX, mouseY);
   startX = mouseX;
   startY = mouseY;
 }
}
Page Index Toggle Pages: 1