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 › Sample Bezier Curve X, Y Coordinates
Page Index Toggle Pages: 1
Sample Bezier Curve X, Y Coordinates? (Read 1648 times)
Sample Bezier Curve X, Y Coordinates?
Sep 3rd, 2009, 8:32pm
 
Hello,

I'm trying to design a generic animation curve editor. Before I write my own bezier curve function, I'm wondering if this may have already been done before?

I am aware of the bezier() and bezierPoint() functions in processing. But what I am hoping to do it to be able to input an x (time) coordinate of the curve I wish to sample. And have the function return the y value of the curve for that point in time.

Does anyone have any pointers in this area? I'm fairly new to processing. Is there a way I can take a peak at the underliying code to the bezier function?

Thanks in Advance,

Dan.
Re: Sample Bezier Curve X, Y Coordinates?
Reply #1 - Sep 4th, 2009, 1:39am
 
danielt wrote on Sep 3rd, 2009, 8:32pm:
Is there a way I can take a peak at the underliying code to the bezier function

PGraphics.java
Re: Sample Bezier Curve X, Y Coordinates?
Reply #2 - Sep 5th, 2009, 12:00am
 
Thanks for the link. A bit advanced for me at the moment to try to de-syfer at this low level I think.

Here's an example:
Code:

float i;

void setup(){
size(100,100);
}

void draw(){
background(100);
noFill();
bezier(0, 100, 90, 90, 10, 10, 100, 0);
fill(255);
int steps = 100;
 i = mouseX;
 float t = i / float(steps);
 float x = bezierPoint(0, 90, 10, 100, t);
 float y = bezierPoint(100, 90, 10, 0, t);
 ellipse(x, y, 5, 5);
}


if you scrub mouse in x you can see that the ellipse doesn't stay locked in x with mouseX. This is because the ellipse x,y is derived based on the length of the curve and not the distance traveled in mouseX. I was hoping for a secret formular for this Smiley I looked at a few bezier curve examples on the net and they all seem to derive the point on the curve by halving linear interpolation between control and anchor points http://www.cubic.org/docs/bezier.htm

Is there another way to plot a bezier curve than this? I imagine there's a common approach because ever animation package has this ability. Just wondering if anyone knows what this approach is?

Thanks again,

Dan.
Re: Sample Bezier Curve X, Y Coordinates?
Reply #3 - Sep 5th, 2009, 5:02am
 
ok, this is from memory so could be wrong.

you have control points p0-p3and from these it draws a curve using

p(t) =
 1 * p0 * t^0 * (1-t)^3 +
 3 * p1 * t^1 * (1-t)^2 +
 3 * p2 * t^2 * (1-t)^1 +
 1 * p3 * t^3 * (1-t)^0

(see wikipedia page. n^0 is 1 and n^1 = n which simplifies things but... )

where t (time) goes from 0 to 1 (that's what the t in bezierPoint() is doing)

for the value of y that matches the given x you're going to have to find t in the above equation so that p(t).x = your x. which doesn't strike me as trivial.

and yes using t = .5 doesn't mean that you're halfway along the curve. but it might be a start. use a binary chop or newton-raphson (i seem to recall someone has done this)

ah, jo wood brings the maths and philho brings the code:
http://processing.org/discourse/yabb2/num_1219808869.html

there's also this
http://processing.org/discourse/yabb2/num_1240800499.html
which sounds like what you might be trying to do - someone's attempt at a timeline.
Re: Sample Bezier Curve X, Y Coordinates?
Reply #4 - Sep 6th, 2009, 2:27am
 
Thanks koogy,

I will check out the formula when I get some time. But in the mean time those links you posted were very interesting. Especially the Timeline program. Sounds almost exactly what I was looking for!

Cheers.
Page Index Toggle Pages: 1