贝塞尔曲线拟合算法!
楼主 · 1 楼
三次贝塞耳曲线,现给出的算法。
//------------------BEZIER--------
procedure tmdichild.newpoint; //2úéúD?μ?
var i,h:integer;
begin h:=bo-1;
if (bo-1)<=4 then howbe:=1
else howbe:=round((bo-1-2)/2);
for i:=2 to howbe do
if (h>2*i) then begin
x1[19+i]:=round((x1[2*i-1]+x1[2*i])/2);
y1[19+i]:=round((y1[2*i-1]+y1[2*i])/2);
end;
x1[20]:=x1[1];y1[20]:=y1[1];
becout:=1;
end;
procedure tmdichild.bezier3ini(var x1,y1,x2,y2,x3,y3,x4,y4:integer);
begin
a0:=x1; a1:=-3*x1+3*x2;
a2:=3*x1-6*x2+3*x3;
a3:=-x1+3*x2-3*x3+x4;
b0:=y1;
b1:=-3*y1+3*y2;
b2:=3*y1-6*y2+3*y3;
b3:=-y1+3*y2-3*y3+y4;
dt:=0.01;
fillgrid(x1,y1,mainform.Panel3.color);
statubar(x1,y1,3);
fillcout:=0;
end;
procedure tmdichild.bezier3;
begin
fillcout:=fillcout+1;
t:=fillcout*dt; t2:=t*t; t3:=t2*t;
bx:=trunc(a0+a1*t+a2*t2+a3*t3);
by:=trunc(b0+b1*t+b2*t2+b3*t3);
fillgrid(bx,by,mainform.Panel3.color);
statubar(bx,by,3);
end;
procedure tmdichild.bezier;
begin
if (howbe-becout)>=1 then
begin
if (ini=true) then begin
bezier3ini(x1[19+becout],y1[19+becout],x1[2*becout],y1[2*becout],
x1[2*becout+1],y1[2*becout+1],x1[20+becout],y1[20+becout]);
ini:=false;
end;
bezier3;
if fillcout=100 then
begin becout:=becout+1; ini:=true; end;
end
else
begin
if ini=true then begin
if ((bo-1) mod 2)=0 then
bezier3ini(x1[19+becout],y1[19+becout],x1[bo-3],y1[bo-3],
x1[bo-2],y1[bo-2],x1[bo-1],y1[bo-1])
else bezier3ini(x1[19+becout],y1[19+becout],x1[bo-2],y1[bo-2],
x1[bo-1],y1[bo-1],x1[bo-1],y1[bo-1]);
ini:=false;
end;
bezier3;
if fillcout=100 then begin
//timer1.enabled:=false;
ini:=true;
//bo:=0;
end;
end;
end;
//------------------BEZIER--------
procedure tmdichild.newpoint; //2úéúD?μ?
var i,h:integer;
begin h:=bo-1;
if (bo-1)<=4 then howbe:=1
else howbe:=round((bo-1-2)/2);
for i:=2 to howbe do
if (h>2*i) then begin
x1[19+i]:=round((x1[2*i-1]+x1[2*i])/2);
y1[19+i]:=round((y1[2*i-1]+y1[2*i])/2);
end;
x1[20]:=x1[1];y1[20]:=y1[1];
becout:=1;
end;
procedure tmdichild.bezier3ini(var x1,y1,x2,y2,x3,y3,x4,y4:integer);
begin
a0:=x1; a1:=-3*x1+3*x2;
a2:=3*x1-6*x2+3*x3;
a3:=-x1+3*x2-3*x3+x4;
b0:=y1;
b1:=-3*y1+3*y2;
b2:=3*y1-6*y2+3*y3;
b3:=-y1+3*y2-3*y3+y4;
dt:=0.01;
fillgrid(x1,y1,mainform.Panel3.color);
statubar(x1,y1,3);
fillcout:=0;
end;
procedure tmdichild.bezier3;
begin
fillcout:=fillcout+1;
t:=fillcout*dt; t2:=t*t; t3:=t2*t;
bx:=trunc(a0+a1*t+a2*t2+a3*t3);
by:=trunc(b0+b1*t+b2*t2+b3*t3);
fillgrid(bx,by,mainform.Panel3.color);
statubar(bx,by,3);
end;
procedure tmdichild.bezier;
begin
if (howbe-becout)>=1 then
begin
if (ini=true) then begin
bezier3ini(x1[19+becout],y1[19+becout],x1[2*becout],y1[2*becout],
x1[2*becout+1],y1[2*becout+1],x1[20+becout],y1[20+becout]);
ini:=false;
end;
bezier3;
if fillcout=100 then
begin becout:=becout+1; ini:=true; end;
end
else
begin
if ini=true then begin
if ((bo-1) mod 2)=0 then
bezier3ini(x1[19+becout],y1[19+becout],x1[bo-3],y1[bo-3],
x1[bo-2],y1[bo-2],x1[bo-1],y1[bo-1])
else bezier3ini(x1[19+becout],y1[19+becout],x1[bo-2],y1[bo-2],
x1[bo-1],y1[bo-1],x1[bo-1],y1[bo-1]);
ini:=false;
end;
bezier3;
if fillcout=100 then begin
//timer1.enabled:=false;
ini:=true;
//bo:=0;
end;
end;
end;
2 楼
这个程序不错