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 & HelpSyntax Questions › what's value t means in bezierPoint()
Page Index Toggle Pages: 1
what's value t means in bezierPoint() (Read 1145 times)
what's value t means in bezierPoint()
Jul 11th, 2009, 3:12am
 
Hi, I'm new here.
I want to control the speed of a ball moving along the bezier curve. So I want to know what the value t is in bezierPoint(), is it the distance along the curve or the angle or anything else?
thx
Re: what's value t means in bezierPoint()
Reply #1 - Jul 11th, 2009, 3:16am
 
I admit it is not obvious at first sight.
t is between 0 and 1.
Near 0, you get points near one end point (the start). Near 1, you get points near the other end point. Vary continuously t at the needed granularity to get all the points you need on this curve.
Re: what's value t means in bezierPoint()
Reply #2 - Jul 11th, 2009, 6:29pm
 
I know it.
but I want to know what's the relationship between t and the curve length, or in other words, how t control the position of a point on the curve.
any idea?
Re: what's value t means in bezierPoint()
Reply #3 - Jul 12th, 2009, 12:03am
 
Please, read again my answer...
t is percentage of position on the curve. Not expressed in length.
Re: what's value t means in bezierPoint()
Reply #4 - Jul 12th, 2009, 5:12am
 
PhiLho  wrote on Jul 12th, 2009, 12:03am:
Please, read again my answer...
t is percentage of position on the curve. Not expressed in length.

well, of course it is percentage value... Huh

but what kind of persentage is it
I find that in some curves(not all), the distances between neighbour points are changing even if I add the same value to t. so obviously t is not the percentage the distance between start point and current to the whole curve
unfortunately I'm not a computer guy, but I will try to read the code.
thx anyway
Re: what's value t means in bezierPoint()
Reply #5 - Jul 12th, 2009, 5:48am
 
Aah, I understand your problem now. Indeed, the points are closer when the curve is tighter (more curved).
I know the maths of Bézier curves are quite complex, I don't know if it is possible / easy to get something more regular. You can get more samples and pick the ones you need, computing a simple distance between each successive point (dist() should be a good enough approximation if the points are close enough) until you reach the wanted distance.
I fear most computations on Bézier curves (eg. find the closest point to the mouse position) have to use such iterative process.
Re: what's value t means in bezierPoint()
Reply #6 - Jul 12th, 2009, 6:15am
 
I quickly hacked one of my first sketches experimenting with Bézier...
I get more regularly spaced points on the curve, but of course, we can no longer guarantee to end on the end point.
Code:
BezierCurve bc1, bc2;

int margin1 = 20;
int margin2 = 50;

void setup()
{
size(400, 400);

bc1 = new BezierCurve(
new PVector(margin1, height - margin1), // Start point: bottom left
new PVector(margin1, margin2), // Start control point: top left
new PVector(width - margin1, margin2), //End control point: top right
new PVector(width - margin1, height - margin1) // End point: bottom right
);

bc2 = new BezierCurve(
new PVector(margin1, height - margin1), // Start point: bottom left
new PVector(width - margin2, height - margin1), // Start control point: bottom right
new PVector(margin1, margin1), // End control point: top left
new PVector(width - margin1, margin1) // End point: top right
);
}

class BezierCurve
{
PVector m_startPoint, m_endPoint;
PVector m_startControlPoint, m_endControlPoint;
//boolean bDump = true;

BezierCurve(PVector startPoint, PVector startControlPoint, PVector endControlPoint, PVector endPoint)
{
m_startPoint = startPoint;
m_endPoint = endPoint;
m_startControlPoint = startControlPoint;
m_endControlPoint = endControlPoint;
}

/**
* Gets a point on the Bézier curve, given a relative "distance" from the starting point,
* expressed as a float number between 0 (start point) and 1 (end point).
*/
PVector GetPoint(float pos)
{
float x = bezierPoint(m_startPoint.x, m_startControlPoint.x,
m_endControlPoint.x, m_endPoint.x,
pos);
float y = bezierPoint(m_startPoint.y, m_startControlPoint.y,
m_endControlPoint.y, m_endPoint.y,
pos);
return new PVector(x, y);
}

void Draw()
{
bezier(m_startPoint.x, m_startPoint.y,
m_startControlPoint.x, m_startControlPoint.y,
m_endControlPoint.x, m_endControlPoint.y,
m_endPoint.x, m_endPoint.y);
}

void DrawDetailsNb(int pointNb)
{
if (pointNb <= 1) pointNb = 2;
stroke(#FF0000);
strokeWeight(1);

for (int i = 0; i < pointNb; i++)
{
float pos = (float) i / (pointNb - 1);

PVector pt = GetPoint(pos);
ellipse(pt.x, pt.y, 3, 3);
}
}

void DrawDetailsDist(float dist)
{
stroke(#FFFF00);
strokeWeight(1);

// Use an arbitrary number of points, it is hard to estimate
// the length of a curve
int pointNb = 1000;
float totalDist = 0;
PVector prevPt = m_startPoint;
ellipse(prevPt.x, prevPt.y, 7, 7);
for (int i = 1; i < pointNb; i++)
{
float pos = (float) i / (pointNb - 1);

PVector pt = GetPoint(pos);
totalDist += dist(pt.x, pt.y, prevPt.x, prevPt.y);
if (totalDist >= dist)
{
ellipse(pt.x, pt.y, 7, 7);
totalDist = 0;
}
prevPt = pt;
}
}
}

void draw()
{
background(#AACCFF);
noFill();

stroke(#80A0FF);
strokeWeight(3);
bc1.Draw();

bc1.DrawDetailsNb(10);
bc1.DrawDetailsDist(60);

stroke(#80FF80);
strokeWeight(3);
bc2.Draw();

bc2.DrawDetailsNb(10);
bc2.DrawDetailsDist(60);
}
Page Index Toggle Pages: 1