Mega Code Archive

 
Categories / Delphi / Graphic
 

How to Perpendicular from Point to Segment in 2D

Title: How to Perpendicular from Point to Segment in 2D procedure PerpendicularPntToSegment(x1, y1, x2, y2, Px, Py: Double; var Nx, Ny: Double); var R: Double; Dx: Double; Dy: Double; begin Dx := x2 - x1; Dy := y2 - y1; R := ((Px - x1) * Dx + (Py - y1) * Dy) / Sqr(Dx * Dx + Dy * Dy); Nx := x1 + R * Dx; Ny := y1 + R * Dy; end; (* End PerpendicularPntSegment *)