B-Spline rajzolás deBoor algoritmussal

Top  Previous  Next

uses Gr, Crt, MetaGr;

 

{ Curves and Surfaces for Computer Aided Geometric Design. A Practical Guide.

Third Edition.

}

 

{ Source: DEBOOR.C

}

 

function deBoor(degree: Integer; var Coeff: array of Real;

var knot: array of Real; u: Real; i: Integer): Real;

{

       uses de Boor algorithm to compute one

       coordinate on B-spline curve for param. value u in interval i.

input:  degree:        polynomial degree of each piece of curve

       coeff:        B-spline control points

       knot:        knot sequence

       u:        evaluation abscissa

       i:        u's interval: u[i]<= u < u[i+1]

output:        coordinate value.

}

var

k, j: Integer;

t1, t2: Real;

coeffa: array[0..30] of Real;                { might need adjustment }

 

Begin

For j := i - degree + 1 to i + 1 do

   coeffa[j] := coeff[j];

For k := 1 to degree do

   For j := i + 1 downto i - degree + k + 1 do Begin

     t1 := (knot[j+degree-k] - u) / (knot[j+degree-k] - knot[j-1]);

     t2 := 1.0 - t1;

     coeffa[j] := t1 * coeffa[j-1] + t2 * coeffa[j]

   End;

deBoor := coeffa[i+1]

End;

 

{ Source: BSPL_TO_.

}

 

procedure bspl_to_points(degree, l: Integer; var coeff_x: array of Real;

var knot: array of Real; dense: Integer; var points_x: array of Real;

var point_num: Integer);

{        generates points on B-spline curve. (one coordinate)

input:        degree:        polynomial degree of each piece of curve

       l:        number of intervals

       coeff:        B-spline control points

       knot:        knot sequence: knot[0]...knot[l+2*degree-2]

       dense:        how many points per segment

Output:        points:        output array.

       point_num: how many points are generated.

}

var

i, j, ii, kk, l2: Integer;

du, u: Real;

mmbox: array[1..4] of Real;

 

 

Begin

l2 := l + degree - 1;

point_num := 0;

for i := degree - 1 to l + degree - 2 do

   if knot[i+1] > knot[i] then Begin

     du := (knot[i+1] - knot[i]) / dense;

     u := knot[i];

     for ii := 0 to dense do Begin

       points_x[point_num] := deboor(degree, coeff_x, knot, u, i);

       Inc(point_num);

       u := u + du

     End

   End

End;

 

const

knot: array[0..9] of Real = (0, 1, 2, 3, 4, 5, 6, 7, 8, 9);

 

var

coeff_x: array[0..20] of Real;

coeff_y: array[0..20] of Real;

points_x: array[0..100] of Real;

points_y: array[0..100] of Real;

i, num: Integer;

 

Begin

Randomize;

SetGrMode(grVgaHiStd);

InitBiosGraph;

For i := 0 to 20 do Begin

   coeff_x[i] := 1e38;

   coeff_y[i] := 1e38

End;

For i := 0 to 100 do Begin

   points_x[i] := 1e38;

   points_y[i] := 1e38

End;

{  For i := 1 to 5 do Begin

   coeff_x[i] := 640*Random;

   coeff_y[i] := 480*Random;

   PutPixel(Round(coeff_x[i]), Round(coeff_y[i]), 10);

End;

coeff_x[6] := coeff_x[1];

coeff_y[6] := coeff_y[1];}

coeff_x[0] :=  20; coeff_y[0] := 450;

coeff_x[1] := 320; coeff_y[1] := 20;

coeff_x[2] := 620; coeff_y[2] := 450;

coeff_x[3] :=  20; coeff_y[3] := 450;        { [0] repeated }

coeff_x[4] := 320; coeff_y[4] := 20;        { [1] repeated }

bspl_to_points(2, 3, coeff_x, knot, 20, points_x, num);

bspl_to_points(2, 3, coeff_y, knot, 20, points_y, num);

SetColor(15);

MoveTo(Round(points_x[0]), Round(points_y[0]));

For i := 1 to num-1 do

   LineTo(Round(points_x[i]), Round(points_y[i]));

ReadKey;

CloseBiosGraph

End.