码迷,mamicode.com
首页 > Windows程序 > 详细

WPF点补间、拟合回归直线

时间:2015-07-28 10:38:28      阅读:135      评论:0      收藏:0      [点我收藏+]

标签:

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

生成正弦点

技术分享
                profilePoint.Value =76 * (1 - Math.Sin(i * Math.PI / 92));
                profilePoint.Type = 1;
View Code

画点

技术分享
 1             EllipseGeometry el = new EllipseGeometry();
 2             el.Center = p;
 3             el.RadiusX = 0.5;
 4             el.RadiusY = 0.5;
 5             
 6             Path mypath = new Path();
 7             mypath.Stroke = s;
 8             mypath.StrokeThickness = 1;
 9             mypath.Data = el;
10 
11             panelCanvas.Children.Add(mypath);
View Code

画线

技术分享
 1             LineGeometry line = new LineGeometry();
 2             line.StartPoint = startPoint;
 3             line.EndPoint = endPoint;
 4 
 5             Path mypath = new Path();
 6             mypath.Stroke = s;
 7             mypath.StrokeThickness = 1;
 8             mypath.Data = line;
 9 
10             panelCanvas.Children.Add(mypath);
View Code

2,回归直线

技术分享
 1         /// <summary>
 2         /// 最小二乘法计算回归直线
 3         /// </summary>
 4         /// <param name="listPoints">最小二乘法计算单位</param>
 5         /// <returns></returns>
 6         public ApproximateLine calApproximateLine(List<Point> listPoints)
 7         {
 8             ApproximateLine appr = new ApproximateLine();
 9             double total = 0;       //临时变量
10             double averageX = 0;    //X平均值
11             double averageY = 0;    //Y平均值
12             double dispersion = 0;  //协方差
13             double b = 0;  //斜率
14             double a = 0;  //y轴截距
15             //x平均值计算
16             total = 0;
17             for (int i = 0; i < listPoints.Count; i++)
18             {
19                 total += listPoints[i].X;
20             }
21             averageX = total / listPoints.Count;
22             //y平均值计算
23             total = 0;
24             for (int i = 0; i < listPoints.Count; i++)
25             {
26                 total += listPoints[i].Y;
27             }
28             averageY = total / listPoints.Count;
29             //协方差计算
30             total = 0;
31             for (int i = 0; i < listPoints.Count; i++)
32             {
33                 total += ((listPoints[i].Y - averageY)*(listPoints[i].X - averageX));
34             }
35             dispersion = total / listPoints.Count;
36             //斜率计算
37             total = 0;
38             double tmp = 0;
39             for (int i = 0; i < listPoints.Count; i++)
40             {
41                 total += listPoints[i].Y * listPoints[i].X;
42                 tmp += Math.Pow(listPoints[i].X, 2);
43             }
44             b = (total - listPoints.Count*averageX*averageY)/(tmp - listPoints.Count*averageX*averageX);
45             //截距计算
46             a = averageY - b * averageX;
47             //确定起止点坐标
48             ScaleProfilePoint p1 = new ScaleProfilePoint();
49             ScaleProfilePoint p2 = new ScaleProfilePoint();
50             p1.Type = 1;
51             p1.ValueX = listPoints[0].X;
52             p1.ValueZ = a + b * p1.ValueX;
53             p2.Type = 1;
54             p2.ValueX = listPoints[listPoints.Count-1].X;
55             p2.ValueZ = a + b * p2.ValueX;
56             //填充返回值
57             appr.Dispersion = dispersion;
58             appr.Horizontal = true;
59             appr.StartPnt = p1;
60             appr.EndPnt = p2;
61             return appr;
62         }
View Code

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

技术分享
this.panelCanvas.Children.Clear();
View Code

 附上工程源码

WPF点补间、拟合回归直线

标签:

原文地址:http://www.cnblogs.com/codeinet/p/4681814.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!