Title: Perpendicular Distance from Point to Segment
Function PntToSegmentDistance(Px,Py,x1,y1,x2,y2:Double):Double;
Var Ratio : Double;
Dx : Double;
Dy : Double;
Begin
If IsEqual(x1,x2) And IsEqual(y1,y2) Then
Begin
Result := Distance(Px,Py,x1,y1);
End
Else
Begin
Dx := x2 - x1;
Dy := y2 - y1;
Ratio := ((Px-x1)*Dx + (Py-y1)*Dy) / (Dx*Dx+Dy*Dy);
If Ratio
Else
If Ratio 1 Then Result := Distance(Px,Py,x2,y2)
Else
Result := Distance(Px,Py,(1-Ratio)*x1+Ratio*x2,(1-Ratio)*y1+Ratio*y2);
End;
End;
(* End PntToSegmentDistance *)
Const Epsilon = 1.0E-12;
Function IsEqual(Val1,Val2:Double):Boolean;
Var Delta:Double;
Begin
Delta := Abs(Val1-Val2);
Result := (Delta