1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
| function DistPS(PC,PA,PB:point;var PP:point):single ;
{
distance from point C to segment AB
input PC <point> coordinates of point C
PA,PB <point> coordinates of segment extremities
PP <pointer> point on AB segment nearest of C
result <point> distance
!}
var
XAC,XBC,XAB,XAC2,XAB2,XBC2,R,XBN : single ;
N : GEO_llrec;
begin
try
XAC:=DistPP(PC,PA) ; // GEO_DISTPP(PI,PJ)=Distance du point PI au point PJ
XBC:=DistPP(PC,PB) ;
XAB:=DistPP(PA,PB) ;
if XAB<0.01 then begin
result:=XAC ;
PP:=PA;
exit ;
end ;
XAB2:=XAB*XAB ; XAC2:=XAC*XAC ; XBC2:=XBC*XBC ;
if XBC2+XAB2<=XAC2 then begin
result:=XBC ;
PP=PB;
exit ;
end ;
if XAC2+XAB2<=XBC2 then begin
result:=XAC ;
PP:=PA;
exit ;
end ;
R:=2*XAB2*XBC2+2*XBC2*XAC2+2*XAC2*XAB2-XAB2*XAB2-XBC2*XBC2-XAC2*XAC2 ;
if R>0.01
then begin
result:=SQRT(R) /(2*XAB);
(* compute coordinate of nearest point *)
try XBN:=sqrt(XBC2-(result*result)) except XBN:=0; end;
PP.X:=PB.X+((XBN / XAB)*(PA.X-PB.X);
PP.Y:=PB.Y+((XBN / XAB)*(PA.Y-PB.Y);
end;
end
else begin
(* C is on AB segment *)
result:=0 ;
PP:=PC;
end;
end ; (*DistPS*) |
Partager