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

Author: Arash Partow
Homepage: http://www.partow.net

Category: Math

function Collinear(x1, y1, z1, x2, y2, z2, x3, y3, z3: Double): Boolean;
var 
  
Dx1, Dx2: Double;
  Dy1, Dy2: Double;
  Dz1, Dz2: Double;
  Cx, Cy, Cz: Double;
  //Var AB,AC,BC:Double;
begin
  
{find the difference between the 2 points P2 and P3 to P1 }
  
Dx1 := x2 - x1;
  Dy1 := y2 - y1;
  Dz1 := z2 - z1;

  Dx2 := x3 - x1;
  Dy2 := y3 - y1;
  Dz2 := z3 - z1;

  {perform a 3d cross product}
  
Cx := Dy1 * Dz2 - Dy2 * Dz1;
  Cy := Dx2 * Dz1 - Dx1 * Dz2;
  Cz := Dx1 * Dy2 - Dx2 * Dy1;

  Result := IsEqual(Cx * Cx + Cy * Cy + Cz * Cz, 0.0);

 {
  Note:
   The method below is very stable and logical, however at the same time
   it is "VERY" inefficient, it requires 3 SQRTs which is not acceptable...
 Result:=False;
 AB:=Distance(x1,y1,z1,x2,y2,z2);
 AC:=Distance(x1,y1,z1,x3,y3,z3);
 BC:=Distance(x2,y2,z2,x3,y3,z3);

 If (AB+AC) = BC Then Result:=True
  Else
   If (AB+BC) = AC Then Result:=True
    Else
     If (AC+BC) = AB Then Result:=True;
 }
end;
(* End Of Collinear *)

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

 

printed from
www.swissdelphicenter.ch
developers knowledge base