whats new ¦  programming tips ¦  indy articles ¦  intraweb articles ¦  informations ¦  links ¦  interviews ¦  misc ¦  forum
 kylix ¦  tutorials ¦  online shop ¦  photos ¦  Add&Win Game

Tips (1565)

Database (91)
Files (139)
Forms (113)
Graphic (116)
IDE (21)
Indy (5)
Internet / LAN (133)
IntraWeb (0)
Kylix (10)
Math (77)
Misc (128)
Multimedia (46)
Objects/
ActiveX (51)

OpenTools API (3)
Printing (35)
Strings (83)
System (268)
VCL (246)

Top15

Tips sort by
component


Search Tip

Add new Tip

Add&Win Game

Advertising

27 Visitors Online


SwissDelphiCenter is a Borland Technology Partner
 
...Perpendicular distance from Point to Segment in 2D?
Autor: Arash Partow
Homepage: http://www.partow.net
[ Print tip ]  

Tip Rating (2):  
     


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 < 0 then Result := Distance(Px, Py, x1, y1)
    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 *)



Note: Distance is simple pythagoras distance routine

// calcule la distance entre 1 point et 1 droite (bidimensionnel)


 

Rate this tip:

poor
very good


Copyright © by SwissDelphiCenter.ch
All trademarks are the sole property of their respective owners