The following example shows how to tell the distance between two
points. In this example, the caption of the form shows the current
point and distance from the center of the form. The y coordinates are
inverted to show a more natural north orientation where north is
directly up.
Example:
funcTion Distance(Pt : TPoint;
Pt2 : TPoint) : Double;
var
dx : LongInt;
dy : LongInt;
begin
dx:= pt .x - pt2.x;
dy:= pt .y - pt2.y;
Result := Sqrt((Dx * Dx) + (Dy * Dy));
end;
procedure TForm .FormMouseMove(Sender: TObject; Shift:
TShiftState; X, Y: Integer);
var
Pt : TPoint;
Pt2 : TPoint;
Dist : Double;
begin
Pt .x := Form .Width div 2;
Pt .y := Form .Height div 2;
Pt2.x := x;
Pt2.y := y;
Dist := Distance(Pt , Pt2);
Form .Caption := IntToStr(Pt2.x - Pt .x) + #32 +
IntToStr(Pt .y - Pt2.y) + ' = ' +
FloatToStr(Dist);
end;
procedure TForm .FormPaint(Sender: TObject);
begin
Form .Canvas.MoveTo(Form .Width div 2,
0);
Form .Canvas.LineTo(Form .Width div 2,
Form .Height);
Form .Canvas.MoveTo(0,
Form .Height div 2);
Form .Canvas.LineTo(Form .Width,
Form .Height div 2);
end;
procedure TForm .FormResize(Sender: TObject);
begin
Invalidate;
end;
7/ 6/98 4:3 :28 PM