implementation
{$R *.dfm}
function PointToPointDist(Ax, Ay, Bx, By: double): double;
begin
Result := sqrt(sqr(Bx - Ax) + sqr(By - Ay));
end;
function MinDistPointLine(Px, Py, Ax, Ay, Bx, By: double): double;
var
q: double;
begin
if (Ax = Bx) and (Ay = By) then
begin
Result := PointToPointDist(Px, Py, Ax, Ay);
end
else
begin
q := ((Px - Ax) * (Bx - Ax) + (Py - Ay) * (By - Ay)) / (sqr(Bx - Ax) + sqr(By - Ay));
if q < 0 then
q := 0;
if q > 1 then
q := 1;
Result := PointToPointDist(Px, Py, (1 - q) * Ax + q * Bx, (1 - q) * Ay + q * By);
end;
end;
procedure TForm1.FormMouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
var a:double;
begin
Label1.Caption:=inttostr(x)+' '+inttostr(y);
a:=MinDistPointLine(x, y, 100, 100, 200, 200);
if a<2 then Label2.Caption:='The mouse is over the line' else
Label2.Caption:='The mouse isn''t over the line';
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
Canvas.Pen.Width:=3;
Canvas.MoveTo(100, 100);
Canvas.LineTo(200, 200);
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
end;
end.