From 578a48ebf7a8099c80a81b433ca0d14b4af6af6f Mon Sep 17 00:00:00 2001 From: Pawel Baran Date: Thu, 15 Feb 2024 14:58:40 +0100 Subject: [PATCH] #3285 fixed --- Geometry_Engine/Query/IsCollinear.cs | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/Geometry_Engine/Query/IsCollinear.cs b/Geometry_Engine/Query/IsCollinear.cs index e25a0bab52..888b781bf9 100644 --- a/Geometry_Engine/Query/IsCollinear.cs +++ b/Geometry_Engine/Query/IsCollinear.cs @@ -45,18 +45,14 @@ public static bool IsCollinear(this List pts, double tolerance = Toleranc if (pts.Count < 3) return true; - double[,] vMatrix = new double[pts.Count - 1, 3]; - for (int i = 0; i < pts.Count - 1; i++) - { - vMatrix[i, 0] = pts[i + 1].X - pts[0].X; - vMatrix[i, 1] = pts[i + 1].Y - pts[0].Y; - vMatrix[i, 2] = pts[i + 1].Z - pts[0].Z; - } - - double REFTolerance = vMatrix.REFTolerance(tolerance); - double[,] rref = vMatrix.RowEchelonForm(true, REFTolerance); - int nonZeroRows = rref.CountNonZeroRows(REFTolerance); - return nonZeroRows < 2; + Line fitLine = pts.FitLine(tolerance); + + // Coincident points can be considered collinear + if (fitLine == null) + return true; + + double sqTol = tolerance * tolerance; + return pts.All(x => x.SquareDistance(fitLine, true) < sqTol); }