Hehe, eYes就这样给大家送分?
这里是我以前做的经典的BreshamLine算法和自己的改进PegasusLine
当然常言道:没有最好只有更好,比如就可以从两边一齐划,可以减少
接近一半的计算, eYes如有兴趣可以继续改进
Procedure BreshamLine( X0,Y0,X1,Y1:Integer; Color:Word );
Var
deltaX,deltaY:Integer;
dir,delta:LongInt;
e:Integer;
X,Y:Integer;
Begin
deltaX:=Abs(X1-X0); deltaY:=Abs(Y1-Y0);
If ( deltaX > deltaY ) Then
Begin
Y:=Y0;X:=X0;
e:=2*deltaY-deltaX;
If X1>X0 Then dir:=1 Else dir:=-1;
If Y1>Y0 THen delta:=1 Else delta:=-1;
While (X<>X1) Do
Begin
putpixel(X,Y,Color);
If e>0 Then
Begin
Inc(Y,delta);
Inc(e,2*(deltaY-deltaX));
End
Else Inc(e,2*deltaY);
Inc(X,dir);
End
End
Else
Begin
Y:=Y0;X:=X0;
e:=2*deltaX-deltaY;
If Y1>Y0 Then dir:=1 Else dir:=-1;
If X1>X0 THen delta:=1 Else delta:=-1;
While (Y<>Y1) Do
Begin
putpixel(X,Y,Color);
If e>0 Then
Begin
Inc(X,delta);
Inc(e,2*(deltaX-deltaY));
End
Else Inc(e,2*deltaX);
Inc(Y,dir);
End
End
End;
Procedure PegasusLine( X0,Y0,X1,Y1:Integer; Color:Word );
Var
X, Y, dX, dY, sgn_dX, sgn_dY:Integer;
EXdy, EYdx :LongInt;
Temp1, Temp2, Temp3 :LongInt;
Begin
X:=X0; Y:=Y0; dX:=X1-X0; dY:=Y1-Y0;
If (dX>0)
Then sgn_dX:=1
Else sgn_dX:=-1;
If (dY>0)
Then sgn_dY:=1
Else sgn_dY:=-1;
dX:=dX*sgn_dY;
dY:=dY*sgn_dX;
EXdy:=0; EYdx:=0;
Repeat
PutPixel(X,Y,Color);
Temp1:=Abs(EXdy+dX-EYdx);
Temp2:=Abs(EYdx+dY-EXdy);
Temp3:=Abs(EYdx+dY-EXdy-dX);
If Temp1<Temp2
Then
Begin
Inc(Y,sgn_dY); Inc(EXdy,dX);
If Temp1>Temp3 Then
Begin
Inc(X,sgn_dX); Inc(EYdx,dY);
End
End
Else
Begin
Inc(X,sgn_dx); Inc(EYdx,dY);
If Temp2>Temp3 Then
Begin
Inc(Y,sgn_dY); Inc(EXdy,dX);
End
End
Until (X=X1) and (Y=Y1);
End;