public Correlation(TouchPoint2 points) { //Account for a degenerate amount of points if (points.Stroke.StylusPoints.Count <= 1) { RSquared = 0; Slope = 0; Intercept = 0; VerticalLine = false; SlopeRad = 0; return; } var pointlist = points.Stroke.StylusPoints; //Remove duplicate points var workingList = new StylusPointCollection(); for (int i = 1; i < pointlist.Count; i++ ) { var point1 = pointlist[i - 1]; var point2 = pointlist[i]; if (!(point1.X == point2.X && point1.Y == point2.Y)) { workingList.Add(point1); } } VerticalLine = false; xavg = 0; yavg = 0; foreach (var p in workingList) { xavg += p.X; yavg += p.Y; } xavg = xavg / workingList.Count; yavg = yavg / workingList.Count; double numerator = 0; double denominator = 0; foreach (var p in workingList) { numerator += (p.X - xavg) * (p.Y - yavg); denominator += Math.Pow(p.X - xavg,2); } SlopeRad = Math.Atan2(numerator, denominator); if (denominator != 0) { Slope = numerator / denominator; Intercept = yavg - Slope * xavg; } else { VerticalLine = true; } TouchPoint2 tp = points.GetEmptyCopy(); if(workingList.Count > 0) tp.Stroke.StylusPoints = workingList; RSquared = CalculateRSquared(tp); }
private ValidSetOfTouchPoints ValidateBox(TouchPoint2 points) { ValidSetOfTouchPoints output = new ValidSetOfTouchPoints(); int length = points.Stroke.StylusPoints.Count; if (length < 1) { return output; } List<string> slopes = new List<string>(); TouchPoint2 newPoints = points.GetEmptyCopy(); for (int i = 0; i < length - 1; i++) { var point1 = points.Stroke.StylusPoints[i]; var point2 = points.Stroke.StylusPoints[i + 1]; double slope = TrigonometricCalculationHelper.GetSlopeBetweenPoints(point1, point2); double distance = TrigonometricCalculationHelper.GetDistanceBetweenPoints(point1, point2); string stringSlope = TouchPointExtensions.SlopeToDirection(slope); if (distance > 0) { newPoints.Stroke.StylusPoints.Add(point1); Correlation recognizer = new Correlation(newPoints); if (Math.Abs(recognizer.RSquared) < TOLERANCE + 0.15) { int linelength = newPoints.Stroke.StylusPoints.Count; double lineSlope = TrigonometricCalculationHelper.GetSlopeBetweenPoints(newPoints.Stroke.StylusPoints[1], newPoints.Stroke.StylusPoints[linelength - 1]); string lineStringSlope = TouchPointExtensions.SlopeToDirection(lineSlope); slopes.Add(lineStringSlope); newPoints = newPoints.GetEmptyCopy(); } } } RectangleParser parser = new RectangleParser(); bool hasRect = parser.Advance(slopes); if (hasRect) { output.Add(points); } return output; }