-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathBSPLINE.PAS
125 lines (106 loc) · 2.38 KB
/
BSPLINE.PAS
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
program BSplineCurve;
{$N+}
uses
Crt, Graph;
const
NumDots = 5;
type
xyzArray = Array[0..100,1..3] of LongInt;
var
N,K,GraphDriver, GraphMode, ErrorCode : Integer;
Ch : Char;
knotK, knotN : Integer;
function Knot (I : Integer) : Integer;
begin
If I < knotK then
Knot := 0
else
If I > knotN then Knot := knotN - knotK + 2
else
Knot := i - knotK + 1;
end;
function NBlend (I,K : Integer; U : Real) : Real;
var
T : Integer;
V : Real;
begin
If K <= 1 then
begin
V := 0;
If (Knot(I) <= U) and (U < Knot(I+1)) then V := 1;
end
else
begin
V := 0;
T := Knot(I+K-1)-Knot(I);
If T <> 0 then V := (u-Knot(i))*NBlend(i,K-1,u)/t;
t := knot(i+K) - knot(i+1);
If T <> 0 then
V := V + (Knot(I+K)-U)*NBlend(I+1,K-1,U)/t;
end;
NBlend := V;
end;
procedure BSpline (var X,Y,Z : Real; U : Real; N,K : Integer; P : xyzArray);
var
I : integer;
B : Real;
begin
KnotK := K;
KnotN := N;
X := 0;
Y := 0;
Z := 0;
For I := 0 to N do
begin
B := NBlend(I,K,U);
X := X + p[I,1]*B;
Y := Y + p[I,2]*B;
end;
end;
procedure DrawCurve;
var
ControlPoints : xyzArray;
I : LongInt;
X, Y, Z : Real;
begin
For I := 0 to 30 do
ControlPoints[I,3] := 0;
For I := 0 to NumDots-1 do
begin
ControlPoints[I,1] := Random(GetMaxX);
ControlPoints[I,2] := Random(GetMaxY);
end;
SetColor (LightCyan);
SetLineStyle (UserBitLn,1,NormWidth);
For I := 0 to NumDots-1 do
If I = 0 then
MoveTo (ControlPoints[I,1],ControlPoints[I,2])
else
LineTo (ControlPoints[I,1],ControlPoints[I,2]);
SetLineStyle (SolidLn,0,NormWidth);
SetColor (Yellow);
N := NumDots-1;
K := 3;
SetColor (K+8);
For I := 0 to 2300 do
begin
BSpline(X,Y,Z,I/2301*(N-K+2),N,K,ControlPoints);
If I = 0 then
MoveTo (Round(X),Round(Y))
else
LineTo (Round(X),Round(Y));
If Keypressed then Halt;
end;
end;
begin
GraphDriver := Detect;
InitGraph (GraphDriver, GraphMode, '..');
ErrorCode := GraphResult;
If ErrorCode <> grOk then Halt;
SetColor (GetMaxColor);
Randomize;
DrawCurve;
Ch := ReadKey;
ClearDevice;
CloseGraph;
end.