Some formulas in the referenced web site are not correct. Especially the result formula for a
1 is wrong.
You must understand that you have the freedom to chose the t
1 for your P
1: by varying t
1 for your chosen P
1, the shape of the curve will vary too.
Assuming you have three defined points:
- start (t = 0) at P
0
- end (t = 1) at P
2
- a point P
1 between start and end where you also decide for a t
1 (e.g. 0.5).
Given these four parameters (P
0, P
2, P
1, t
1), the needed curve coefficients are:
a
0 = A
a
1 = B - t
1 C
a
2 = C - B
Where
A = P
0
B = (P
1 - P
0) / ((1 - t
1) t
1)
C = (P
2 - P
0) / (1 - t
1)
If working in 2D, each coefficient consists of two values, one for each axis - working in 3D, each coefficient consists of 3 values, on per axis.
E.g. for each axis coeficient value, you set the respective axis value of the points P
u plus the parameter t
1 value into the formulas above.
Then you sweep the parameter t from 0 to 1 and calculate the curve points P(t) to draw.
pseudo code:
class Formula
{
private Point a0;
private Point a1;
private Point a2;
private Point A(Point p0, Point p1, Point p2, double t1) { return p0; }
private Point B(Point p0, Point p1, Point p2, double t1) { double d = 1/((1-t1)*t1); return (p1-p0)*d; }
private Point C(Point p0, Point p1, Point p2, double t1) { return d = 1/(1-t1); return (p2-p0)*d; }
public Formula(Point p0, Point p1, point p2, double t1)
{
Point a = A(p0, p1, p2, t1);
Point b = B(p0, p1, p2, t1);
Point c = C(p0, p1, p2, t1);
a0 = a;
a1 = b-c*t1;
a2 = c-b;
}
public Point P(double t) { return ((a2*t)+a1)*t+a0; }
}
...
var start = new Point(0,2);
var end = new Point(7,6);
var p1 = new Point(2,3);
double t1 = 0.5;
var Formula curve = new Formula(start, p1, end, t1);
...
int steps = 100;
double step = 1.0/(double)steps;
Point curr = p0;
foreach(int i = 1; i <= steps; ++i)
{
Point next = curve.P(step*(double)i);
Draw(curr, next);
curr = next;
}
Cheers
Andi