FAQ
Cover
This is the archive Discourse for the Processing (ALPHA) software.
Please visit the new Processing forum for current information.

   Processing 1.0 _ALPHA_
   Programming Questions & Help
   Programs
(Moderators: fry, REAS)
   How to evaluate a closed Bezier
« Previous topic | Next topic »

Pages: 1 
   Author  Topic: How to evaluate a closed Bezier  (Read 460 times)
skloopy

WWW
How to evaluate a closed Bezier
« on: Aug 9th, 2003, 7:40am »

I've been looking all over the internet for how to evaluate the X, Y and tangents of a closed bezier curve (where it loops around and has no beginning or end) but i havn't found anything.
 
Do any of you know how to do that sort of thing?
 
fry


WWW
Re: How to evaluate a closed Bezier
« Reply #1 on: Aug 14th, 2003, 6:37pm »

not sure i follow.. do you have an example image? the bezier() functions in p5 won't do it? or are you trying to use multiple segments?
 
skloopy

WWW
Re: How to evaluate a closed Bezier
« Reply #2 on: Aug 21st, 2003, 3:03am »

Hi sorry it took me so long to reply.
 
This is what I mean. I want to be able to draw this:
Code:
beginShape(LINE_LOOP);
bezierVertex(20,20);
bezierVertex(180,20);
bezierVertex(180,180);
bezierVertex(20,180);
endShape();

and then determine the XY of a certain point on the curve, but P5 doesn't close the loop like it should. With those 4 points it should draw more or less a circle.
 
I was also wondering about this:
 
Code:
beginShape(LINE_STRIP);
bezierVertex(20,20);
bezierVertex(180,20);
bezierVertex(180,180);
bezierVertex(20,180);
bezierVertex(20,50);
endShape();

P5 doesn't draw the 5th point, but i was wondering anyway how to get the XY on a bezier more than 4 points long. Is it really complicated?
 
ryan
 
flight404

WWW Email
Re: How to evaluate a closed Bezier
« Reply #3 on: Aug 21st, 2003, 5:53am »

I think part of the problem (please slap me around if I am wrong) is that when using bezierVertex, the first vertex is the first anchor point, the second vertex is the first control point, the third vertex is the second control point, and the fourth anchor is the second anchor point.
 
I was able to make a circle thusly...
 
Code:

beginShape(LINE_STRIP);  
bezierVertex(100,50);  
bezierVertex(200,50);  
bezierVertex(200,200);  
bezierVertex(100,200);
endShape();
 
beginShape(LINE_STRIP);  
bezierVertex(100,200);  
bezierVertex(0,200);  
bezierVertex(0,50);  
bezierVertex(100,50);
endShape();  

 
...but admittedly, my knowledge of bezier curves is rather base.  However, I know nothing about the math for finding points on that curve.  But after a little searching and poking, I found this post by Fry about recent additions to Processing.  Does this help
 
- a new function has been added to calculate points along a bezier curve, rather than just drawing it. so for the old function: bezier(x1, y1, x2, y2, x3, y3, x4, y4);  
 
if you wanted to draw 10 segments along it:  
 
beginShape(LINE_STRIP);  
   for (int i = 0; i <= 10; i++) {  
     float t = i / 10.0;  // t should go from 0..1 along the curve  
     float x = bezier(x1, x2, x3, x4, t);  // get the x coordinate  
     float y = bezier(y1, y2, y3, y4, t);  // get the y coordinate  
     vertex(x, y);  
   }  
endShape();  
  
   this was also asked for/inquired about in the following bboard posts:  
   http://proce55ing.net/discourse/yabb/board_Proce55ing_Software__action_display_num_1053449577.html  
   http://proce55ing.net/discourse/yabb/board_Proce55ing_Software__action_display_num_1057393989.html
« Last Edit: Aug 21st, 2003, 5:54am by flight404 »  
skloopy

WWW
Re: How to evaluate a closed Bezier
« Reply #4 on: Aug 21st, 2003, 7:27pm »

Thanks flight404, that's a good idea, but i was thinking of something a little different
 
I think that this is sort of what i'm thinking, except this is would be using p5 curves, not beziers. I'm still looking for a good example of beziers. Maybe what i'm talking about are b-splines?
 
http://www.cse.unsw.edu.au/~lambert/splines/natcubicclosed.html
« Last Edit: Aug 21st, 2003, 7:32pm by skloopy »  
skloopy

