解决方法:
利用计算机画直线的方法(直线差补法):
以终点(xe,ye)位于起点为原点的坐标系的第一象限为例:判断函数f=y*xe-x*ye的大小,f>=0,向+x方向增加1画点,f<0,向+y方向增加1画点。
执行到x=xe,y=ye为止,下面是示例程序(其他象限你自己推吧):
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls,StdCtrls, Buttons;
type
TForm1 = class(TForm)
Button1: TButton;
Timer1: TTimer;
procedure Button1Click(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
a1,b1,a2,b2,x,y,f:integer; //起点(a1,b1),终点(a2,b2)
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
a1:=20;
b1:=13;
a2:=160;
b2:=260;
x:=0;
y:=0;
Timer1.Enabled:=true ;
form1.Canvas.MoveTo(a1,a2);
end;
procedure TForm1.Timer1Timer(Sender: TObject);
begin
f:=(b2-b1)*x-(a2-a1)*y;
if (a1+x<>a2) or (b1+y<>b2) then
begin
if f>=0 then
begin
y:=y+1;
form1.Canvas.Pixels[a1+x,b1+y]:=clBlack
end
else
begin
x:=x+1;
form1.Canvas.Pixels[a1+x,b1+y]:=clBlack;
end
end
end;
end.