Hi,
I am new to computer graphic calculations. When I tried to write a program to draw a b-spline according to the points given, but the result makes me confused.
According to the formula
Ni,p(t) = Ni,p(t) (t - ti)/(ti+p - ti) + Ni+1,p(t) (ti+p - t)/(ti+p - ti+1) ,
Ni,0 = {1 if ti <= t <= ti+1 , 0 otherwise }
so I had my methods to compute the b-spline basis and set the path like below:
|
Code:
|
private double bsplineBasis(double u, int i, int p) {
if(p==0){
if(U[i]<=u&& u<U[i+1]){
return 1;
}
else{
return 0;
}
}
else{
double t1 = (u-U[i])/(U[i+p]-U[i]);
double t2 = (U[i+p+1]-u)/(U[i+p+1]-U[i+1]);
double n1 =bsplineBasis(u,i,p-1);
double n2 =bsplineBasis(u,i+1,p-1);
double c1 =0;
if (n1!=0) {
c1 =t1*n1;
}
double c2 =0;
if (n2!=0) {
c2 =t2*n2;
}
return c1 + c2;
}
}
private void setPath() {
path.reset();
int n = points.length;
int w = getWidth();
for(int j = 0; j <= w; j++) {
double t = (double)j/w; // [0 <= t <= 1.0]
double x = 0;
double y = 0;
for(int k = 0; k < n; k++) {
x += bsplineBasis(t,k,p)*points[k].x;
y += bsplineBasis(t,k,p)*points[k].y;
}
if(j > 0)
path.lineTo(x,y);
else
path.moveTo(x,y);
}
} |
the variables used:
Point[] points;
double U[] = {0,0,0,0,0.5,1,1,1,1};
int p = 3;
And the result is like that:
It looks like the point[0,0] is automatically added as the last control point. since I have enforced the path end at the last control point, so the path turns sharply at the last part.
Anyone can tell me what is wrong with the methods, thanks very much..