在2d空间中,通过整合一些离散点, 拟合出表现这些点线性分布规律的直线
请见flash示例:
源码片段:
// /* * 直线拟合实现 * @param vs 是一个数组,包含若干待计算点(每一个点是{x:0,y:0}型的Object的对象) * @param len 是vs数组的长度 * @param pa 是一个as3 Object对象->{x:0,y:0},是计算出来线段的第一个端点 * @param pb 是一个as3 Object对象->{x:0,y:0},是计算出来线段的第二个端点 * * */ private function linearFitting(vs:Array,len: int,pa:Object, pb:Object): void { var A:Number = sumVSY( vs ); var B:Number = sumVSX( vs ); // var C:Number = sumVSXMultY( vs ); var D:Number = sumVSXMultX( vs ); // 计算出直线的参数k和b(基于:y= k*x + b) var k:Number = (A * B - len * C) / (B * B - len * D); var b:Number = (B * C - A * D) / (B * B - len * D); // var fr:Rectangle = DynamicDrawer.createByVS(vs); pa.x = fr.x; pb.x = fr.right; pa.y = k * pa.x + b; pb.y = k * pb.x + b; } private function sumVSY(vs:Array):Number { var len: int = vs.length; var tem:Number = 0; for ( var i: int = 0; i < len; i++) { tem += vs[i].y; } return tem; } private function sumVSX(vs:Array):Number { var len: int = vs.length; var tem:Number = 0; for ( var i: int = 0; i < len; i++) { tem += vs[i].x; } return tem; } private function sumVSXMultY(vs:Array):Number { var len: int = vs.length; var tem:Number = 0; for ( var i: int = 0; i < len; i++) { tem += vs[i].x * vs[i].y; } return tem; } private function sumVSXMultX(vs:Array):Number { var len: int = vs.length; var tem:Number = 0; for ( var i: int = 0; i < len; i++) { tem += vs[i].x * vs[i].x; } return tem; }