这是关于bresenham 算法的程序,主要就是画直线.
估计应该不是这个问题.
铁上来,以供大家参考.
procedure TForm1.bresen_line(x0,y0,x1,y1:short);
var
x,y,dx,dy,dx1,dy1:short;
xt0,yt0,xt1,yt1:short;
i: short;
e: real;
linecolor:tcolor;
begin
//image1.canvas.moveto(x0,y0+50);
//image1.canvas.LineTo(x1,y1+50);
//linecolor:=rgb(255,0,0);
if(x0>x1) then
begin
xt0:=x1;
yt0:=y1;
xt1:=x0;
yt1:=y0;
end
else
begin
xt0:=x0;
yt0:=y0;
xt1:=x1;
yt1:=y1;
end;
dx:=xt1-xt0;
dy:=yt1-yt0;
x:=xt0;
y:=yt0;
if dy>dx then
begin
dx1:=yt1-yt0;
dy1:=xt1-xt0;
x:=yt0;
y:=xt0;
end
else
if -dy>dx then
begin
dx1:=-(yt1-yt0);
dy1:=xt1-xt0;
x:=-yt0;
y:=xt0;
end
else if (dy<0) and ((-dy)<dx) then
begin
dx1:=xt1-xt0;
dy1:=-(yt1-yt0);
x:=xt0;
y:=-yt0;
end
else
begin
dx1:=xt1-xt0;
dy1:=yt1-yt0;
x:=xt0;
y:=yt0;
end;
e:=-dx1;
for i:=0 to dx1 do
begin
if dy>dx then
image1.canvas.Pixels[y,x]:=mcolor
else if -dy>dx then
image1.canvas.Pixels[y,-x]:=mcolor
else if (dy<0) and (-dy<dx) then
image1.canvas.Pixels[x,-y]:=mcolor
else
image1.canvas.Pixels[x,y]:=mcolor;
x:=x+1;
e:=e+2*dy1;
if e>=0 then
begin
y:=y+1;
e:=e-2*dx1;
end;
end;
end;