Several Maths functions II

Obtener el circuncentro de 3 puntos
procedure Circumcenter(const x1, y1, x2, y2, x3, y3: Double; out Px, Py: Double);
var
A: Double;
C: Double;
B: Double;
D: Double;
E: Double;
F: Double;
G: Double;
begin
A := x2 - x1;
B := y2 - y1;
C := x3 - x1;
D := y3 - y1;
E := A * (x1 + x2) + B * (y1 + y2);
F := C * (x1 + x3) + D * (y1 + y3);
G := 2.0 * (A * (y3 - y2) - B * (x3 - x2));
if IsEqual(G, 0.0) then Exit;
Px := (D * E - B * F) / G;
Py := (A * F - C * E) / G;
end;
(* End of Circumcenter *)


Obtener el incentro de 3 puntos
procedure Incenter(const x1, y1, x2, y2, x3, y3: Double; out Px, Py: Double);
var
Perim: Double;
Side12: Double;
Side23: Double;
Side31: Double;
begin
Side12 := Distance(x1, y1, x2, y2);
Side23 := Distance(x2, y2, x3, y3);
Side31 := Distance(x3, y3, x1, y1);
(* Using Heron's S=UR *)
Perim := 1.0 / (Side12 + Side23 + Side31);
Px := (Side23 * x1 + Side31 * x2 + Side12 * x3) * Perim;
Py := (Side23 * y1 + Side31 * y2 + Side12 * y3) * Perim;
end;
(* End of Incenter *)


Distancia entre dos puntos
function Distance(const x1, y1, x2, y2: Double): Double;
var
dx: Double;
dy: Double;
begin
dx := x2 - x1;
dy := y2 - y1;
Result := Sqrt(dx * dx + dy * dy);
end;
(* End of Distance *)


Perpendicular desde un punto a un segmento en 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 *)
// trouve la perpendiculaire entre un point et une droite (2D)


Distancia perpendicular desde un punto a un segmento en 2D
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 <> 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)

Autor: Arash Partow
http://www.partow.net/


Relacionados

Rotación de un octaedro
Modelado 3D con Delphi

Funciones relacionadas con la geometria
Funciones estadisticas, xml en Delphi