unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, jpeg, ExtCtrls;type TForm1 = class(TForm) RadioButton1: TRadioButton; RadioButton2: TRadioButton; RadioButton3: TRadioButton; ComboBox1: TComboBox; ComboBox2: TComboBox; Label1: TLabel; Label2: TLabel; Image1: TImage; Label3: TLabel; Label4: TLabel; Edit1: TEdit; Button1: TButton; procedure FormCreate(Sender: TObject); procedure Button1Click(Sender: TObject); procedure Image1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); procedure RadioButton1Click(Sender: TObject); private { Private declarations } procedure Drawline(nP:array of TPoint;iColor:TColor); public { Public declarations } end;var Form1: TForm1;implementationvar ps:array of TPoint;//存端点坐标 pCount:integer; //确定端点的数目 Lbl:array[0..2] of TLabel; //显示坐标值的标签{$R *.dfm} procedure TForm1.Drawline(nP:array of TPoint;iColor:TColor);//画线 var i:integer; begin if pCount=3 then begin Image1.Canvas.Pen.Color:=iColor; Image1.Canvas.MoveTo(np[0].X,np[0].Y); for i:=1 to 2 do Image1.Canvas.LineTo(nP[i].X,nP[i].Y); Image1.Canvas.LineTo(nP[0].X,nP[0].Y); end; end;procedure TForm1.FormCreate(Sender: TObject); var i:integer; begin Image1.Align:=alLeft; setlength(ps,3); pCount:=0; Lbl[0]:=Label1; Lbl[1]:=Label2; Lbl[2]:=Label3; for i:=0 to 2 do Lbl[i].Caption:=''; Label4.Caption:='倍数:'; edit1.Text:='2'; RadioButton1.Caption:='以中心不变'; RadioButton2.Caption:='边中点不变:'; RadioButton3.Caption:='角端点不变:'; RadioButton1.Checked:=true; ComboBox2.Items.Append('第一边'); ComboBox2.Items.Append('第二边'); ComboBox2.Items.Append('第三边'); ComboBox2.ItemIndex:=0; ComboBox2.Visible:=false; ComboBox1.Items.Append('第一点'); ComboBox1.Items.Append('第二点'); ComboBox1.Items.Append('第三点'); ComboBox1.ItemIndex:=0; ComboBox1.Visible:=false; Button1.Caption:='画新三角形'; Button1.Enabled:=false; RadioButton2.OnClick:=RadioButton1Click; RadioButton3.OnClick:=RadioButton1Click; end;procedure TForm1.Button1Click(Sender: TObject); var ips:array of TPoint; //新三角形的端点 i,Translation_x,Translation_y:integer;//新三角形中心平移值 Center1,Center2:Tpoint; //新旧中心点 Multiple:Extended; //缩放倍数 begin try Multiple:=strtofloat(edit1.Text); if Multiple<=0 then begin showmessage('倍数输入值无效!!'); edit1.SetFocus; exit; end; except showmessage('倍数输入值无效!!'); edit1.SetFocus; exit; end; if pCount<2 then exit; setlength(ips,3); //按倍数求新三角形的三端点坐标(第一个点不变): ips[0].X:=ps[0].X; ips[0].Y:=ps[0].Y; ips[1].y:=round(Multiple*(ps[1].Y-ps[0].Y))+ps[0].Y; ips[1].x:=round(Multiple*(ps[1].x-ps[0].x))+ps[0].x; ips[2].y:=round(Multiple*(ps[2].Y-ps[0].Y))+ps[0].Y; ips[2].x:=round(Multiple*(ps[2].x-ps[0].x))+ps[0].x; //求中心点(三端点的算术平均值): Center1.X:=(ps[0].X+ps[1].X+ps[2].X) div 3; Center1.y:=(ps[0].y+ps[1].y+ps[2].y) div 3; Center2.X:=(ips[0].X+ips[1].X+ips[2].X) div 3; Center2.y:=(ips[0].y+ips[1].y+ips[2].y) div 3; //新的三角形中心平移值: Translation_x:=Center2.X-Center1.x; Translation_y:=Center2.y-Center1.y; //把新三角形中心点移到原来的中心点上: for i:=0 to 2 do begin ips[i].X:=ips[i].X-Translation_x; ips[i].y:=ips[i].Y-Translation_y; end; if RadioButton2.Checked then //让同名边中点重合: begin if ComboBox2.ItemIndex>0 then begin Translation_x:=(ps[ComboBox2.ItemIndex-1].X-ips[ComboBox2.ItemIndex-1].X)div 2; Translation_y:=(ps[ComboBox2.ItemIndex-1].Y-ips[ComboBox2.ItemIndex-1].Y)div 2; end else begin Translation_x:=(ps[2].X-ips[2].X)div 2; Translation_y:=(ps[2].Y-ips[2].Y)div 2; end; for i:=0 to 2 do begin ips[i].X:=ips[i].X-Translation_x; ips[i].y:=ips[i].Y-Translation_y; end; end else if RadioButton3.Checked then//让同名角端点重合 begin Translation_x:=(ips[ComboBox1.ItemIndex].X-ps[ComboBox1.ItemIndex].X); Translation_y:=(ips[ComboBox1.ItemIndex].Y-ps[ComboBox1.ItemIndex].Y); for i:=0 to 2 do begin ips[i].X:=ips[i].X-Translation_x; ips[i].y:=ips[i].Y-Translation_y; end; end; // Drawline(ps,clWindow);//清除旧三角形的线条 Drawline(ips,clred); end;procedure TForm1.Image1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); begin if pCount<3 then begin ps[pCount].X:=X; ps[pCount].Y:=Y; Lbl[pCount].Caption:='第'+inttostr(pCount) +'个点的坐标为'+inttostr(X)+','+inttostr(Y); inc(pCount); if pCount=3 then begin Drawline(ps,clred);//画线条 Button1.Enabled:=true; end; end; end; procedure TForm1.RadioButton1Click(Sender: TObject); begin ComboBox1.Visible:=false; ComboBox2.Visible:=false; if RadioButton3.Checked then ComboBox1.Visible:=true else if RadioButton2.Checked then ComboBox2.Visible:=true end;end.
斜截式:y=kx+b
截距式:x/a+y/b=1
两点式:(x-x1)/(x2-x1)=(y-y1)/(y2-y1)
一般式:ax+by+c=0
只要知道两点坐标,代入任何一种公式,都可以求出直线的方程。
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, jpeg, ExtCtrls;type
TForm1 = class(TForm)
RadioButton1: TRadioButton;
RadioButton2: TRadioButton;
RadioButton3: TRadioButton;
ComboBox1: TComboBox;
ComboBox2: TComboBox;
Label1: TLabel;
Label2: TLabel;
Image1: TImage;
Label3: TLabel;
Label4: TLabel;
Edit1: TEdit;
Button1: TButton;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Image1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure RadioButton1Click(Sender: TObject);
private
{ Private declarations }
procedure Drawline(nP:array of TPoint;iColor:TColor);
public
{ Public declarations }
end;var
Form1: TForm1;implementationvar ps:array of TPoint;//存端点坐标
pCount:integer; //确定端点的数目
Lbl:array[0..2] of TLabel; //显示坐标值的标签{$R *.dfm}
procedure TForm1.Drawline(nP:array of TPoint;iColor:TColor);//画线
var i:integer;
begin
if pCount=3 then
begin
Image1.Canvas.Pen.Color:=iColor;
Image1.Canvas.MoveTo(np[0].X,np[0].Y);
for i:=1 to 2 do Image1.Canvas.LineTo(nP[i].X,nP[i].Y);
Image1.Canvas.LineTo(nP[0].X,nP[0].Y);
end;
end;procedure TForm1.FormCreate(Sender: TObject);
var i:integer;
begin
Image1.Align:=alLeft;
setlength(ps,3);
pCount:=0;
Lbl[0]:=Label1;
Lbl[1]:=Label2;
Lbl[2]:=Label3;
for i:=0 to 2 do Lbl[i].Caption:='';
Label4.Caption:='倍数:';
edit1.Text:='2';
RadioButton1.Caption:='以中心不变';
RadioButton2.Caption:='边中点不变:';
RadioButton3.Caption:='角端点不变:';
RadioButton1.Checked:=true;
ComboBox2.Items.Append('第一边');
ComboBox2.Items.Append('第二边');
ComboBox2.Items.Append('第三边');
ComboBox2.ItemIndex:=0;
ComboBox2.Visible:=false;
ComboBox1.Items.Append('第一点');
ComboBox1.Items.Append('第二点');
ComboBox1.Items.Append('第三点');
ComboBox1.ItemIndex:=0;
ComboBox1.Visible:=false;
Button1.Caption:='画新三角形';
Button1.Enabled:=false;
RadioButton2.OnClick:=RadioButton1Click;
RadioButton3.OnClick:=RadioButton1Click;
end;procedure TForm1.Button1Click(Sender: TObject);
var ips:array of TPoint; //新三角形的端点
i,Translation_x,Translation_y:integer;//新三角形中心平移值
Center1,Center2:Tpoint; //新旧中心点
Multiple:Extended; //缩放倍数
begin
try
Multiple:=strtofloat(edit1.Text);
if Multiple<=0 then
begin
showmessage('倍数输入值无效!!');
edit1.SetFocus;
exit;
end;
except
showmessage('倍数输入值无效!!');
edit1.SetFocus;
exit;
end;
if pCount<2 then exit;
setlength(ips,3);
//按倍数求新三角形的三端点坐标(第一个点不变):
ips[0].X:=ps[0].X;
ips[0].Y:=ps[0].Y;
ips[1].y:=round(Multiple*(ps[1].Y-ps[0].Y))+ps[0].Y;
ips[1].x:=round(Multiple*(ps[1].x-ps[0].x))+ps[0].x;
ips[2].y:=round(Multiple*(ps[2].Y-ps[0].Y))+ps[0].Y;
ips[2].x:=round(Multiple*(ps[2].x-ps[0].x))+ps[0].x;
//求中心点(三端点的算术平均值):
Center1.X:=(ps[0].X+ps[1].X+ps[2].X) div 3;
Center1.y:=(ps[0].y+ps[1].y+ps[2].y) div 3;
Center2.X:=(ips[0].X+ips[1].X+ips[2].X) div 3;
Center2.y:=(ips[0].y+ips[1].y+ips[2].y) div 3;
//新的三角形中心平移值:
Translation_x:=Center2.X-Center1.x;
Translation_y:=Center2.y-Center1.y;
//把新三角形中心点移到原来的中心点上:
for i:=0 to 2 do
begin
ips[i].X:=ips[i].X-Translation_x;
ips[i].y:=ips[i].Y-Translation_y;
end;
if RadioButton2.Checked then //让同名边中点重合:
begin
if ComboBox2.ItemIndex>0 then
begin
Translation_x:=(ps[ComboBox2.ItemIndex-1].X-ips[ComboBox2.ItemIndex-1].X)div 2;
Translation_y:=(ps[ComboBox2.ItemIndex-1].Y-ips[ComboBox2.ItemIndex-1].Y)div 2;
end
else
begin
Translation_x:=(ps[2].X-ips[2].X)div 2;
Translation_y:=(ps[2].Y-ips[2].Y)div 2;
end;
for i:=0 to 2 do
begin
ips[i].X:=ips[i].X-Translation_x;
ips[i].y:=ips[i].Y-Translation_y;
end;
end
else if RadioButton3.Checked then//让同名角端点重合
begin
Translation_x:=(ips[ComboBox1.ItemIndex].X-ps[ComboBox1.ItemIndex].X);
Translation_y:=(ips[ComboBox1.ItemIndex].Y-ps[ComboBox1.ItemIndex].Y);
for i:=0 to 2 do
begin
ips[i].X:=ips[i].X-Translation_x;
ips[i].y:=ips[i].Y-Translation_y;
end;
end;
// Drawline(ps,clWindow);//清除旧三角形的线条
Drawline(ips,clred);
end;procedure TForm1.Image1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
if pCount<3 then
begin
ps[pCount].X:=X;
ps[pCount].Y:=Y;
Lbl[pCount].Caption:='第'+inttostr(pCount)
+'个点的坐标为'+inttostr(X)+','+inttostr(Y);
inc(pCount);
if pCount=3 then
begin
Drawline(ps,clred);//画线条
Button1.Enabled:=true;
end;
end;
end;
procedure TForm1.RadioButton1Click(Sender: TObject);
begin
ComboBox1.Visible:=false;
ComboBox2.Visible:=false;
if RadioButton3.Checked then ComboBox1.Visible:=true
else if RadioButton2.Checked then ComboBox2.Visible:=true
end;end.
程序运行后,在Image1里随意点三个点,作为三角形的端点...