Bezier
uses crt,graph;
type PointType = record
x,y : integer;
end;
Mang = array[0..50] of PointType;
var a: Mang;
i, L: integer;
gd,gm: integer;
{ Tao ham tinh tich 2 so }
function tich(x,y:word): real;
var S:real; i:word;
begin
if y<=1 then tich:=1
else
begin
s:=1;
for i:=x to y do s:=s*i;
tich:=s;
end;
end;
{ Tao ham tinh To hop chap K cua L }
function CLK(l,k:word):real;
begin
CLK:=tich(k+1,l)/tich(1,l-k);
end;
{ Tao ham tinh X^mu}
function Xmu(x:real;mu:word):real;
var i:word; s:real;
begin
if mu=0 then s:=1
else begin
s:=1;
for i:=1 to mu do s:=s*x;
end;
Xmu:=s;
end;
{Tao ham tinh Bkl - da thuc Bernstein}
function BKL(t:real; l,k:word):real;
begin
BKL:=CLK(l,k)*Xmu(1-t,l-k)*Xmu(t,k);
end;
{ Tao thu tuc tim diem Bezier P(t)}
procedure Pt(t:real;L:word;A:Mang;var diem: PointType);
var k:word; s,x,y:real;
begin
x:=0;
y:=0;
for k:=0 to L do
begin
s:=BKL(t,l,k);
x:=x+A[k].x*s;
y:=y+A[k].y*s;
end;
diem.x:=round(x);
diem.y:=round(y);
end;
{Thu tuc ve duong cong Bezier}
procedure Vebezier(a:Mang;L:integer);
var i,SoDiem:word; Diem:PointType;
dx,x:real;
begin
sodiem:=100;
dx:=1/sodiem;
x:=0;
if L>0 then
begin
for i:=1 to sodiem+1 do
begin
Pt(x,L,a,Diem);
if i=1 then moveto(round(diem.x),round(diem.y))
else lineto(round(diem.x),round(diem.y));
x:=x+dx;
end;
end;
end;
{ Chuong trinh chinh }
begin
clrscr;
gd:=detect;
writeln('Hay nhap so diem Bezier'); readln(L);
writeln('Hay nhap toa do cac diem');
for i:=1 to L do
begin
writeln('a[',i,'] = '); readln(a[i].x,a[i].y);
end;
initgraph(gd,gm,'..\bgi');
Vebezier(a,L);
readkey;
end.
Bạn đang đọc truyện trên: AzTruyen.Top