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 › curvePoint and curveVertex commands
Page Index Toggle Pages: 1
curvePoint and curveVertex commands (Read 728 times)
curvePoint and curveVertex commands
Feb 4th, 2006, 8:06pm
 
I want to find points along the entire length of a curve, and there doesn't seem to be a way to do this using the curvePoints command.  Currently, curvePoints only accepts four variables, with the inside two measured as being on the curve, and the outside acting only as control handles. This means that the values curvePoints returns are all between my middle two variables. How can I find values between my first two variables or my last two variables? The way I can get those parts of the spline to draw is by including the first and last curveVertex() twice, but there doesn't seem to be a similar 'hack' to get the values of points on those curves.

I want to eventually add mouse control to the following "seaweed" generator, which is why I don't want to clip the ends of the curve or leave long threads trailing.

Thanks for any suggestions.

class Seaweed{
 //instance properties
 float x1;
 float y1;
 float x2;
 float y2;
 float x3;
 float y3;
 float x4;
 float y4;
 float w;
 int branches = 12;

 //constructor
 Seaweed(float x1, float y1, float x2, float y2, float x3, float y3, float x4, float y4, float w){
   this.x1 = x1;
   this.y1 = y1;
   this.x2 = x2;
   this.y2 = y2;
   this.x3 = x3;
   this.y3 = y3;
   this.x4 = x4;
   this.y4 = y4;
   this.w = w;

   createStem();
   createWebbing();
   createBranches();
 }

 float getFirstX(){
   return x1;
 }
 float getFinalX(){
   return x4;
 }
 float getFirstY(){
   return y1;
 }
 float getFinalY(){
   return y4;
 }

 void createStem(){
   stroke(150,60,40);
   strokeWeight(1);
   beginShape(LINE_STRIP);
   curveVertex(x1, y1);
   curveVertex(x1, y1);
   curveVertex(x2, y2);
   curveVertex(x3, y3);
   curveVertex(x4, y4);
   curveVertex(x4, y4);
   endShape();
 }
 void createBranches(){
   stroke(150,60,40);
   strokeWeight(1);
   float centrality = 1;
   float centerX = curvePoint(x1,x2,x3,x4,.5);
   float centerY = curvePoint(y1,y2,y3,y4,.5);
   for (int i = 0; i < branches; i++) {
     float t = i / float(branches);
     float x = curvePoint(x1, x2, x3, x4, t);
     float y = curvePoint(y1, y2, y3, y4, t);
     centrality = 1 - dist(x,y,centerX,centerY)/dist(x2,y2,centerX,centerY);
     line(x, y, x+w*(centrality), y);
     line(x, y, x-w*(centrality), y);
   }
 }
 void createWebbing(){
   fill(200,50,60,127);
   noStroke();
   float centrality = 1;
   float centerX = curvePoint(x1,x2,x3,x4,.5);
   float centerY = curvePoint(y1,y2,y3,y4,.5);
   beginShape(POLYGON);
   curveVertex(x1,y1);
   for (int i = 0; i < branches; i++) {
     float t = i / float(branches);
     float x = curvePoint(x1, x2, x3, x4, t);
     float y = curvePoint(y1, y2, y3, y4, t);
     centrality = 1 - dist(x,y,centerX,centerY)/dist(x2,y2,centerX,centerY);
     curveVertex(x+w*centrality,y);
   }
   curveVertex(x3,y3);
   curveVertex(x4,y4);
   endShape();
   beginShape(POLYGON);
   curveVertex(x1,y1);
   for (int i = 0; i < branches; i++) {
     float t = i / float(branches);
     float x = curvePoint(x1, x2, x3, x4, t);
     float y = curvePoint(y1, y2, y3, y4, t);
     centrality = 1 - dist(x,y,centerX,centerY)/dist(x2,y2,centerX,centerY);
     curveVertex(x-w*centrality,y);
   }
   curveVertex(x3,y3);
   curveVertex(x4,y4);
   endShape();
 }
}
Re: curvePoint and curveVertex commands
Reply #1 - Feb 6th, 2006, 4:02pm
 
you'd just use curvePoint the same way, but you'd have to split things up for your three segments.

if your points are a, b, c, d:

float p1 = curvePoint(a, a, b, c, t);
float p2 = curvePoint(a, b, c, d, t);
float p3 = curvePoint(b, c, d, d, t);

and for the first third you'd use p1, the second third you'd use p2 and for the final you'd use p3.

it's not a hack, it's just math Smiley

(alternatively you could use other sorts of curves with more control points and just interpolate along that, but we'll stick w/ your original question)
Re: curvePoint and curveVertex commands
Reply #2 - Feb 7th, 2006, 6:29am
 
Thanks Ben.  That makes complete sense.  I'll post the finished product on codetree when it's together (a partial implementation is already up).
Page Index Toggle Pages: 1