Mega Code Archive

 
Categories / Delphi / Graphic
 

Determine if three 3D Points are collinear to each other

Title: Determine if three 3D Points are collinear to each other 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 := ((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 *)