Skip to content

Commit

Permalink
#3278 fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
pawelbaran committed Feb 12, 2024
1 parent 4e5ef3c commit 19144dc
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions Geometry_Engine/Compute/FitLine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,16 @@ public static Line FitLine(this IEnumerable<Point> points, double tolerance = To
double sumXY = points.Sum(x => x.X * x.Y);
double p1 = sumX * sumX - sumY * sumY - n * (sumXsq - sumYsq);
double p2 = n * sumXY - sumX * sumY;
double b = (p1 + Math.Sqrt(p1 * p1 + 4 * p2 * p2)) / (2 * p2);
Vector dir = new Vector { X = 1, Y = b };

Vector dir;
if (Math.Abs(p2) > tolerance)
{
double b = (p1 + Math.Sqrt(p1 * p1 + 4 * p2 * p2)) / (2 * p2);
dir = new Vector { X = 1, Y = b };
}
else
dir = p1 < 0 ? Vector.XAxis : Vector.YAxis;

return new Line { Start = C, End = C + dir };
}
else
Expand Down

0 comments on commit 19144dc

Please sign in to comment.