WPF点补间、拟合回归直线

时间:2023-09-06 17:29:58

1,path画刷,绘制正弦 点,线;

生成正弦点

                profilePoint.Value = * ( - Math.Sin(i * Math.PI / ));
profilePoint.Type = ;

画点

             EllipseGeometry el = new EllipseGeometry();
el.Center = p;
el.RadiusX = 0.5;
el.RadiusY = 0.5; Path mypath = new Path();
mypath.Stroke = s;
mypath.StrokeThickness = ;
mypath.Data = el; panelCanvas.Children.Add(mypath);

画线

             LineGeometry line = new LineGeometry();
line.StartPoint = startPoint;
line.EndPoint = endPoint; Path mypath = new Path();
mypath.Stroke = s;
mypath.StrokeThickness = ;
mypath.Data = line; panelCanvas.Children.Add(mypath);

2,回归直线

         /// <summary>
/// 最小二乘法计算回归直线
/// </summary>
/// <param name="listPoints">最小二乘法计算单位</param>
/// <returns></returns>
public ApproximateLine calApproximateLine(List<Point> listPoints)
{
ApproximateLine appr = new ApproximateLine();
double total = ; //临时变量
double averageX = ; //X平均值
double averageY = ; //Y平均值
double dispersion = ; //协方差
double b = ; //斜率
double a = ; //y轴截距
//x平均值计算
total = ;
for (int i = ; i < listPoints.Count; i++)
{
total += listPoints[i].X;
}
averageX = total / listPoints.Count;
//y平均值计算
total = ;
for (int i = ; i < listPoints.Count; i++)
{
total += listPoints[i].Y;
}
averageY = total / listPoints.Count;
//协方差计算
total = ;
for (int i = ; i < listPoints.Count; i++)
{
total += ((listPoints[i].Y - averageY)*(listPoints[i].X - averageX));
}
dispersion = total / listPoints.Count;
//斜率计算
total = ;
double tmp = ;
for (int i = ; i < listPoints.Count; i++)
{
total += listPoints[i].Y * listPoints[i].X;
tmp += Math.Pow(listPoints[i].X, );
}
b = (total - listPoints.Count*averageX*averageY)/(tmp - listPoints.Count*averageX*averageX);
//截距计算
a = averageY - b * averageX;
//确定起止点坐标
ScaleProfilePoint p1 = new ScaleProfilePoint();
ScaleProfilePoint p2 = new ScaleProfilePoint();
p1.Type = ;
p1.ValueX = listPoints[].X;
p1.ValueZ = a + b * p1.ValueX;
p2.Type = ;
p2.ValueX = listPoints[listPoints.Count-].X;
p2.ValueZ = a + b * p2.ValueX;
//填充返回值
appr.Dispersion = dispersion;
appr.Horizontal = true;
appr.StartPnt = p1;
appr.EndPnt = p2;
return appr;
}

3,去除canvas内的元素,清空画布

this.panelCanvas.Children.Clear();

附上工程源码