关于canvas的小问题,求两点间距离(50分)

  • 主题发起人 主题发起人 小争
  • 开始时间 开始时间

小争

Unregistered / Unconfirmed
GUEST, unregistred user!
我随手写的在image中求两点间距离,怎么结果不对呀,大家帮看一看。
另如果控件没有mouseup或mousemove等事件,怎么实现求距离。
procedure Tmainform.Image1MouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
VAR
s,j:real;
s2,j2:real;
he:real;
begin
case z of
true:
begin
image1.Canvas.MoveTo(x,y);
s:=image1.Canvas.PenPos.x;
j:=image1.Canvas.PenPos.y;
z:=false;
end;
false:
begin
image1.Canvas.LineTo(x,y);
image1.Canvas.MoveTo(x,y);
s2:=image1.Canvas.PenPos.x;
j2:=image1.Canvas.PenPos.y;
he:=sqrt(sqr(s2-s)+sqr(j2-j));
label1.Caption:='the distance is'+inttostr(round(he));
z:=true;
end;
end;
end;
 
随手写的,不一定是最好的。
var
Form1: TForm1;
srcPoint:tpoint;
destPoint:tpoint;
implementation

{$R *.dfm}

procedure TForm1.Image1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
srcPoint.X :=x;
srcPoint.Y :=y;

end;

procedure TForm1.Image1MouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
destPoint.X :=x;
destPoint.Y :=y;
caption:='distanceX: '+inttostr(abs(destPoint.X-srcPoint.X))+'___'+
'distanceY: '+inttostr(abs(destPoint.Y-srcPoint.Y))
end;
 
不要用 PenPos 了.
var
Form1: TForm1;
p1,p2 :TPoint;
dis :Extended;

implementation

{$R *.DFM}

procedure TForm1.Image1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
p1 :=Point(x,y);
end;

procedure TForm1.Image1MouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
p2 :=Point(x,y);
dis :=Sqrt(Sqr(p1.x-p2.x)+Sqr(p1.y-p2.y));
ShowMessage(FloatToStr(dis));
end;
 
接受答案了.
 
后退
顶部