was ist neu ¦  programmier tips ¦  indy artikel ¦  intraweb artikel ¦  informationen ¦  links ¦  interviews
 sonstiges ¦  tutorials ¦  Add&Win Gewinnspiel

Tips (1541)

Dateien (137)
Datenbanken (90)
Drucken (35)
Grafik (114)
IDE (21)
Indy (5)
Internet / LAN (130)
IntraWeb (0)
Mathematik (76)
Multimedia (45)
Oberfläche (107)
Objekte/
ActiveX (51)

OpenTools API (3)
Sonstiges (126)
Strings (83)
System (266)
VCL (242)

Tips sortiert nach
Komponente


Tip suchen

Tip hinzufügen

Add&Win Gewinnspiel

Werbung

39 Visitors Online


 
...die Distanz zwischen einem Punkt und einem 2D segment berechnen?
Autor: Arash Partow
Homepage: http://www.partow.net
[ Tip ausdrucken ]  

Tip Bewertung (11):  
     


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)


 

Bewerten Sie diesen Tipp:

dürftig
ausgezeichnet


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