public static void DrawApproximateLine(Graphics g, Pen pen, ApproximateLine line) { IntPoint first = line.Points.First(); IntPoint last = line.Points.Last(); IntPoint diff = first - last; g.DrawLine(pen, new System.Drawing.Point(first.X + diff.X, first.Y + diff.Y), new System.Drawing.Point(last.X - diff.X, last.Y - diff.Y)); }
public ProcessedBlob(Blob blob, List<IntPoint> edges, List<IntPoint> hull, Vector4 bounds, ApproximateLine[] lines, bool isQuad, List<IntPoint> quadPoints = null) { Blob = blob; Edges = edges; Hull = hull; Bounds = bounds; Lines = lines; IsQuad = isQuad; if (isQuad) { QuadPoints = quadPoints; } else { QuadPoints = new List<IntPoint>(); } }
private static ApproximateLine[] findLines(List<IntPoint> points) { if (points.Count < 3) { return new ApproximateLine[0]; } List<ApproximateLine> results = new List<ApproximateLine>(); ApproximateLine line = null; double lineAngle = 0, newAngle, diff, distance; IntPoint previous = new IntPoint(); //Essentially, initialization is null bool quitting = false; for(int i = 0; i < points.Count; i++) { IntPoint point = points[i]; if (results.Count > 0 && point == results[0].Points.Last()) { quitting = true; } if (line == null) { line = new ApproximateLine(); line.Add(point); previous = point; continue; } if (line.Points.Count == 1) { lineAngle = Math.Atan2((double)point.Y - (double)previous.Y, (double)point.X - (double)previous.X); line.Add(point); } else { newAngle = Math.Atan2((double)point.Y - (double)previous.Y, (double)point.X - (double)previous.X); diff = Math.Min((2d * Math.PI) - Math.Abs(newAngle - lineAngle), Math.Abs(newAngle - lineAngle)); if (diff > AngleErrorMargin) { results.Add(line); if (quitting) { break; } line = new ApproximateLine(); line.Add(previous); line.Add(point); } else { distance = point.DistanceTo(previous); lineAngle = (lineAngle * line.Length + newAngle * distance) / (line.Length + distance); line.Add(point); } } previous = point; if (i + 1 == points.Count) { i = 0; } } results.RemoveAt(0); return results.ToArray(); }