improve:直线拉直

水平直线算法改进
This commit is contained in:
2025-12-31 16:57:03 +08:00
parent c28a2bd792
commit 9bd1214567
@@ -1201,14 +1201,59 @@ namespace Ink_Canvas
directionY = eigenvalue1 - covXX; directionY = eigenvalue1 - covXX;
// 归一化 // 归一化
double length = Math.Sqrt(directionX * directionX + directionY * directionY); double length = Math.Sqrt(directionX * directionX + directionY * directionY);
directionX /= length; if (length > 1e-10)
directionY /= length; {
directionX /= length;
directionY /= length;
}
else
{
// 如果归一化失败,使用起点和终点计算方向
Point start = points.First();
Point end = points.Last();
double dx = end.X - start.X;
double dy = end.Y - start.Y;
double lineLength = Math.Sqrt(dx * dx + dy * dy);
if (lineLength > 1e-10)
{
directionX = dx / lineLength;
directionY = dy / lineLength;
}
else
{
directionX = (covXX >= covYY) ? 1 : 0;
directionY = (covXX >= covYY) ? 0 : 1;
}
}
} }
else else
{ {
// 如果协方差为 0,则是水平或垂直直线 Point start = points.First();
directionX = (covXX >= covYY) ? 1 : 0; Point end = points.Last();
directionY = (covXX >= covYY) ? 0 : 1; double dx = end.X - start.X;
double dy = end.Y - start.Y;
double lineLength = Math.Sqrt(dx * dx + dy * dy);
if (lineLength > 1e-10)
{
directionX = dx / lineLength;
directionY = dy / lineLength;
}
else
{
if (Math.Abs(eigenvalue1 - covXX) < Math.Abs(eigenvalue1 - covYY))
{
// 主要方向是 X 轴方向
directionX = 1;
directionY = 0;
}
else
{
// 主要方向是 Y 轴方向
directionX = 0;
directionY = 1;
}
}
} }
// 计算解释方差比例(拟合优度) // 计算解释方差比例(拟合优度)