Several Maths functions I

Determinar si 3 puntos están sobre la misma línea en 2D
function Collinear(x1, y1, x2, y2, x3, y3: Double): Boolean;
begin
Result := (((x2 - x1) * (y3 - y1) - (x3 - x1) * (y2 - y1)) = 0);
end;
(* End Of Collinear *)

// vérifier si 3 points appartiennent à une même droite bidimensionnelle
// c.a.d s'ils sont alignés.

...Determine if 3 points are collinear in 2D?

function Collinear(x1, y1, x2, y2, x3, y3: Double): Boolean;
begin
Result := (((x2 - x1) * (y3 - y1) - (x3 - x1) * (y2 - y1)) = 0);
end;
(* End Of Collinear *)

// vérifier si 3 points appartiennent à une même droite bidimensionnelle
// c.a.d s'ils sont alignés.


Determinar el punto en el que 2 segmentos 2D se tocan
procedure IntersectPoint(x1, y1, x2, y2, x3, y3, x4, y4: Double; var Nx, Ny: Double);
var
R: Double;
dx1, dx2, dx3: Double;
dy1, dy2, dy3: Double;
begin
dx1 := x2 - x1;
dx2 := x4 - x3;
dx3 := x1 - x3;

dy1 := y2 - y1;
dy2 := y1 - y3;
dy3 := y4 - y3;

R := dx1 * dy3 - dy1 * dx2;

if R <> 0 then
begin
R := (dy2 * (x4 - x3) - dx3 * dy3) / R;
Nx := x1 + R * dx1;
Ny := y1 + R * dy1;
end
else
begin
if Collinear(x1, y1, x2, y2, x3, y3) then
begin
Nx := x3;
Ny := y3;
end
else
begin
Nx := x4;
Ny := y4;
end;
end;
end;



function Collinear(x1, y1, x2, y2, x3, y3: Double): Boolean;
begin
Result := (((x2 - x1) * (y3 - y1) - (x3 - x1) * (y2 - y1)) = 0);
end;
(* End Of Collinear *)

// calcule le point d'intersection de 2 droites bidimensionnelles


Verifica si 2 segmentos 2D son paralelos
function SegmentsParallel(x1, y1, x2, y2, x3, y3, x4, y4: Double): Boolean;
begin
Result := (((y1 - y2) * (x1 - x2)) = ((y3 - y4) * (x3 - x4)));
end;
(* End Of SegmentsParallel *)



Verifica si 2 segmentos 3D son paralelos
function SegmentsParallel(x1, y1, z1, x2, y2, z2, x3, y3, z3, x4, y4, z4: Double): Boolean;
var
Dx1, Dx2: Double;
Dy1, Dy2: Double;
Dz1, Dz2: Double;
begin
{
Theory:
If the gradients in the following planes x-y, y-z, z-x are equal then it can be
said that the segments are parallel in 3D, However as of yet I haven't been able
to prove this "mathematically".

Worst case scenario: 6 floating point divisions and 9 floating point subtractions
}

Result := False;

{
There is a division-by-zero problem that needs attention.
My initial solution to the problem is to check divisor of the divisions.
}


Dx1 := x1 - x2;
Dx2 := x3 - x4;

//If (IsEqual(dx1,0.0) Or IsEqual(dx2,0.0)) And NotEqual(dx1,dx2) Then Exit;

Dy1 := y1 - y2;
Dy2 := y3 - y4;

//If (IsEqual(dy1,0.0) Or IsEqual(dy2,0.0)) And NotEqual(dy1,dy2) Then Exit;

Dz1 := z1 - z2;
Dz2 := z3 - z4;

//If (IsEqual(dy1,0.0) Or IsEqual(dy2,0.0)) And NotEqual(dy1,dy2) Then Exit;


if NotEqual(Dy1 / Dx1, Dy2 / Dx2) then Exit;
if NotEqual(Dz1 / Dy1, Dz2 / Dy2) then Exit;
if NotEqual(Dx1 / Dz1, Dx2 / Dz2) then Exit;

Result := True;
end;
(* End Of SegmentsParallel*)

const
Epsilon = 1.0E-12;

function IsEqual(Val1, Val2: Double): Boolean;
var
Delta: Double;
begin
Delta := Abs(Val1 - Val2);
Result := (Delta <= Epsilon); end; (* End Of Is Equal *) function NotEqual(Val1, Val2: Double): Boolean; var Delta: Double; begin Delta := Abs(Val1 - Val2); Result := (Delta > Epsilon);
end;
(* End Of Not Equal *)

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

Links relacionados:
Rotación de un octaedro
Modelado 3D con Delphi