WWW
Re: How to evaluate a closed Bezier
« Reply #5 on: Aug 21st, 2003, 11:08pm »

Here's my progress so far.
 
I tried to do it using the new bezier function, but it didn't work for this. I'm guessing that beziers and b-splines are not the same? Every example of doing this i've found is way too confusing so i've just been doing it my own way.. here's where i am so far. Any suggestions?
 
Code:
float xp[] = {20,180,180,20,100};
float yp[] = {20,20,180,180,100};
 
void setup()
{
  size(200,200);
}
 
void loop()
{
  bspline(xp, yp, 50);
}
 
void bspline(float x[], float y[], int res)
{
  int v1, v2, v3;
  int nv = x.length;
 
  float res3 = res * 3;
  float onethird = 1.0f / 3.0f;
 
  for(int v = 0; v < nv; v++) {
    point(x[v], y[v]);
  }
 
  beginShape(LINE_LOOP);
  for(int v = 0; v < nv; v++) {
    v1 = (v + 1) % nv;
    v2 = (v + 2) % nv;
    v3 = (v + 3) % nv;
 
    for(int i = 1; i < res; i++) {
 float t = onethird + i / res3;  // t should go from 0..1 along the curve
 float vx = bezier(x[v], x[v1], x[v2], x[v3], t);  // get the x coordinate
 float vy = bezier(y[v], y[v1], y[v2], y[v3], t);  // get the y coordinate
 vertex(vx + random(0), vy + random(0));
    }
  }
  endShape();
}

ry
 
fry


WWW
Re: How to evaluate a closed Bezier
« Reply #6 on: Aug 22nd, 2003, 1:19am »

yes, beziers and b-splines are indeed different.  
 
and flight404 is correct in the assertion that the first pt is the on-curve, then middle two are the control, and the next is another on-curve. i believe illustrator, for instance, uses four such bezier segments to draw a circle properly.  
 
from the reference, you can see how the on-curve points relate to the control points:
http://proce55ing.net/reference/bezier_.html
the orange lines are like the 'handles' from an app like adobe illustrator.
 
fwiw, for a corner point, the control point and on-curve point are the same. i always use something like illustrator to mess around with this till it starts making sense.
 
not very obvious conceptually.. that's why we included the generic curve() function, which uses catmull-rom curves, which are more intuitive, but sometimes less 'functional' (since everything uses beziers..)
 
skloopy

WWW
Re: How to evaluate a closed Bezier
« Reply #7 on: Aug 22nd, 2003, 2:52am »

Hmm
 
I guess i'm gonna have to keep looking for how to draw b-splines. Or is it possible in p5 to draw a smoothly closed catmull-rom curve? I tried using LINE_LOOP with curveVertex() but it doesn't close right.
 
fry


WWW
Re: How to evaluate a closed Bezier
« Reply #8 on: Aug 22nd, 2003, 4:39am »

to back up, what is it you're hoping to do with these?  
 
for instance, if you're just looking to evaluate points on a circle, that's gonna be a lot simpler..  
 
for what it's worth, these aren't p5-specific issues, it's more about how the curves themselves work. all we can do is implement
 
skloopy

WWW
Re: How to evaluate a closed Bezier
« Reply #9 on: Aug 23rd, 2003, 6:37am »

Sorry I should've been more clear.
 
Here's what I'm trying to do. I want to take this typing sketch i made..
 
http://elfman.vendetta.com/ryan/type/
 
(just click inside the applet and then start typing) ..and I'm trying to make a bubble around the paragraph that's being typed. I was thinking that i'd need more that 4 points and that's why I was saying b-splines might work.
 
But the problem is that i'm not sure how to draw a closed curve so that there is no hard corner at the beginning and the end. I thought about using just a basic shape like a circle or something, but I want to control the shape with springs so that wouldn't work.
 
So I guess what i've figured out is that i'm gonna need to use a closed b-spline but i'm having trouble finding out how to make one because all the stuff on the internet about it is really technical with math equations and stuff.. *cringe* ..unless there's a way to do that with p5's beziers, but they can only have 4 control points.
 
I guess I should add bsplines to the suggestions list
 
Sorry that's kind of long but i hope it makes it clearer.
 
Pages: 1 

« Previous topic | Next topic »