Code:
function Collinear(const x1,y1,z1,x2,y2,z2,x3,y3,z3:TFloat):Boolean;
var
Dx1 : TFloat;
Dx2 : TFloat;
Dy1 : TFloat;
Dy2 : TFloat;
Dz1 : TFloat;
Dz2 : TFloat;
Cx : TFloat;
Cy : TFloat;
Cz : TFloat;
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);
end;
function RobustCollinear(const x1,y1,x2,y2,x3,y3:TFloat; const Epsilon : TFloat = Epsilon_High):Boolean;
var
LeyDist1 : TFloat;
LeyDist2 : TFloat;
LeyDist3 : TFloat;
begin
LeyDist1 := LayDistance(x1,y1,x2,y2);
LeyDist2 := LayDistance(x2,y2,x3,y3);
LeyDist3 := LayDistance(x3,y3,x1,y1);
if LeyDist1 >= LeyDist2 then
if LeyDist1 >= LeyDist3 then
Result := IsEqual(MinimumDistanceFromPointToLine(x3,y3,x1,y1,x2,y2),0.0,Epsilon)
else
Result := IsEqual(MinimumDistanceFromPointToLine(x2,y2,x3,y3,x1,y1),0.0,Epsilon)
else if LeyDist2 >= LeyDist3 then
Result := IsEqual(MinimumDistanceFromPointToLine(x1,y1,x2,y2,x3,y3),0.0,Epsilon)
else
Result := IsEqual(MinimumDistanceFromPointToLine(x2,y2,x3,y3,x1,y1),0.0,Epsilon);
end;
function IsPointCollinear(const x1,y1,x2,y2,Px,Py:TFloat; const Robust : Boolean = False):Boolean;
begin
(*
This method will return true iff the point (px,py) is collinear
to points (x1,y1) and (x2,y2) and exists on the segment A(x1,y1)->B(x2,y2)
*)
if (((x1 <= px) and (px <= x2)) or ((x2 <= px) and (px <= x1))) and
(((y1 <= py) and (py <= y2)) or ((y2 <= py) and (py <= y1))) then
begin
if Robust then
Result := RobustCollinear(x1,y1,x2,y2,Px,Py)
else
Result := Collinear(x1,y1,x2,y2,Px,Py);
end
else
Result := False;
end;
Hope this helps.
Bookmarks