S
starpc
Unregistered / Unconfirmed
GUEST, unregistred user!
下面我用polybezier API函数(黑色线)和DrawBezier算法(红色线)分别画了二条曲线,但是这两条曲线不完全吻合。大约差1个象素。
请问:
1.这是计算精度的问题还是算法问题?
2.有没有和系统polybezier API函数完全一致的算法?
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs;
type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
procedure FormPaint(Sender: TObject);
procedure DrawBezier(point:array of Tpoint;PosNum:integer);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
point:array[0..3] of Tpoint;
implementation
{$R *.dfm}
procedure TForm1.DrawBezier(point: array of Tpoint; PosNum: integer);
var c,b,p,q:array[0..10] of single;
t, xe, ye:single;
i,j,k:integer;
begin
canvas.Pixels[point[0].x,point[0].y]:=clred;
canvas.MoveTo(point[0].x,point[0].y);
For i:= 0 To PosNum do
c:= 1;
For i:= 2 To PosNum do
begin
For j:= 1 To i - 1 do
b[j] := c[j - 1] + c[j];
For j:= 1 To i - 1 do
c[j] := b[j];
end;
For i := 0 To PosNum * 4 do
begin
t := i / (PosNum * 4);
p[0] := 1; q[0] := 1;
For j := 1 To PosNum do
begin
p[j] := p[j - 1] * t ;
q[j] := q[j - 1] * (1 - t);
end;
For j := 0 To PosNum do
b[j] := p[j] * q[PosNum - j] ;
xe := 0; ye := 0;
For j:= 0 To PosNum do
begin
xe := xe + c[j] * b[j] * point[j].X;
ye := ye + c[j] * b[j] * point[j].Y;
end;
canvas.LineTo(trunc(xe),trunc(ye));
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
point[0].x:=80; point[0].y:=280;
point[1].x:=350; point[1].y:=200;
point[2].x:=180; point[2].y:=140;
point[3].x:=200; point[3].y:=200;
end;
procedure TForm1.FormPaint(Sender: TObject);
var
h:HDC;
i:integer;
begin
h:=getdc(form1.handle);
polybezier(h,point,4);//使用api调用
canvas.Pen.Color:=clred;
DrawBezier(point,3);
end;
end.
请问:
1.这是计算精度的问题还是算法问题?
2.有没有和系统polybezier API函数完全一致的算法?
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs;
type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
procedure FormPaint(Sender: TObject);
procedure DrawBezier(point:array of Tpoint;PosNum:integer);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
point:array[0..3] of Tpoint;
implementation
{$R *.dfm}
procedure TForm1.DrawBezier(point: array of Tpoint; PosNum: integer);
var c,b,p,q:array[0..10] of single;
t, xe, ye:single;
i,j,k:integer;
begin
canvas.Pixels[point[0].x,point[0].y]:=clred;
canvas.MoveTo(point[0].x,point[0].y);
For i:= 0 To PosNum do
c:= 1;
For i:= 2 To PosNum do
begin
For j:= 1 To i - 1 do
b[j] := c[j - 1] + c[j];
For j:= 1 To i - 1 do
c[j] := b[j];
end;
For i := 0 To PosNum * 4 do
begin
t := i / (PosNum * 4);
p[0] := 1; q[0] := 1;
For j := 1 To PosNum do
begin
p[j] := p[j - 1] * t ;
q[j] := q[j - 1] * (1 - t);
end;
For j := 0 To PosNum do
b[j] := p[j] * q[PosNum - j] ;
xe := 0; ye := 0;
For j:= 0 To PosNum do
begin
xe := xe + c[j] * b[j] * point[j].X;
ye := ye + c[j] * b[j] * point[j].Y;
end;
canvas.LineTo(trunc(xe),trunc(ye));
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
point[0].x:=80; point[0].y:=280;
point[1].x:=350; point[1].y:=200;
point[2].x:=180; point[2].y:=140;
point[3].x:=200; point[3].y:=200;
end;
procedure TForm1.FormPaint(Sender: TObject);
var
h:HDC;
i:integer;
begin
h:=getdc(form1.handle);
polybezier(h,point,4);//使用api调用
canvas.Pen.Color:=clred;
DrawBezier(point,3);
end;
end.