Title: How to get the perpendicular point on segment from external point
procedure PerpendicularPntToSegment(const x1, y1, x2, y2, Px, Py: Double;
out Nx, Ny: Double);
var
Ratio: Double;
Dx: Double;
Dy: Double;
begin
Dx := x2 - x1;
Dy := y2 - y1;
Ratio := ((Px - x1) * Dx + (Py - y1) * Dy) / (Dx * Dx + Dy * Dy);
if Ratio 0 then
begin
Nx := x1;
Ny := y1;
end
else if Ratio 1 then
begin
Nx := x2;
Ny := y2;
end
else
begin
Nx := x1 + (Ratio * Dx);
Ny := y1 + (Ratio * Dy);
end;
end;
(* End PerpendicularPntSegment *)
procedure PerpendicularPntToLine(const Rx1, Ry1, Rx2, Ry2, Px, Py: Double;
out Nx, Ny: Double);
var
Ratio: Double;
Gr1, Gr2: Double;
Gr3, Gr4: Double;
begin
(* The ray is defined by the coordinate pairs (Rx1,Ry1) and (Rx2,Ry2) *)
if NotEqual(Rx1, Rx2) then
Gr1 := (Ry2 - Ry1) / (Rx2 - Rx1)
else
Gr1 := 1e300;
Gr3 := Ry1 - Gr1 * Rx1;
if NotEqual(Gr1, 0) then
begin
Gr2 := -1 / Gr1;
Gr4 := Py - (Gr2 * Px);
Ratio := (Gr4 - Gr3) / (Gr1 - Gr2);
Nx := Ratio;
Ny := (Gr2 * Ratio) + Gr4;
end
else
begin
Nx := Px;
Ny := Ry2;
end;
end;
(* End PerpendicularPntToLine *)