逐点比较发生成直线的算法

类别:软件工程 点击:0 评论:0 推荐:

{*****************************************}
{以下是逐点比较发生成直线的算法,          }
{如有建议或意见,请与我联系.QQ:287179523   }
{Email:[email protected]           }
{Copy Right (c) 2003-2004 Reserved.       }
{*****************************************}
unit uniteline;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls;

type
  Tlinefrm = class(TForm)
    StaticText1: TStaticText;
    StaticText2: TStaticText;
    input1: TEdit;
    input2: TEdit;
    input3: TEdit;
    input4: TEdit;
    Button1: TButton;
    GroupBox1: TGroupBox;
    Image1: TImage;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure input1KeyPress(Sender: TObject; var Key: Char);
    procedure input2KeyPress(Sender: TObject; var Key: Char);
    procedure input3KeyPress(Sender: TObject; var Key: Char);
    procedure input4KeyPress(Sender: TObject; var Key: Char);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  linefrm: Tlinefrm;

implementation

{$R *.dfm}

procedure Tlinefrm.Button1Click(Sender: TObject);
//说明,我的第一点取(x0+1,y0);并且在第一象限内
var
 i,j,x0,y0,xe,ye,x,y,n,n1,n2:integer;
 f:array[1..1024] of integer;
begin
 linefrm.GroupBox1.Visible:=true;
 linefrm.GroupBox1.Align:=alclient;
  j:=1;
 x0:=strtoint(input1.Text);
 y0:=strtoint(input2.Text);
 xe:=strtoint(input3.Text);
 ye:=strtoint(input4.Text);
 x:=x0+1;
 y:=y0;
 n1:=xe-x0;
 n2:=ye-y0;
 //fm:=ym*xa-ya*xm=>f1:=-y1  point(1,0)
 f[1]:=0-ye;
 n:=n1+n2;
   for i:=0 to n-1 do
   begin
    linefrm.Image1.Canvas.Pixels[x,y]:=clred;
    if f[j]<0 then
      begin
       y:=y+1;j:=j+1;
       f[j+1]:=f[j]+xe;
      end
    else if f[j]>=0 then
      begin
      j:=j+1;
      f[j+1]:=f[j]-ye;
      x:=x+1;
      end;
  end;

end;
procedure Tlinefrm.FormCreate(Sender: TObject);
begin
 linefrm.Left:=0;
 linefrm.Top:=0;
 linefrm.Width:=600;
 linefrm.Height:=450
end;

procedure Tlinefrm.input1KeyPress(Sender: TObject; var Key: Char);
begin
if ord(key)=13 then
input2.SetFocus;
end;

procedure Tlinefrm.input2KeyPress(Sender: TObject; var Key: Char);
begin
if key=#13 then
input3.SetFocus;
end;

procedure Tlinefrm.input3KeyPress(Sender: TObject; var Key: Char);
begin
if key=#13 then
input4.SetFocus;
end;

procedure Tlinefrm.input4KeyPress(Sender: TObject; var Key: Char);
begin
if key=#13 then
button1.SetFocus;
end;

end.

本文地址:http://com.8s8s.com/it/it33167.htm